##// END OF EJS Templates
schain serializer: minor changes
Miguel Valdez -
r705:2b21a763c9a1
parent child
Show More
@@ -1,115 +1,114
1 '''
1 '''
2 Created on Jul 15, 2014
2 Created on Jul 15, 2014
3
3
4 @author: Miguel Urco
4 @author: Miguel Urco
5 '''
5 '''
6 from serializer import DynamicSerializer
6 from serializer import DynamicSerializer
7
7
8 DEFAULT_SERIALIZER = None #'cPickle', 'msgpack', "yaml"
8 DEFAULT_SERIALIZER = None #'cPickle', 'msgpack', "yaml"
9
9
10 from schainpy.model.data.jrodata import *
10 from schainpy.model.data.jrodata import *
11
11
12 CLASSNAME_KEY = 'classname__'
12 CLASSNAME_KEY = 'classname__'
13
13
14 def isNotClassVar(myObj):
14 def isObject(myObj):
15
15
16 return not hasattr(myObj,'__dict__')
16 return hasattr(myObj,'__dict__')
17
17
18 def isDictFormat(thisValue):
18 def isDictFormat(thisValue):
19
19
20 if type(thisValue) != type({}):
20 if type(thisValue) != type({}):
21 return False
21 return False
22
22
23 if CLASSNAME_KEY not in thisValue.keys():
23 if CLASSNAME_KEY not in thisValue.keys():
24 return False
24 return False
25
25
26 return True
26 return True
27
27
28 def obj2Dict(myObj, keyList=[]):
28 def obj2Dict(myObj, keyList=[]):
29
29
30 if not keyList:
30 if not keyList:
31 keyList = myObj.__dict__.keys()
31 keyList = myObj.__dict__.keys()
32
32
33 myDict = {}
33 myDict = {}
34
34
35 myDict[CLASSNAME_KEY] = myObj.__class__.__name__
35 myDict[CLASSNAME_KEY] = myObj.__class__.__name__
36
36
37 for thisKey, thisValue in myObj.__dict__.items():
37 for thisKey, thisValue in myObj.__dict__.items():
38
38
39 if thisKey not in keyList:
39 if thisKey not in keyList:
40 continue
40 continue
41
41
42 if isNotClassVar(thisValue):
42 if not isObject(thisValue):
43 myDict[thisKey] = thisValue
43 myDict[thisKey] = thisValue
44 continue
44 continue
45
45
46 ## If this value is another class instance
46 ## If this value is another class instance
47 myNewDict = obj2Dict(thisValue)
47 myDict[thisKey] = obj2Dict(thisValue)
48 myDict[thisKey] = myNewDict
49
48
50 return myDict
49 return myDict
51
50
52 def dict2Obj(myDict):
51 def dict2Obj(myDict):
53 '''
52 '''
54 '''
53 '''
55
54
56 if CLASSNAME_KEY not in myDict.keys():
55 if CLASSNAME_KEY not in myDict.keys():
57 return None
56 return None
58
57
59 className = eval(myDict[CLASSNAME_KEY])
58 className = eval(myDict[CLASSNAME_KEY])
60
59
61 myObj = className()
60 myObj = className()
62
61
63 for thisKey, thisValue in myDict.items():
62 for thisKey, thisValue in myDict.items():
64
63
65 if thisKey == CLASSNAME_KEY:
64 if thisKey == CLASSNAME_KEY:
66 continue
65 continue
67
66
68 if not isDictFormat(thisValue):
67 if not isDictFormat(thisValue):
69 setattr(myObj, thisKey, thisValue)
68 setattr(myObj, thisKey, thisValue)
70 continue
69 continue
71
70
72 myNewObj = dict2Obj(thisValue)
71 myNewObj = dict2Obj(thisValue)
73 setattr(myObj, thisKey, myNewObj)
72 setattr(myObj, thisKey, myNewObj)
74
73
75 return myObj
74 return myObj
76
75
77 def dict2Serial(myDict, serializer=DEFAULT_SERIALIZER):
76 def dict2Serial(myDict, serializer=DEFAULT_SERIALIZER):
78
77
79 SERIALIZER = DynamicSerializer(serializer)
78 SERIALIZER = DynamicSerializer(serializer)
80
79
81 mySerial = SERIALIZER.dumps(myDict)
80 mySerial = SERIALIZER.dumps(myDict)
82
81
83 return mySerial
82 return mySerial
84
83
85 def serial2Dict(mySerial, serializer=DEFAULT_SERIALIZER):
84 def serial2Dict(mySerial, serializer=DEFAULT_SERIALIZER):
86
85
87 SERIALIZER = DynamicSerializer(serializer)
86 SERIALIZER = DynamicSerializer(serializer)
88
87
89 myDict = SERIALIZER.loads(mySerial)
88 myDict = SERIALIZER.loads(mySerial)
90
89
91 return myDict
90 return myDict
92
91
93 def obj2Serial(myObj, serializer=DEFAULT_SERIALIZER, **kwargs):
92 def obj2Serial(myObj, serializer=DEFAULT_SERIALIZER, **kwargs):
94
93
95 SERIALIZER = DynamicSerializer(serializer)
94 SERIALIZER = DynamicSerializer(serializer)
96
95
97 myDict = obj2Dict(myObj, **kwargs)
96 myDict = obj2Dict(myObj, **kwargs)
98 mySerial = dict2Serial(myDict, serializer)
97 mySerial = dict2Serial(myDict, serializer)
99
98
100 return mySerial
99 return mySerial
101
100
102 def serial2Obj(mySerial, metadataDict = {}, serializer=DEFAULT_SERIALIZER):
101 def serial2Obj(mySerial, metadataDict = {}, serializer=DEFAULT_SERIALIZER):
103
102
104 SERIALIZER = DynamicSerializer(serializer)
103 SERIALIZER = DynamicSerializer(serializer)
105
104
106 myDataDict = serial2Dict(mySerial, serializer)
105 myDataDict = serial2Dict(mySerial, serializer)
107
106
108 if not metadataDict:
107 if not metadataDict:
109 myObj = dict2Obj(myDataDict)
108 myObj = dict2Obj(myDataDict)
110 return myObj
109 return myObj
111
110
112 metadataDict.update(myDataDict)
111 metadataDict.update(myDataDict)
113 myObj = dict2Obj(metadataDict)
112 myObj = dict2Obj(metadataDict)
114
113
115 return myObj
114 return myObj
General Comments 0
You need to be logged in to leave comments. Login now