一个强大的,动态的,pythonic接口到aws dynamodb。

duo的Python项目详细描述


https://travis-ci.org/eykd/duo.svg?branch=masterhttps://coveralls.io/repos/github/eykd/duo/badge.svg?branch=master

duo为工作提供了一些简单的python抽象 使用amazon web服务的dynamodb。它的包装很薄 boto.dynamodb.layer2,所以您可以完全访问 图书馆在你需要的时候,但你不必为细节操心 你没有。

严厉警告:

不,说真的,这是一个很轻的包装 boto.dynamodb.layer2。如果你超出了使用示例的范围 下面,您最好熟悉boto的dynamodb api。The docs很好。阅读duo’s source也可能有帮助。是的 因为这个原因而保持简短。

用法:

duo由一个模块组成:

>>> import duo

模块不是很大(在撰写本文时,大约700 行)。如果你想知道一些东西是如何工作的,you should read it

在aws控制台中预先创建表,然后编写简单的类 去接近他们。duo.Table自动注册子类 使用数据库:

>>> class MyHashKeyTable(duo.Table):
...     table_name = 'my_hashkey_table'
...     hash_key_name = 'slug'
...     range_key_name = None  # Implicit default

duo.Item是围绕boto.dynamodb.items.Item的薄包装,具有 大量的语法糖。duo.Item子类是自动的 在数据库中注册:

>>> import datetime

>>> class MyHashKeyItem(duo.Item):
...     table_name = 'my_hashkey_table'
...     hash_key_name = 'slug'
...
...     slug = duo.UnicodeField()
...     my_field = duo.UnicodeField(default='foo')
...     on_this_date = duo.DateField(default=lambda o: datetime.date.today())

数据库和表使用类似dict的访问语法:

>>> db = duo.DynamoDB(key='access_key', secret='secret_key')

>>> # The correct Table sub-class is matched by table name:
>>> table = duo.DynamoDB['my_hashkey_table']

>>> # The correct Item sub-class is matched by table name:
>>> item = table['new-item']

>>> # Items are actually dict subclasses, but that's not where the
>>> # fun is. They can only store unicode strings and integers:
>>> item['slug']
'new-item'

指定项子类上的字段以获取有用的数据类型:

>>> item.is_new
True

>>> # A field doesn't exist initially...
>>> item['my_field']
Traceback (most recent call last):
  File "...", line 1, in <module>
    item['my_field']
KeyError: 'my_field'

>>> # But we specified a default.
>>> item.my_field
'foo'

>>> # The default, once accessed, gets populated:
>>> item['my_field']
'foo'

>>> # Or we can set our own value...
>>> item.my_field = 'bar'

>>> item['my_field']
'bar'

>>> # Finally, we save it to DynamoDB.
>>> item.put()

>>> item.is_new
False

缓存:

duo与实现python-memcached兼容接口的任何缓存集成,即:

import pylibmc
cache = pylibmc.Client(['127.0.0.1'])
cache.get(<keyname>)
cache.set(<keyname>, <duration-in-seconds>)
cache.delete(<keyname>)

通过将缓存传递给db构造函数来集成缓存:

>>> import duo
>>> db = duo.DynamoDB(key='access_key', secret='secret_key', cache=cache)

还可以按表或按项指定缓存对象:

>>> class MyHashKeyTable(duo.Table):
 ...     cache = pylibmc.Client(['127.0.0.1'])
 ...
 ...     table_name = 'my_hashkey_table'
 ...     hash_key_name = 'slug'
 ...     range_key_name = None  # Implicit default

默认情况下,缓存处于关闭状态,但可以通过指定 acache_duration作为整数(0是永远的):

>>> class MyHashKeyItem(duo.Item):
...     cache_duration = 30  # 30 seconds
...
...     table_name = 'my_hashkey_table'
...     hash_key_name = 'slug'
...
...     slug = duo.UnicodeField()
...     my_field = duo.UnicodeField(default='foo')
...     on_this_date = duo.DateField(default=lambda o: datetime.date.today())

缓存键由哈希键、范围键和缓存前缀确定 (放在桌子上)。默认情况下,缓存前缀是表名:

>>> table = duo.DynamoDB['my_hashkey_table']
>>> item = table['new-item']
>>> item.cache_prefix is None
True
>>>item._cache_key
'my_hashkey_table_new-item'
>>> MyHashKeyTable.cache_prefix = 'hello_world'
>>> item._get_cache_key()
'hello_world_new-item'

更改日志

0.3.1

修正了在Python3中EnumMeta和子类没有正确比较(re:at-all)的错误。

0.3.0

添加Python3兼容性。

0.2.5

get_item()现在写入缓存,即使它不从缓存中读取。

0.2.4

向表中添加了自定义get_项,用于指定一致读取, 等,由getitem使用,以获得更简单的代码!

0.2.3

再做一次包装修复,这样pip就不会爆炸了。谢谢,卡宾克!

0.2.2

table.scan()和.query()应该返回扩展项。

0.2.1

setup.py的更正/改进。包装很难。

0.2

首次公开发行。

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

推荐PyPI第三方库


热门话题
java使用EntityManager有没有更有效的习惯用法?   Android上的java Google应用程序引擎(GAE)响应代码和cookie   如何在Java中创建单元测试?   java从DB获取特定列的最新行   java替换所有悬空元字符   java使用Hibernate删除SQL表中的数据   swing显示JComponent对象Java   java在确认内容类型后如何将URL保存到文件?   javascript如何从段落中选择大量单词?(硒)   java在Linux上使用BundleEnableTiveCode不起作用   java使用日志似然性来比较不同的mallet主题模型?   java无法在Tomcat7上运行Spring Boot 2.0:“由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext。”   java有办法显式引用非静态内部类实例吗?   java如何使用Spring的NamedParameterJdbcTemplate在MySQL数据库中创建和删除表?