本文整理汇总了Python中google.cloud.datastore.Entity方法的典型用法代码示例。如果您正苦于以下问题:Python datastore.Entity方法的具体用法?Python datastore.Entity怎么用?Python datastore.Entity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.cloud.datastore
的用法示例。
在下文中一共展示了datastore.Entity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: put_user_credentials
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def put_user_credentials(
self, user_name: str, creds: Credentials) -> None:
"""Stores OAuth2 credentials for a user.
Args:
user_name (str): The identifier for the associated user.
creds (Credentials): The OAuth2 credentials obtained for the user.
"""
key = self.datastore_client.key('RefreshToken', user_name)
entity = datastore.Entity(key)
entity.update({
'credentials': {
'token': creds.token,
'refresh_token': creds.refresh_token,
'token_uri': creds.token_uri,
'client_id': creds.client_id,
'client_secret': creds.client_secret,
'scopes': creds.scopes,
},
'timestamp': time.time()
})
self.datastore_client.put(entity)
示例2: test__from_datastore_compressed_to_uncompressed
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def test__from_datastore_compressed_to_uncompressed():
class ThisKind(model.Model):
foo = model.BlobProperty(compressed=False)
key = datastore.Key("ThisKind", 123, project="testing")
datastore_entity = datastore.Entity(key=key)
uncompressed_value = b"abc" * 1000
compressed_value = zlib.compress(uncompressed_value)
datastore_entity.update({"foo": compressed_value})
meanings = {"foo": (model._MEANING_COMPRESSED, compressed_value)}
datastore_entity._meanings = meanings
protobuf = helpers.entity_to_protobuf(datastore_entity)
entity = model._entity_from_protobuf(protobuf)
assert entity.foo == uncompressed_value
ds_entity = model._entity_to_ds_entity(entity)
assert ds_entity["foo"] == uncompressed_value
示例3: test__from_datastore_compressed_repeated_to_compressed
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def test__from_datastore_compressed_repeated_to_compressed():
class ThisKind(model.Model):
foo = model.BlobProperty(compressed=True, repeated=True)
key = datastore.Key("ThisKind", 123, project="testing")
datastore_entity = datastore.Entity(key=key)
uncompressed_value_one = b"abc" * 1000
compressed_value_one = zlib.compress(uncompressed_value_one)
uncompressed_value_two = b"xyz" * 1000
compressed_value_two = zlib.compress(uncompressed_value_two)
datastore_entity.update(
{"foo": [compressed_value_one, compressed_value_two]}
)
meanings = {
"foo": (
model._MEANING_COMPRESSED,
[compressed_value_one, compressed_value_two],
)
}
datastore_entity._meanings = meanings
protobuf = helpers.entity_to_protobuf(datastore_entity)
entity = model._entity_from_protobuf(protobuf)
ds_entity = model._entity_to_ds_entity(entity)
assert ds_entity["foo"] == [compressed_value_one, compressed_value_two]
示例4: test_legacy_repeated_structured_property_projection
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def test_legacy_repeated_structured_property_projection():
class OtherKind(model.Model):
foo = model.IntegerProperty()
bar = model.StringProperty()
class ThisKind(model.Model):
baz = model.StructuredProperty(OtherKind, repeated=True)
copacetic = model.BooleanProperty()
key = datastore.Key("ThisKind", 123, project="testing")
datastore_entity = datastore.Entity(key=key)
datastore_entity.update(
{"baz.foo": 42, "baz.bar": "himom", "copacetic": True}
)
protobuf = helpers.entity_to_protobuf(datastore_entity)
entity = model._entity_from_protobuf(protobuf)
assert isinstance(entity, ThisKind)
assert entity.baz[0].foo == 42
assert entity.baz[0].bar == "himom"
assert entity.copacetic is True
示例5: test_polymodel
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def test_polymodel():
class Animal(polymodel.PolyModel):
foo = model.IntegerProperty()
class Cat(Animal):
bar = model.StringProperty()
key = datastore.Key("Animal", 123, project="testing")
datastore_entity = datastore.Entity(key=key)
datastore_entity.update(
{"foo": 42, "bar": "himom!", "class": ["Animal", "Cat"]}
)
entity = model._entity_from_ds_entity(datastore_entity)
assert isinstance(entity, Cat)
assert entity.foo == 42
assert entity.bar == "himom!"
assert entity.class_ == ["Animal", "Cat"]
示例6: run_quickstart
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def run_quickstart():
# [START datastore_quickstart]
# Imports the Google Cloud client library
from google.cloud import datastore
# Instantiates a client
datastore_client = datastore.Client()
# The kind for the new entity
kind = 'Task'
# The name/ID for the new entity
name = 'sampletask1'
# The Cloud Datastore key for the new entity
task_key = datastore_client.key(kind, name)
# Prepares the new entity
task = datastore.Entity(key=task_key)
task['description'] = 'Buy milk'
# Saves the entity
datastore_client.put(task)
print('Saved {}: {}'.format(task.key.name, task['description']))
# [END datastore_quickstart]
示例7: entity_with_parent
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def entity_with_parent(client):
# [START datastore_entity_with_parent]
key_with_parent = client.key(
'TaskList', 'default', 'Task', 'sample_task')
task = datastore.Entity(key=key_with_parent)
task.update({
'category': 'Personal',
'done': False,
'priority': 4,
'description': 'Learn Cloud Datastore'
})
# [END datastore_entity_with_parent]
return task
示例8: properties
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def properties(client):
key = client.key('Task')
# [START datastore_properties]
task = datastore.Entity(
key,
exclude_from_indexes=['description'])
task.update({
'category': 'Personal',
'description': 'Learn Cloud Datastore',
'created': datetime.datetime.utcnow(),
'done': False,
'priority': 4,
'percent_complete': 10.5,
})
# [END datastore_properties]
return task
示例9: array_value
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def array_value(client):
key = client.key('Task')
# [START datastore_array_value]
task = datastore.Entity(key)
task.update({
'tags': [
'fun',
'programming'
],
'collaborators': [
'alice',
'bob'
]
})
# [END datastore_array_value]
return task
示例10: insert
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def insert(client):
# [START datastore_insert]
with client.transaction():
incomplete_key = client.key('Task')
task = datastore.Entity(key=incomplete_key)
task.update({
'category': 'Personal',
'done': False,
'priority': 4,
'description': 'Learn Cloud Datastore'
})
client.put(task)
# [END datastore_insert]
return task
示例11: batch_upsert
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def batch_upsert(client):
# [START datastore_batch_upsert]
task1 = datastore.Entity(client.key('Task', 1))
task1.update({
'category': 'Personal',
'done': False,
'priority': 4,
'description': 'Learn Cloud Datastore'
})
task2 = datastore.Entity(client.key('Task', 2))
task2.update({
'category': 'Work',
'done': False,
'priority': 8,
'description': 'Integrate Cloud Datastore'
})
client.put_multi([task1, task2])
# [END datastore_batch_upsert]
return task1, task2
示例12: ancestor_query
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def ancestor_query(client):
task = datastore.Entity(
client.key('TaskList', 'default', 'Task'))
task.update({
'category': 'Personal',
'description': 'Learn Cloud Datastore',
})
client.put(task)
# [START datastore_ancestor_query]
# Query filters are omitted in this example as any ancestor queries with a
# non-key filter require a composite index.
ancestor = client.key('TaskList', 'default')
query = client.query(kind='Task', ancestor=ancestor)
# [END datastore_ancestor_query]
return list(query.fetch())
示例13: exploding_properties
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def exploding_properties(client):
# [START datastore_exploding_properties]
task = datastore.Entity(client.key('Task'))
task.update({
'tags': [
'fun',
'programming',
'learn'
],
'collaborators': [
'alice',
'bob',
'charlie'
],
'created': datetime.datetime.utcnow()
})
# [END datastore_exploding_properties]
return task
示例14: transactional_get_or_create
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def transactional_get_or_create(client):
# [START datastore_transactional_get_or_create]
with client.transaction():
key = client.key('Task', datetime.datetime.utcnow().isoformat())
task = client.get(key)
if not task:
task = datastore.Entity(key)
task.update({
'description': 'Example task'
})
client.put(task)
return task
# [END datastore_transactional_get_or_create]
示例15: transactional_single_entity_group_read_only
# 需要导入模块: from google.cloud import datastore [as 别名]
# 或者: from google.cloud.datastore import Entity [as 别名]
def transactional_single_entity_group_read_only(client):
client.put_multi([
datastore.Entity(key=client.key('TaskList', 'default')),
datastore.Entity(key=client.key('TaskList', 'default', 'Task', 1))
])
# [START datastore_transactional_single_entity_group_read_only]
with client.transaction(read_only=True):
task_list_key = client.key('TaskList', 'default')
task_list = client.get(task_list_key)
query = client.query(kind='Task', ancestor=task_list_key)
tasks_in_list = list(query.fetch())
return task_list, tasks_in_list
# [END datastore_transactional_single_entity_group_read_only]