灯光方案python对象的扁平编组/解编
flatt的Python项目详细描述
导言
^ tt1}$是一种用于轻松创建灵活的Python类模式的微框架。 通常,您将在使用python dict作为 输入/输出格式。考虑一下couchdb、json、xml、yaml等等。 只需存储 数据(不存储元数据)
主要功能:
- easy to use
- couchdb adapter to use flatty schemas with couchdb
- only plain data is marshaled, no class meta-data
- extensible, add custom converters for your own needs and types
- can be easily extended to support unique features of a marshal framework
- light-weight (flatty has currently less than 200 lines of code)
- OpenSource BSD-licensed
完整文档可以在PyPi Flatty Documentation上找到
公寓背后的想法
flatty的目标是为dict marshaller提供一个类,该类保持在 其他低级编组员的背景。他们可能只支持python 听写和一些基本类型。
使用Flatty,您可以构建一个完整的类模式 marshall/unmarshall(扁平化/非扁平化)高级类对象到低级 提供持久层的marshaller。 Flatty已经提供了一个很好的例子 适配器是couchdb。我们尽量保留模式定义 “标准python”并通过检查收集所需信息以保持 事情很简单
Flatty将所有内容简化为一个简单的dict,而不将元信息存储在 编组的数据Flatty使用的编组过程很简单: 它将类视为dict,将它们的属性视为dict中的键值对。 列表存储为列表。就这样。
公寓入门
Let’s go:
>>> import flattyThis imports the flatty module. Now we can define a schema using python classes:
>>> class Bar(flatty.Schema): ... a_num = int ... a_str = str ... a_thing = NoneThe class Bar`has 3 attributes. `a_num is typed as int, a_str as string and a_thing can be of any type. Types are checked during flattening and unflattening and couse a TypeError Exception if type does not fit.
>>> class Foo(flatty.Schema): ... my_typed_list = flatty.TypedList.set_type(Bar)The class Foo defines just one attribute my_typed_list. As the name might already explain, the type of this attribute is a list. It acts like a normal python list (actually it is inherited from list) with one difference it only accepts items instances of type Bar.
Note
The benefit of this “strict” typing with TypedList is that Flatty knows which types you expect and can create instances of class Bar during unflattening. Because flatty doesn’t marshal type information it needs this information during unmarshaling to restore the correct types
You can also use just a normal python list but when you unflat your data you will just get “classless” items instead of Bar instances.
There is also a TypedDict to produce “strict” typed dicts
Next we create some instances. You see we can use named arguments in the constructor to fill the attributes.
>>> my_bar = Bar(a_num=42, a_str='hello world', a_thing='whatever type here') >>> foo = Foo(my_typed_list=[my_bar,])No we have my_bar`added to the list of `foo.
Note
Above you can see that we use a python list (not TypedList) ^{tt2}$ to create the foo instance with the my_typed_list attribute.
Flatty, flat it!
>>> flatted = foo.flatit() >>> print flatted {'my_typed_list': [{'a_num': 42, 'a_str': 'hello world', 'a_thing': 'whatever type here'}]}Voila, this is the flattened dictionary you get.
Per default just instances of type Schema, datetime, date, time will be flattened. But if - for example - your marshaller don’t understand integers just strings you can easily add a Converter for type int (see reference).
The flatted can now be stored using your favorite low-level marshaller (couchdb, json, yaml, xml, etc).
Next we see how we can restore objects only using the flatted data and the schema.
>>> restored_obj = Foo.unflatit(flatted) >>> isinstance(restored_obj, Foo) True >>> isinstance(restored_obj.my_typed_list[0], Bar) True >>> restored_obj.my_typed_list[0].a_num 42The restored_obj is a new object filled with the data of flatted
窃听器
如果您发现任何问题,请在https://github.com/ceelian/Flatty/issues上报告
安装
Flatty可以通过源代码的python包索引安装。
使用easy_install安装Flatty:
$ easy_install Flatty
如果你下载了一个源tarball,你可以安装它 通过执行以下操作:
$ python setup.py build $ python setup.py install
由
支持Wingware-PythonIDE(http://wingware.com)
贡献
我们欢迎所有想为公寓做贡献的人。 扁率的发展发生在https://github.com/ceelian/Flatty
许可证
Flatty是根据BSD许可证发布的。 完整的许可证文本位于flatty包的根文件夹中。