用于google云数据存储的asyncio python客户端

gcloud-aio-datastore的Python项目详细描述


Latest PyPI VersionPython Version Support

安装

$ pip install --upgrade gcloud-aio-datastore

用法

我们仍在进行文档工作;目前,这将有助于您 开始时间:

fromgcloud.aio.datastoreimportDatastorefromgcloud.aio.datastoreimportDirectionfromgcloud.aio.datastoreimportFilterfromgcloud.aio.datastoreimportGQLQueryfromgcloud.aio.datastoreimportKeyfromgcloud.aio.datastoreimportPathElementfromgcloud.aio.datastoreimportPropertyFilterfromgcloud.aio.datastoreimportPropertyFilterOperatorfromgcloud.aio.datastoreimportPropertyOrderfromgcloud.aio.datastoreimportQueryfromgcloud.aio.datastoreimportValueds=Datastore('my-gcloud-project','/path/to/creds.json')key1=Key('my-gcloud-project',[PathElement('Kind','entityname')])key2=Key('my-gcloud-project',[PathElement('Kind','entityname2')])# batched lookupsentities=awaitds.lookup([key1,key2])# convenience functions for any datastore mutationsawaitds.insert(key1,{'a_boolean':True,'meaning_of_life':41})awaitds.update(key1,{'a_boolean':True,'meaning_of_life':42})awaitds.upsert(key1,{'animal':'aardvark'})awaitds.delete(key1)# or build your own mutation sequences with full transaction supporttransaction=awaitds.beginTransaction()try:mutations=[ds.make_mutation(Operation.INSERT,key1,properties={'animal':'sloth'}),ds.make_mutation(Operation.UPSERT,key1,properties={'animal':'aardvark'}),ds.make_mutation(Operation.INSERT,key2,properties={'animal':'aardvark'}),]awaitds.commit(transaction,mutations=[mutation])exceptException:awaitds.rollback(transaction)# support for partial keyspartial_key=Key('my-gcloud-project',[PathElement('Kind')])# and ID allocation or reservationallocated_keys=awaitds.allocateIds([partial_key])awaitds.reserveIds(allocated_keys)# query supportproperty_filter=PropertyFilter(prop='answer',operator=PropertyFilterOperator.EQUAL,value=Value(42))property_order=PropertyOrder(prop='length',direction=Direction.DESCENDING)query=Query(kind='the_meaning_of_life',query_filter=Filter(property_filter),order=property_order)results=awaitds.runQuery(query,session=s)# alternatively, query support using GQLgql_query=GQLQuery('SELECT * FROM the_meaning_of_life WHERE answer = @answer',named_bindings={'answer':42})results=awaitds.runQuery(gql_query,session=s)

自定义子类

gcloud-aio-datastore提供类接口,镜像所有正式的 google api类型,即KeyPathElementEntityEntityResultQueryResultBatchValue。这些类型将是 从任意数据存储操作返回,例如 Datastore.allocateIds(...)将返回Key实体的列表。

对于高级用法,所有这些数据类型都可能被重载。通用用例 可能是将实体反序列化为更具体的类。例如,给定 自定义实体类,如:

classMyEntityKind(gcloud.aio.datastore.Entity):def__init__(self,key,properties=None)->None:self.key=keyself.is_an_aardvark=(propertiesor{}).get('aardvark',False)def__repr__(self):return"I'm an aardvark!"ifself.is_an_aardvarkelse"Sorry, nope"

然后,我们可以将gcloud-aio-datastore配置为从 此自定义实体类具有:

classMyCustomDatastore(gcloud.aio.datastore.Datastore):entity_result_kind.entity_kind=MyEntityKind

可以用这种方式重写的类的完整列表是:

classMyVeryCustomDatastore(gcloud.aio.datastore.Datastore):datastore_operation_kind=DatastoreOperationentity_result_kind=EntityResultentity_result_kind.entity_kind=Entityentity_result_kind.entity_kind.key_kind=Keykey_kind=Keykey_kind.path_element_kind=PathElementquery_result_batch_kind=QueryResultBatchquery_result_batch_kind.entity_result_kind=EntityResultvalue_kind=Valuevalue_kind.key_kind=KeyclassMyVeryCustomQuery(gcloud.aio.datastore.Query):value_kind=ValueclassMyVeryCustomGQLQuery(gcloud.aio.datastore.GQLQuery):value_kind=Value

然后,您可以将MyVeryCustomDatastore类放在 以前使用过Datastore,对QueryGQLQuery执行同样的操作。

要重写任何子键,您需要重写使用它的任何父项。为了 例如,如果要使用自定义密钥类型并能够使用 它需要实现自己的ValueQueryGQLQuery 类并将它们连接到其他自定义类:

classMyKey(gcloud.aio.datastore.Key):passclassMyValue(gcloud.aio.datastore.Value):key_kind=MyKeyclassMyEntity(gcloud.aio.datastore.Entity):key_kind=MyKeyvalue_kind=MyValueclassMyEntityResult(gcloud.aio.datastore.EntityResult):entity_kind=MyEntityclassMyQueryResultBatch(gcloud.aio.datastore.QueryResultBatch):entity_result_kind=MyEntityResultclassMyDatastore(gcloud.aio.datastore.Datastore):key_kind=MyKeyentity_result_kind=MyEntityResultquery_result_batch=MyQueryResultBatchvalue_kind=MyValueclassMyQuery(gcloud.aio.datastore.Query):value_kind=MyValueclassMyGQLQuery(gcloud.aio.datastore.GQLQuery):value_kind=MyValue

贡献

请看我们的contributing guide

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

推荐PyPI第三方库


热门话题
java如何从ImageIO中排除特定的TIFF读取器?   JavaJMockit和passbyreference。我们中一定有一个人错了(可能是我!)   java Android camera2放弃了牛轧糖的表面,但在棉花糖上工作   java按字符串中出现的顺序对字符数组进行排序   如何获取Groovy生成的java源代码   java无法使用AutoIT和Selenium Webdriver在所需位置/文件夹保存图像   java为什么我的冒泡排序代码会打印出这些奇怪的东西?   java JAXB:typesafeEnumMemberName=“generateName”是否可自定义?   Java编程输入:今天是星期天输出:星期天是今天   java不理解首个OOAD书的吉他示例   java如何从JformattedTextfield检索货币格式值   java可以从相同的源代码生成功能不同的可执行文件吗?