资源简介

代码片段和文件信息
‘‘‘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))
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 795 2012-01-09 21:40 decisiontree\lenses.txt
文件 2789 2014-12-01 12:11 decisiontree\ob
文件 1317 2014-12-12 14:57 decisiontree\testDS.py
文件 4239 2014-11-12 13:56 decisiontree\treePlotter.py
文件 9529 2014-12-12 14:17 decisiontree\trees.py
文件 8655 2014-12-12 14:22 decisiontree\trees.pyc
目录 0 2014-12-12 15:17 decisiontree
----------- --------- ---------- ----- ----
27324 7
- 上一篇:双三次插值算法
- 下一篇:数据结构课程设计-校园导游
相关资源
- Pythonamp;课堂amp;笔记(高淇amp;400;集第
- Python中Numpy库最新教程
- 用python编写的移动彩信的发送程序
- Python全栈学习笔记面向对象大作业:
- python实现的ftp自动上传、下载脚本
- Python版的A*寻路算法
- IronPython IDE
- ppt 机器学习.ppt
- Logistic回归总结非常好的机器学习总结
- Convex Analysis and Optimization (Bertsekas
- 机器学习个人笔记完整版v5.2-A4打印版
- JUNIOR:粒子物理学中无监督机器学习
- 语料库.zip
- pip-10.0.1.tar.gz
- Data Science from Scratch 2nd Edition
- 中国科学技术大学 研究生课程 机器学
- 遗传算法越野小车unity5.5
- 吴恩达机器学习编程题
- shape_predictor_68_face_landmarks.dat.bz2 68个标
- 机器学习实战高清pdf,中文版+英文版
- 李宏毅-机器学习(视频2017完整)
- 爬取豆瓣电影TOP250程序,包含非常详
- 机器学习深度学习 PPT
- 麻省理工:深度学习介绍PPT-1
- Wikipedia机器学习迷你电子书之四《D
- Learning From Data Yaser S. Abu-Mostafa
- 中文维基百科语料库百度网盘网址.
- 基于决策树和朴素贝叶斯算法对Adul
- MSCNN_dehaze.rar
- 爬取豆瓣排行榜电影数据(含GUI界面
评论
共有 条评论