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