灯光方案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第三方库


热门话题
utf 8在Java中编码法语字符?   Spring安全web应用程序中的javaajax请求   使用AWS Elastic Beanstalk记录java应用程序   java My方法运行了两次,而它们应该只运行一次并映射。get()正在影响所有条目   java基于特定接口的bean通过HttpInvoker导出bean   java xlsx文件在http请求中发送时已损坏/为空(0字节)   java修剪字符串中的所有“空格”   java如何为二维数组生成列和行和方法?   java在使用配置类时使用@PreAuthorize或@Secured with Jersey   java SmartGWT不能与Maven和Jetty Chrome Ubuntu x64一起使用   java在Google App Engine中,对于一个包含许多延迟任务的任务队列,最佳的存储桶大小是多少?   javajaxb:从XML子树解组?   java如何使媒体播放器在按设备后退按钮从片段返回活动时停止   从java进程中跳过批处理中的暂停命令   java Vaadin Designer不是可编辑的自定义组件   java递归打印数组中等于给定和的所有子集   java上传覆盖率。ec文件到sonar Android   java如何在游戏中添加按钮,使两者都可见?