-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.py
68 lines (57 loc) · 2.18 KB
/
record.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
import pandas as pd
import pickle
import datetime
import os
"""
SEE THE SPECIFICATION OF THE FIELDS IN THE WEB API DOCS
SPECIFIC FIELDS TBD
"""
class Record:
def __init__(self, **kwargs):
self.dict_temp = kwargs
minimum_list = ["userId", "quantity", "companyAddress", "price",
"categoryName", "auctionDescription", "usedOrNew", "vatInvoice",
"vatInvoiceMargin"]
for required_entry in minimum_list:
if not required_entry in kwargs.keys():
print("LACKING KEY {}".format(required_entry))
raise TypeError
for k, v in kwargs.items():
setattr(self, k, v)
def __str__(self):
return str(self.__dict__['dict_temp'])
def parseDataFrame(self):
#parses into dataframe
new_records = pd.DataFrame()
return new_records.from_records(self.__dict__['dict_temp'])
@staticmethod
def pickle_read(path):
with open(path, 'rb') as fs:
try:
while True:
return pickle.load(fs)
except EOFError:
pass
@staticmethod
def read_pickle_folder(directory):
obj_array = []
for filename in os.listdir(directory):
obj = Record.pickle_read(os.path.join(directory, filename))
obj_array.append(obj)
return obj_array
def pickle_dump(self, worker_id=0):
print("SAVING")
now = datetime.datetime.now()
date_str = "./pickle_dumps/pickle_" + str(now.year) + "_" + str(now.day) + \
"_" + str(now.hour) + "_" + str(now.minute) \
+ "_" + str(now.second) + "_worker_id_" + str(worker_id) + ".pickle"
with open(date_str, 'wb') as f:
pickle.dump(self, f)
if __name__ == "__main__":
kwargs = {"userId":[1,2,3], "quantity":[1,2,3], "companyAddress":[1,2,3], "price":[1,2,3],
"categoryName":[1,2,3], "auctionDescription":[1,2,3], "usedOrNew":[1,2,3], "vatInvoice": [1,2,3],
"vatInvoiceMargin": ["name", "name2", "name2"]}
new_record = Record(**kwargs)
new_record.pickle_dump()
directory = "./pickle_dumps"
print(Record.read_pickle_folder(directory)[0])