ruby的openstruct for python
pydapt的Python项目详细描述
Pydapt
pydapt将字典转换为对象。 它类似于红宝石的OpenStruct。
安装
Python Version Required >= 3
pip install pydapt
用法
将字典转换为对象
frompydapt.modelsimportPyFlexdictionary={"test":1,"test1":{"test2":2}}pyflex=PyFlex(dictionary)print(pyflex.test)# 1print(pyflex.test1.test2)# 2
将字典转换为带kwargs
的对象frompydapt.modelsimportPyFlexdictionary={"test":1,"test1":{"test2":2}}pyflex=PyFlex(dictionary,test3=3,test4=4)print(pyflex.test)# 1print(pyflex.test1.test2)# 2print(pyflex.test3)# 3print(pyflex.test4)# 4
删除属性
frompydapt.modelsimportPyFlexdictionary={"test":1,"test1":{"test2":2}}pyflex=PyFlex(dictionary)print(pyflex.test)# 1print(pyflex.test1.test2)# 2pyflex.drop('test3')# Nonepyflex.drop('test')# 1print(pyflex.test)# AttributeError: 'PyFlex' object has no attribute 'test'