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