DynamORM 是一个针对亚马逊DynamoDB服务的Python对象与关系映射库。
项目描述
DynamORM
此包是一个进行中的作品 - 欢迎反馈/建议等!
Python + DynamoDB ♡
DynamORM (发音为 Dynamo-R-M) 是一个针对亚马逊 DynamoDB 服务的Python对象与关系映射库。
项目有两个目标
抽象出与底层DynamoDB库的交互。Python对DynamoDB服务的访问已经迅速发展,从
到 ,再到 。通过提供一个用户熟悉的、与其他Python ORM (SQLAlchemy, Django, Peewee等) 用户类似的统一接口,我们可以始终提供最佳实践查询并利用新功能,而无需对任何应用程序逻辑进行重构。 将模式验证和序列化委托给更专注的库。构建“ORM”语义是“简单”的,而数据验证和序列化则不是。我们支持Marshmallow和Schematics来构建您的对象模式。您可以利用这些库的完整功能,因为它们在代码中以透明的方式暴露。
支持的Schema验证库
Schematics >= 2.1.0
Marshmallow >= 2.15.1
示例
import datetime
from dynamorm import DynaModel, GlobalIndex, ProjectAll
# In this example we'll use Marshmallow, but you can also use Schematics too!
# You can see that you have to import the schema library yourself, it is not abstracted at all
from marshmallow import fields
# Our objects are defined as DynaModel classes
class Book(DynaModel):
# Define our DynamoDB properties
class Table:
name = 'prod-books'
hash_key = 'isbn'
read = 25
write = 5
class ByAuthor(GlobalIndex):
name = 'by-author'
hash_key = 'author'
read = 25
write = 5
projection = ProjectAll()
# Define our data schema, each property here will become a property on instances of the Book class
class Schema:
isbn = fields.String(validate=validate_isbn)
title = fields.String()
author = fields.String()
publisher = fields.String()
# NOTE: Marshmallow uses the `missing` keyword during deserialization, which occurs when we save
# an object to Dynamo and the attr has no value, versus the `default` keyword, which is used when
# we load a document from Dynamo and the value doesn't exist or is null.
year = fields.Number(missing=lambda: datetime.datetime.utcnow().year)
# Store new documents directly from dictionaries
Book.put({
"isbn": "12345678910",
"title": "Foo",
"author": "Mr. Bar",
"publisher": "Publishorama"
})
# Work with the classes as objects. You can pass attributes from the schema to the constructor
foo = Book(isbn="12345678910", title="Foo", author="Mr. Bar",
publisher="Publishorama")
foo.save()
# Or assign attributes
foo = Book()
foo.isbn = "12345678910"
foo.title = "Foo"
foo.author = "Mr. Bar"
foo.publisher = "Publishorama"
# In all cases they go through Schema validation, calls to .put or .save can result in ValidationError
foo.save()
# You can then fetch, query and scan your tables.
# Get on the hash key, and/or range key
book = Book.get(isbn="12345678910")
# Update items, with conditions
# Here our condition ensures we don't have a race condition where someone else updates the title first
book.update(title='Corrected Foo', conditions=(title=book.title,))
# Query based on the keys
Book.query(isbn__begins_with="12345")
# Scan based on attributes
Book.scan(author="Mr. Bar")
Book.scan(author__ne="Mr. Bar")
# Query based on indexes
Book.ByAuthor.query(author="Mr. Bar")
文档
完整文档是从每个构建的源代码构建的,可以在以下网址找到:
https://nerdwalletoss.github.io/dynamorm/
“tests/”中也包含了如何实际使用此库的最完整文档,因此我们鼓励您阅读这些文档,以便真正熟悉一些更高级的概念和用例。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源代码分发
dynamorm-0.11.0.tar.gz (28.5 kB 查看哈希)
构建分发
dynamorm-0.11.0-py2.py3-none-any.whl (30.9 kB 查看哈希)
关闭
dynamorm-0.11.0.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6ad48124fcdb90f22beac43f45a5446c8282668777591c26b9cb15c9561c81ac |
|
MD5 | a6254393ede05516f5efe1e06a110519 |
|
BLAKE2b-256 | 72fa82ff91343afb8d40a0452d045c31e8d31ce063060625af3ccd746205811d |
关闭
dynamorm-0.11.0-py2.py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 94e1ead390718cff38519c5bafb02d35b077c949544894309fd841bcd097381e |
|
MD5 | 23a080e453fb365b289dda8bc5cc4d10 |
|
BLAKE2b-256 | e925780eced1add0419cfbf73237ba8fce6dc70fc07e984820cd1867fe2b7948 |