backend.fcmutils   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 12

7 Functions

Rating   Name   Duplication   Size   Complexity  
A formattime() 0 3 1
A safestring() 0 6 3
A deserializelist_withschema() 0 8 2
A serialize() 0 2 1
A deserialize() 0 6 2
A jsonserialize() 0 5 1
A deserializelistofstrings() 0 7 2
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