Passed
Push — master ( 269add...33a345 )
by Dave
01:22
created

backend.fcmutils.deserializelistofstrings()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 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
37
def serializelist(self, listofentities):
38
    '''serialize a list of entities'''
39
    json_list = json.dumps([e.__dict__ for e in listofentities])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable json does not seem to be defined.
Loading history...
40
    return json_list
41
42
def deserializelistofstrings(the_list, sch):
43
    '''deserialize list of strings into list of miners'''
44
    results = []
45
    for item in the_list:
46
        miner = deserialize(sch, safestring(item))
47
        results.append(miner)
48
    return results
49