資源簡介

代碼片段和文件信息
‘‘‘Created?on?Jan02?2014
dumps?and?load?object?with?JSON
@author:?Aidan
‘‘‘
import?json
import?inspect?
import?pdb
def?object2dict(obj):???
????#convert?object?to?a?dict???
????d?=?{‘__class__‘:obj.__class__.__name__?‘__module__‘:obj.__module__}???
????d.update(obj.__dict__)???
????return?d
def?objectDumps2File(obj?jsonfile):
????objDict?=?object2dict(obj)
????with?open(jsonfile?‘w‘)?as?f:
????????f.write(json.dumps(objDict))
????
def?dict2object(d):???
????‘‘‘convert?dict?to?object?the?dict?will?be?changed‘‘‘????
????if‘__class__‘?in?d:???
????????class_name?=?d.pop(‘__class__‘)???
????????module_name?=?d.pop(‘__module__‘)???
????????module?=?__import__(module_name)???
????????#print?‘the?module?is:‘?module???
????????class_?=?getattr(moduleclass_name)???
????????args?=?dict((key.encode(‘ascii‘)?value)?for?key?value?in?d.items())?#get?args??
????????#print?‘the?atrribute:‘?repr(args)
????????#pdb.set_trace()
????????inst?=?class_(**args)?#create?new?instance???
????else:???
????????inst?=?d???
????return?inst
def?objectLoadFromFile(jsonFile):
????‘‘‘load?json?file?and?generate?a?new?object?instance?whose?__name__?filed
????will?be?‘inst‘?‘‘‘
????with?open(jsonFile)?as?f:
????????objectDict?=json.load(f)
????obj?=?dict2object(objectDict)
????return?obj
#test?function???
if?__name__??==?‘__main__‘:
????class?Person(object):???
????????def?__init__(selfnameage?**args):
????????????obj_list?=?inspect.stack()[1][-2]
????????????self.__name__?=?obj_list[0].split(‘=‘)[0].strip()#object?instance?name
????????????self.name?=?name???
????????????self.age?=?age
????????????
????????def?__repr__(self):???
????????????return?‘Person?object?name?:?%s??age?:?%d‘?%?(self.nameself.age)
????????def?say(self):
????????????#d?=?inspect.stack()[1][-2]
????????????#print?d[0].split(‘.‘)[0].strip()
????????????return?self.__name__
????????def?jsonDumps(self?filename=None):
????????????‘‘‘essential?transformation?to?Python?basic?type?in?order?to
????????????store?as?json.?dumps?as?objectname.json?if?filename?missed?‘‘‘
????????????if?not?filename:
????????????????jsonfile?=?self.__name__+‘.json‘
????????????else:?jsonfile?=?filename
????????????objectDumps2File(self?jsonfile)
????????
????????def?jsonloadTransfer(self):#TBD
????????????‘‘‘essential?transformation?to?object?required?typesuch?as
????????????numpy?matrix.call?this?function?after?newobject?=?objectLoadFromFile(jsonfile)‘‘‘
????????????pass
????p?=?Person(‘Aidan‘22)?????
????#json.dumps(p)#error?will?be?throwed
????
????#objectDumps2File(p‘Person.json‘)
????p.jsonDumps()
????p_l?=?objectLoadFromFile(‘p.json‘)
??????
????print?‘the?decoded?obj?type:?%s?obj:%s‘?%?(type(p_l)repr(p_l))
????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2789??2014-12-01?12:11??svm\ob
?????文件???????9937??2014-12-01?12:09??svm\svm.py
?????文件???????5180??2014-11-25?16:42??svm\SVMClassifier_Rbf_0_05.json
?????文件???????1320??2014-11-25?16:42??svm\SVMClassifier_Rbf_0_5.json
?????文件???????1572??2014-11-25?16:41??svm\SVMClassifier_Rbf_1_3.json
?????文件????????646??2014-11-25?14:54??svm\SVMClassifier_simple.json
?????文件???????2208??2010-11-04?14:13??svm\testSet.txt
?????文件???????2945??2010-11-26?18:16??svm\testSetRBF.txt
?????文件???????2951??2010-11-26?18:17??svm\testSetRBF2.txt
?????文件???????3703??2014-12-01?12:12??svm\testsvm.py
?????目錄??????????0??2014-12-01?12:12??svm
?????文件?????129569??2011-05-04?16:03??svm\digits.zip
?????文件???????3932??2014-12-01?12:12??svm\ob
?????文件???????9123??2014-12-01?12:12??svm\svm.pyc
-----------?---------??----------?-----??----
???????????????175875????????????????????14
- 上一篇:數據庫數據用圖表顯示
- 下一篇:EXCEL戰(zhàn)斗模擬器
評論
共有 條評論