本文整理汇总了Python中gcloud.datastore.key.Key.path方法的典型用法代码示例。如果您正苦于以下问题:Python Key.path方法的具体用法?Python Key.path怎么用?Python Key.path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gcloud.datastore.key.Key
的用法示例。
在下文中一共展示了Key.path方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_entity_hit
# 需要导入模块: from gcloud.datastore.key import Key [as 别名]
# 或者: from gcloud.datastore.key.Key import path [as 别名]
def test_get_entity_hit(self):
from gcloud.datastore.connection import datastore_pb
from gcloud.datastore.key import Key
DATASET_ID = 'DATASET'
KIND = 'Kind'
ID = 1234
PATH = [{'kind': KIND, 'id': ID}]
entity_pb = datastore_pb.Entity()
path_element = entity_pb.key.path_element.add()
path_element.kind = KIND
path_element.id = ID
prop = entity_pb.property.add()
prop.name = 'foo'
prop.value.string_value = 'Foo'
connection = _Connection(entity_pb)
dataset = self._makeOne(DATASET_ID, connection)
key = Key(dataset=dataset, path=PATH)
result = dataset.get_entity(key)
key = result.key()
self.assertTrue(key.dataset() is dataset)
self.assertEqual(key.path(), PATH)
self.assertEqual(list(result), ['foo'])
self.assertEqual(result['foo'], 'Foo')
示例2: Entity
# 需要导入模块: from gcloud.datastore.key import Key [as 别名]
# 或者: from gcloud.datastore.key.Key import path [as 别名]
#.........这里部分代码省略.........
"""Return our dataset, or raise NoDataset if not set.
:rtype: :class:`gcloud.datastore.key.Key`.
:returns: our key
:raises: NoDataset if key is None
"""
if self._dataset is None:
raise NoDataset()
return self._dataset
def reload(self):
"""Reloads the contents of this entity from the datastore.
This method takes the :class:`gcloud.datastore.key.Key`, loads all
properties from the Cloud Datastore, and sets the updated properties on
the current object.
.. warning::
This will override any existing properties if a different value
exists remotely, however it will *not* override any properties that
exist only locally.
"""
key = self._must_key
dataset = self._must_dataset
entity = dataset.get_entity(key.to_protobuf())
if entity:
self.update(entity)
return self
def save(self):
"""Save the entity in the Cloud Datastore.
.. note::
Any existing properties for the entity will be replaced by those
currently set on this instance. Already-stored properties which do
not correspond to keys set on this instance will be removed from
the datastore.
.. note::
Property values which are "text" ('unicode' in Python2, 'str' in
Python3) map to 'string_value' in the datastore; values which are
"bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.
:rtype: :class:`gcloud.datastore.entity.Entity`
:returns: The entity with a possibly updated Key.
"""
key = self._must_key
dataset = self._must_dataset
connection = dataset.connection()
key_pb = connection.save_entity(
dataset_id=dataset.id(),
key_pb=key.to_protobuf(),
properties=dict(self),
exclude_from_indexes=self.exclude_from_indexes())
# If we are in a transaction and the current entity needs an
# automatically assigned ID, tell the transaction where to put that.
transaction = connection.transaction()
if transaction and key.is_partial():
transaction.add_auto_id_entity(self)
if isinstance(key_pb, datastore_pb.Key):
# Update the path (which may have been altered).
# NOTE: The underlying namespace can't have changed in a save().
# The value of the dataset ID may have changed from implicit
# (i.e. None, with the ID implied from the dataset.Dataset
# object associated with the Entity/Key), but if it was
# implicit before the save() we leave it as implicit.
path = []
for element in key_pb.path_element:
key_part = {}
for descriptor, value in element._fields.items():
key_part[descriptor.name] = value
path.append(key_part)
self._key = key.path(path)
return self
def delete(self):
"""Delete the entity in the Cloud Datastore.
.. note::
This is based entirely off of the :class:`gcloud.datastore.key.Key`
set on the entity. Whatever is stored remotely using the key on the
entity will be deleted.
"""
key = self._must_key
dataset = self._must_dataset
dataset.connection().delete_entities(
dataset_id=dataset.id(),
key_pbs=[key.to_protobuf()],
)
def __repr__(self):
if self._key:
return '<Entity%s %s>' % (self._key.path(),
super(Entity, self).__repr__())
else:
return '<Entity %s>' % (super(Entity, self).__repr__())