-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
100 lines (73 loc) · 3.08 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import os
import sys
class AbstractDemo(object):
def __init__(self, file_name: str, file_extension: str, number_of_object: int = 3) -> None:
self.file_name = file_name
self.file_extension = file_extension
self.number_of_object = number_of_object
self.input_directory = os.getcwd() + "/output/"
self.output_directory = os.getcwd() + "/demo/"
self.file = self.load_file(
self.file_name, self.file_extension) # this is a json array
self.output_data = []
self.action()
def action(self):
self.pre_build_demo()
self.build_demo()
self.pre_write_file()
self.write_file()
def pre_build_demo(self):
# override this method for different file type
pass
def load_file(self, file_name, file_extension):
print("Loading file: " + file_name + "." + file_extension)
with open(self.input_directory + file_name + "." + file_extension, "r") as file:
return self.handle_loading_file(file)
def build_demo(self):
print("Creating demo file")
count = 0
while count < self.number_of_object:
self.output_data.append(self.file[count])
count += 1
def pre_write_file(self):
# override this method for different file type
pass
def write_file(self):
with open(self.output_directory + self.file_name + "_demo" + "." + self.file_extension, "w") as file:
self.handle_writing_file(file)
def handle_loading_file(self, file):
pass
def handle_writing_file(self, file):
pass
class jsonDemo(AbstractDemo):
def __init__(self, file_name: str, file_extension: str, number_of_object: int = 3) -> None:
super().__init__(file_name, file_extension, number_of_object)
def handle_loading_file(self, file):
return json.loads(file.read())
def handle_writing_file(self, file):
json.dump(self.output_data, file, indent=4)
class ndjsonDemo(AbstractDemo):
def __init__(self, file_name: str, file_extension: str, number_of_object: int = 3) -> None:
super().__init__(file_name, file_extension, number_of_object)
def handle_loading_file(self, file):
return file.readlines()
def handle_writing_file(self, file):
file.writelines(self.output_data)
class csvDemo(AbstractDemo):
def __init__(self, file_name: str, file_extension: str, number_of_object: int = 3) -> None:
super().__init__(file_name, file_extension, number_of_object + 1) # +1 for header
def handle_loading_file(self, file):
return file.readlines()
def handle_writing_file(self, file):
file.writelines(self.output_data)
if __name__ == "__main__":
file_name = sys.argv[1]
file_extension = sys.argv[2]
number_of_object = 3
# if number of object is not specified, use default value 3
try:
number_of_object = int(sys.argv[3])
except:
print("Number of object is not specified, using default value 3")
eval(file_extension + "Demo")(file_name, file_extension, number_of_object)