智能对象
smartobject的Python项目详细描述
智能对象
SmartObject是一个很容易 使用API命令操作对象属性,存储和同步 对象数据。在
SmartObject旨在快速构建物联网应用程序,但可用于 任何其他需要结合本地和分布式对象的应用程序 通过外部API调用自动存储和更改对象属性 正在验证传入数据。在
映射对象属性(称为属性)可以自动验证, 保存、加载、序列化并与外部服务同步。在
SmartObject看起来像ORM,但它与ORM不同:对象属性可以 储存在不同类型的仓库中,并组合成一个 数据对象。在
SmartObject为文件(JSON、YAML、MessagePack和 CBOR)和数据库:通过SQLAlchemy(可以存储对象)和Redis(can)的RDBMS 仅处理外部属性)。在
属性值将自动处理、验证并与 外部服务(如需要)。在
注意:所有SmartObject方法都是线程安全的(至少应该是:)
示例:
你有一个有心率传感器的小组。每个人的数据 heartbeat存储在本地JSON文件中,heartbeat存储在Redis数据库中。如何 用SmartObject实现这一点?只有几行代码:
importsmartobjectclassPerson(smartobject.SmartObject):def__init__(self,name):self.name=nameself.load_property_map('person.yml')self.apply_property_map()smartobject.config.storage_dir='data'smartobject.define_storage(smartobject.JSONStorage())smartobject.define_storage(smartobject.RedisStorage(),'r1')people=smartobject.SmartObjectFactory(Person)# create objects with factorypeople.create(name='John')people.create(name='Jane')# create object manuallyjack=Person('Jack')# you can set a single proppeople.set_prop('John','sex','male')people.set_prop('Jane','sex','female')# or multiple props with dict# heartbeat value is automatically written to Redisjack.set_prop({'sex':'male','heartbeat':100})# print object info (name and sex only)frompprintimportpprintpprint(people.serialize('Jane',mode='info'))people.save()jack.save()# clear Jack's sexjack.set_prop('sex',None)# load it backjack.load()# add Jack to factorypeople.create(obj=jack)# heartbeat value is automatically read from Redisprint('Heartbeat of Jack is: {}'.format(people.get('Jack').heartbeat))
文件人.yml是Person对象的属性映射。它可以 从外部YAML文件加载或直接指定,如Python dict
上例的地图如下所示:
^{pr2}$很简单,不是吗?你定义一个映射,SmartObject就可以完成任务了!在
安装
pip3 install smartobject
文件
完整的文档可在https://smartobject.readthedocs.io/
- 项目
标签: