def dump obj if isinstance obj Object res res append obj __class__ __n

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def dump(obj):
if isinstance(obj, Object):
res = []
res.append(('_', obj.__class__.__name__))
for key in getattr(obj, '__slots__', []):
val = getattr(obj, key)
if val is None:
pass
elif key.endswith('date'):
res.append((key, datetime.fromtimestamp(val)))
else:
res.append((key, dump(val)))
return OrderedDict(res)
elif isinstance(obj, dict):
res = []
for key, val in obj.items():
res.append((key, dump(obj)))
return OrderedDict(res)
elif isinstance(obj, (list, tuple)):
return [dump(x) for x in obj]
elif isinstance(obj, (int, float, str)):
return obj
elif obj is None:
return None