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

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


Latest PyPI VersionPython Version Support

安装

$ pip install --upgrade gcloud-rest-datastore

用法

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

fromgcloud.rest.datastoreimportDatastorefromgcloud.rest.datastoreimportDirectionfromgcloud.rest.datastoreimportFilterfromgcloud.rest.datastoreimportGQLQueryfromgcloud.rest.datastoreimportKeyfromgcloud.rest.datastoreimportPathElementfromgcloud.rest.datastoreimportPropertyFilterfromgcloud.rest.datastoreimportPropertyFilterOperatorfromgcloud.rest.datastoreimportPropertyOrderfromgcloud.rest.datastoreimportQueryfromgcloud.rest.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=ds.lookup([key1,key2])# convenience functions for any datastore mutationsds.insert(key1,{'a_boolean':True,'meaning_of_life':41})ds.update(key1,{'a_boolean':True,'meaning_of_life':42})ds.upsert(key1,{'animal':'aardvark'})ds.delete(key1)# or build your own mutation sequences with full transaction supporttransaction=ds.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'}),]ds.commit(transaction,mutations=[mutation])exceptException:ds.rollback(transaction)# support for partial keyspartial_key=Key('my-gcloud-project',[PathElement('Kind')])# and ID allocation or reservationallocated_keys=ds.allocateIds([partial_key])ds.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=ds.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=ds.runQuery(gql_query,session=s)

自定义子类

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

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

classMyEntityKind(gcloud.rest.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-rest-datastore配置为从 此自定义实体类具有:

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

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

classMyVeryCustomDatastore(gcloud.rest.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.rest.datastore.Query):value_kind=ValueclassMyVeryCustomGQLQuery(gcloud.rest.datastore.GQLQuery):value_kind=Value

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

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

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

贡献

请看我们的contributing guide

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

推荐PyPI第三方库


热门话题
java无法使用Intellij Idea执行Xpath   java为什么springbootstarterdatajpa 2.5.0不能因“未找到表”而初始化数据?   google maps Geoppoint类中的java丢失精度   java fb graph对象中有哪些属性可用?   实现JNI时指针类型的java错误   java使用管理目录API将一个组织单元移动到另一个组织单元?   java局部变量gcd可能尚未初始化   java示例代码未按预期执行   使用持久性的JPA的数据库连接位于何处。xml?   java从AJAX成功函数中检索ArrayList元素   java中的持久文件验证   java编码简约消息的最佳方式   tapestry用java生成站点地图并使其公开   Java文档中使用的哈希集约定   java试图在工作线程上调用join   java有没有时间函数来记录一个方法完成一项工作所需的时间?   如何让计算器在按下等号后接受新数字?JAVA