GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — StarterLearningPython/2code/22701.py at master · IIKovalenko/StarterLearningPython Expand file tree/ Copy path22701.pyMore file actions 46 lines (37 loc) · 1.13 KB / Copy path22701.pyFile metadata and controls46 lines (37 loc) · 1.13 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #!/usr/bin/env python # coding=utf-8 import json class Person(object): def __init__(self,name,age): self.name = name self.age = age def __repr__(self): return 'Person Object name : %s , age : %d' % (self.name,self.age) def object2dict(obj): #convert Person to dict d = {} d['__class__'] = obj.__class__.__name__ d['__module__'] = obj.__module__ d.update(obj.__dict__) return d def dict2object(d): #convert dict ot Person if '__class__' in d: class_name = d.pop('__class__') module_name = d.pop('__module__') module = __import__(module_name) class_ = getattr(module, class_name) args = dict((key.encode('ascii'), value) for key,value in d.items()) #get args inst = class_(**args) #create new instance else: inst = d return inst if __name__ == '__main__': p = Person('Peter',40) print p d = object2dict(p) print d o = dict2object(d) print type(o), o dump = json.dumps(p, default=object2dict) print dump load = json.loads(dump, object_hook=dict2object) print load |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |