本文整理汇总了Python中gcloud.datastore.key.Key.kind方法的典型用法代码示例。如果您正苦于以下问题:Python Key.kind方法的具体用法?Python Key.kind怎么用?Python Key.kind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gcloud.datastore.key.Key
的用法示例。
在下文中一共展示了Key.kind方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_from_key_wo_dataset
# 需要导入模块: from gcloud.datastore.key import Key [as 别名]
# 或者: from gcloud.datastore.key.Key import kind [as 别名]
def test_from_key_wo_dataset(self):
from gcloud.datastore.key import Key
klass = self._getTargetClass()
key = Key().kind(_KIND).id(_ID)
entity = klass.from_key(key)
self.assertTrue(entity.dataset() is None)
self.assertEqual(entity.kind(), _KIND)
key = entity.key()
self.assertEqual(key.kind(), _KIND)
self.assertEqual(key.id(), _ID)
示例2: test_from_key
# 需要导入模块: from gcloud.datastore.key import Key [as 别名]
# 或者: from gcloud.datastore.key.Key import kind [as 别名]
def test_from_key(self):
from gcloud.datastore.dataset import Dataset
from gcloud.datastore.key import Key
klass = self._getTargetClass()
dataset = Dataset(_DATASET_ID)
key = Key(dataset=dataset).kind(_KIND).id(_ID)
entity = klass.from_key(key)
self.assertTrue(entity.dataset() is dataset)
self.assertEqual(entity.kind(), _KIND)
key = entity.key()
self.assertEqual(key.kind(), _KIND)
self.assertEqual(key.id(), _ID)
示例3: Entity
# 需要导入模块: from gcloud.datastore.key import Key [as 别名]
# 或者: from gcloud.datastore.key.Key import kind [as 别名]
class Entity(dict):
""":type dataset: :class:`gcloud.datastore.dataset.Dataset`
:param dataset: The dataset in which this entity belongs.
:type kind: string
:param kind: The kind of entity this is, akin to a table name in a
relational database.
Entities are mutable and act like a subclass of a dictionary.
This means you could take an existing entity and change the key
to duplicate the object.
This can be used on its own, however it is likely easier to use
the shortcut methods provided by :class:`gcloud.datastore.dataset.Dataset`
such as:
- :func:`gcloud.datastore.dataset.Dataset.entity` to create a new entity.
>>> dataset.entity('MyEntityKind')
<Entity[{'kind': 'MyEntityKind'}] {}>
- :func:`gcloud.datastore.dataset.Dataset.get_entity`
to retrieve an existing entity.
>>> dataset.get_entity(key)
<Entity[{'kind': 'EntityKind', id: 1234}] {'property': 'value'}>
You can the set values on the entity
just like you would on any other dictionary.
>>> entity['age'] = 20
>>> entity['name'] = 'JJ'
>>> entity
<Entity[{'kind': 'EntityKind', id: 1234}] {'age': 20, 'name': 'JJ'}>
And you can cast an entity to a regular Python dictionary
with the `dict` builtin:
>>> dict(entity)
{'age': 20, 'name': 'JJ'}
.. note::
When saving an entity to the backend, values which are "text"
('unicode' in Python2, 'str' in Python3) will be saved using
the 'text_value' field, after being encoded to UTF-8. When
retrieved from the back-end, such values will be decoded to "text"
again. Values which are "bytes" ('str' in Python2, 'bytes' in
Python3), will be saved using the 'blob_value' field, without
any decoding / encoding step.
:type dataset: :class:`gcloud.datastore.dataset.Dataset`, or None
:param dataset: the Dataset instance associated with this entity.
:type kind: str
:param kind: the "kind" of the entity (see
https://cloud.google.com/datastore/docs/concepts/entities#Datastore_Kinds_and_identifiers)
:param exclude_from_indexes: names of fields whose values are not to be
indexed for this entity.
"""
def __init__(self, dataset=None, kind=None, exclude_from_indexes=()):
super(Entity, self).__init__()
self._dataset = dataset
if kind:
self._key = Key().kind(kind)
else:
self._key = None
self._exclude_from_indexes = set(exclude_from_indexes)
def dataset(self):
"""Get the :class:`.dataset.Dataset` in which this entity belongs.
:rtype: :class:`gcloud.datastore.dataset.Dataset`
:returns: The Dataset containing the entity if there is a key,
else None.
.. note::
This is based on the :class:`gcloud.datastore.key.Key` set on the
entity. That means that if you have no key set, the dataset might
be `None`. It also means that if you change the key on the entity,
this will refer to that key's dataset.
"""
return self._dataset
def key(self, key=None):
"""Get or set the :class:`.datastore.key.Key` on the current entity.
:type key: :class:`glcouddatastore.key.Key`
:param key: The key you want to set on the entity.
:rtype: :class:`gcloud.datastore.key.Key` or :class:`Entity`.
:returns: Either the current key (on get) or the current
object (on set).
>>> entity.key(my_other_key) # This returns the original entity.
<Entity[{'kind': 'OtherKeyKind', 'id': 1234}] {'property': 'value'}>
>>> entity.key() # This returns the key.
<Key[{'kind': 'OtherKeyKind', 'id': 1234}]>
#.........这里部分代码省略.........