Passed
Push — master ( b77d96...7418b1 )
by Dave
01:04
created

backend.fcmutils.serializelist()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 4
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 serialize(entity, schema):
38
    return schema.dumps(entity).data
39
40
#def serializelist(listofentities):
41
#    '''serialize a list of entities'''
42
#    json_list = json.dumps([e.__dict__ for e in listofentities])
43
#    return json_list
44
45
def deserializelistofstrings(the_list, sch):
46
    '''deserialize list of strings into list of miners'''
47
    results = []
48
    for item in the_list:
49
        miner = deserialize(sch, safestring(item))
50
        results.append(miner)
51
    return results
52