testServer.py
176 lines
| 3.4 KiB
| text/x-python
|
PythonLexer
|
r568 | ''' | |
Created on Jul 15, 2014 | |||
@author: roj-idl71 | |||
''' | |||
import sys | |||
import yaml | |||
import numpy | |||
import jsonpickle | |||
# import schainpy.serializer.DynamicSerializer as DynamicSerializer | |||
def isNotClassVar(myObj): | |||
return not hasattr(myObj,'__dict__') | |||
def isDictFormat(thisValue): | |||
if type(thisValue) != type({}): | |||
return False | |||
|
r1167 | if '__name__' not in list(thisValue.keys()): | |
|
r568 | return False | |
return True | |||
def obj2Dict(myObj): | |||
myDict = {} | |||
myDict['__name__'] = myObj.__class__.__name__ | |||
|
r1167 | for thisKey, thisValue in list(myObj.__dict__.items()): | |
|
r568 | ||
if isNotClassVar(thisValue): | |||
myDict[thisKey] = thisValue | |||
continue | |||
## If this value is another class instance | |||
myNewDict = obj2Dict(thisValue) | |||
myDict[thisKey] = myNewDict | |||
return myDict | |||
def dict2Obj(myDict): | |||
''' | |||
''' | |||
|
r1167 | if '__name__' not in list(myDict.keys()): | |
|
r568 | return None | |
className = eval(myDict['__name__']) | |||
myObj = className() | |||
|
r1167 | for thisKey, thisValue in list(myDict.items()): | |
|
r568 | ||
if thisKey == '__name__': | |||
continue | |||
if not isDictFormat(thisValue): | |||
setattr(myObj, thisKey, thisValue) | |||
continue | |||
myNewObj = dict2Obj(thisValue) | |||
setattr(myObj, thisKey, myNewObj) | |||
return myObj | |||
class myTestClass3(object): | |||
def __init__(self): | |||
''' | |||
''' | |||
self.y1 = 'y1' | |||
self.y2 = 'y2' | |||
class myTestClass2(object): | |||
def __init__(self): | |||
''' | |||
''' | |||
self.x1 = 'x1' | |||
self.x2 = 'x2' | |||
self.otherObj = myTestClass3() | |||
class myTestClass(object): | |||
flagNoData = True | |||
value1 = 1 | |||
value2 = 2 | |||
myObj = None | |||
def __init__(self): | |||
''' | |||
''' | |||
self.flagNoData = True | |||
self.value1 = 1 | |||
self.value2 = 2 | |||
self.myObj = myTestClass2() | |||
def get_dtype(self): | |||
''' | |||
''' | |||
return self.value1 | |||
def set_dtype(self, value): | |||
''' | |||
''' | |||
self.value1 = value | |||
dtype = property(get_dtype, set_dtype) | |||
def myMsgPackTest(): | |||
import msgpack | |||
import msgpack_numpy as m | |||
import numpy as np | |||
x = np.random.rand(5) | |||
x_enc = m.encode(x) | |||
x_rec = m.decode(x_enc) | |||
|
r1167 | print(x_rec) | |
|
r568 | # | |
# x_enc = msgpack.packb(x, default=m.encoder) | |||
# x_rec = msgpack.unpackb(x_enc, object_hook=m.decoder) | |||
if __name__ == '__main__': | |||
myMsgPackTest() | |||
sys.exit() | |||
serializerObj = DynamicSerializer.DynamicSerializer('json') | |||
serializerObj = jsonpickle | |||
myTestObj = myTestClass() | |||
myTestObj.flagNoData = False | |||
myTestObj.value1 = [1+3.4j,4,'5',] | |||
myTestObj.value2 = {'x2': numpy.complex(1,2),'x1': 'x1'} | |||
# myTestObj.myObj.x2 = numpy.arange(15, dtype=numpy.complex) | |||
myDict = obj2Dict(myTestObj) | |||
myNewObj = dict2Obj(myDict) | |||
# print myDict | |||
# print myTestObj.__dict__ | |||
# print myNewObj.__dict__ | |||
# sys.exit() | |||
|
r1167 | print(myDict) | |
|
r568 | ||
newSerial = serializerObj.encode(myDict) | |||
# print newSerial | |||
newDict = serializerObj.decode(newSerial) | |||
|
r1167 | print(newDict) | |
|
r568 | ||
myNewObj = dict2Obj(newDict) | |||
|
r1167 | print() | |
print() | |||
print(50*'###') | |||
print(myTestObj.__dict__) | |||
print(myNewObj.__dict__) | |||
|
r568 |