1
|
|
|
#import time |
2
|
|
|
|
3
|
|
|
def safestring(thestring): |
4
|
|
|
'''safely convert anything into string''' |
5
|
|
|
if thestring is None: |
6
|
|
|
return None |
7
|
|
|
if isinstance(thestring, str): return thestring |
8
|
|
|
return str(thestring, "utf-8") |
9
|
|
|
|
10
|
|
|
def formattime(the_time): |
11
|
|
|
'''standard format for time''' |
12
|
|
|
return the_time.strftime('%Y-%m-%d %H:%M:%S') |
13
|
|
|
|
14
|
|
|
def jsonserialize(sch, msg): |
15
|
|
|
'''serialize a message with schema. returns string''' |
16
|
|
|
smessage = sch.dumps(msg) |
17
|
|
|
#json.dumps(jmessage) |
18
|
|
|
return smessage.data |
19
|
|
|
|
20
|
|
|
def deserialize(sch, msg): |
21
|
|
|
'''Output should be entity, not python json object |
22
|
|
|
msg parameter should be string |
23
|
|
|
''' |
24
|
|
|
if msg is None: return None |
25
|
|
|
return sch.loads(msg).data |
26
|
|
|
|
27
|
|
|
def deserializelist_withschema(schema, the_list): |
28
|
|
|
'''deserialize list of strings into entities''' |
29
|
|
|
results = [] |
30
|
|
|
for item in the_list: |
31
|
|
|
entity = deserialize(schema, safestring(item)) |
32
|
|
|
#todo:for pools the entry is a list |
33
|
|
|
results.append(entity) |
34
|
|
|
return results |
35
|
|
|
|
36
|
|
|
def serialize(entity, schema): |
37
|
|
|
return schema.dumps(entity).data |
38
|
|
|
|
39
|
|
|
#def serializelist(listofentities): |
40
|
|
|
# '''serialize a list of entities''' |
41
|
|
|
# json_list = json.dumps([e.__dict__ for e in listofentities]) |
42
|
|
|
# return json_list |
43
|
|
|
|
44
|
|
|
def deserializelistofstrings(the_list, sch): |
45
|
|
|
'''deserialize list of strings into list of miners''' |
46
|
|
|
results = [] |
47
|
|
|
for item in the_list: |
48
|
|
|
miner = deserialize(sch, safestring(item)) |
49
|
|
|
results.append(miner) |
50
|
|
|
return results |
51
|
|
|
|