资源简介

代码片段和文件信息
‘‘‘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战斗模拟器
相关资源
- ReportMachine 交叉报表 学生成绩表
- reportmachine帮助电子书
- ppt 机器学习.ppt
- Logistic回归总结非常好的机器学习总结
- Convex Analysis and Optimization (Bertsekas
- 基于蚁群算法优化SVM的瓦斯涌出量预
- 基于模糊聚类和SVM的瓦斯涌出量预测
- 机器学习个人笔记完整版v5.2-A4打印版
- 基于CAN总线与ZigBee的瓦斯实时监测及
- JUNIOR:粒子物理学中无监督机器学习
- 语料库.zip
- SVM-Light资料,使用说明
- 果蝇算法融合SVM的开采沉陷预测模型
- Bishop - Pattern Recognition And Machine Learn
- [en]深度学习[Deep Learning: Adaptive Compu
- 中国科学技术大学 研究生课程 机器学
- 遗传算法越野小车unity5.5
- BoW|Pyramid BoW+SVM进行图像分类
- 吴恩达机器学习编程题
- shape_predictor_68_face_landmarks.dat.bz2 68个标
- 基于libsvm的图像分割代码
- 机器学习实战高清pdf,中文版+英文版
- 李宏毅-机器学习(视频2017完整)
- 机器学习深度学习 PPT
- 麻省理工:深度学习介绍PPT-1
- 基于SVM及两类运动想象的多通道特征
- 小波包和SVM在轴承故障识别中的应用
- 林智仁教授最新版本LibSVM工具箱
- Wikipedia机器学习迷你电子书之四《D
- 台湾林教授的支持向量机libsvm
评论
共有 条评论