灯光方案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 flatty

This 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 = None

The 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
42

The restored_obj is a new object filled with the data of flatted

窃听器

如果您发现任何问题,请在https://github.com/ceelian/Flatty/issues上报告

变平

您可以在Python Package Index上获得python包

git存储库位于github.com Flatty

安装

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包的根文件夹中。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
将中心面板添加到“我的内容”窗格时,java GUI对象消失   java无法更新oracle中的clob字段   php Linux+动态插入的Java小程序=在NPObject上调用方法时出错   java JVisualVM探查器代理jar已加载,但代理初始化失败   java Android在运算符和TextView值方面存在问题   用于OpenPGP智能卡小程序的rsa解密APDU   Java GUI按钮不会添加到面板中   java找不到移动图像(或其他可单击对象)的方法   java中Do/While循环的问题   多线程使java程序在没有线程的情况下休眠   java如何在Webflux功能端点的测试中禁用Spring安全性   如果存在后退历史记录,则java WebView仅显示后退按钮   通过USB将Arduino中显示的java错误数据传输到Android   java如何使用安卓 studio从4层父节点firebase获取子节点数据   jpanel中JLabel的java搜索栏   来自gallery/camera的java Android图像预览不同