Python

0.02.2

Keys#

Create / interact with gcloud datastore keys.

class gcloud.datastore.key.Key(path=None, namespace=None, dataset_id=None)[source]#

Bases: object

An immutable representation of a datastore Key.

__init__(path=None, namespace=None, dataset_id=None)[source]#

Constructor / initializer for a key.

Parameters:
  • namespace (str) – A namespace identifier for the key.
  • path (sequence of dicts) – Each dict must have keys ‘kind’ (a string) and optionally ‘name’ (a string) or ‘id’ (an integer).
  • dataset – The dataset ID assigned by back-end for the key.

Note

The key’s _dataset_id field must be None for keys created by application code. The gcloud.datastore.helpers.key_from_protobuf() factory will be set the field to an appropriate value for keys returned from the datastore backend. The application must treat any value set by the back-end as opaque.

Constructor / initializer for a key.

Parameters:
  • namespace (str) – A namespace identifier for the key.
  • path (sequence of dicts) – Each dict must have keys ‘kind’ (a string) and optionally ‘name’ (a string) or ‘id’ (an integer).
  • dataset – The dataset ID assigned by back-end for the key.

Note

The key’s _dataset_id field must be None for keys created by application code. The gcloud.datastore.helpers.key_from_protobuf() factory will be set the field to an appropriate value for keys returned from the datastore backend. The application must treat any value set by the back-end as opaque.

classmethod from_path(*args, **kwargs)[source]#

Factory method for creating a key based on a path.

Parameters:
  • args (tuple) – sequence of even length, where the first of each pair is a string representing the ‘kind’ of the path element, and the second of the pair is either a string (for the path element’s name) or an integer (for its id).
  • kwargs (dict) – Other named parameters which can be passed to Key.__init__().
Return type:

gcloud.datastore.key.Key

Returns:

a new Key instance

id(id_to_set=None)[source]#

ID setter / getter. Based on the last element of path.

Parameters:id_to_set (int) – The new ID for the key.
Return type:Key (for setter); or int (for getter)
Returns:a new key, cloned from self., with the given id (setter); or self’s id (getter).
id_or_name()[source]#

Getter. Based on the last element of path.

Return type:int (if ‘id’ is set); or str (the ‘name’)
Returns:True if the last element of the key’s path has either an ‘id’ or a ‘name’.
is_partial()[source]#

Boolean test: is the key fully mapped onto a backend entity?

Return type:bool
Returns:True if the last element of the key’s path does not have an ‘id’ or a ‘name’.
kind(kind=None)[source]#

Kind setter / getter. Based on the last element of path.

Parameters:kind (str) – The new kind for the key.
Return type:Key (for setter); or str (for getter)
Returns:a new key, cloned from self., with the given kind (setter); or self’s kind (getter).
name(name=None)[source]#

Name setter / getter. Based on the last element of path.

Parameters:kind (str) – The new name for the key.
Return type:Key (for setter); or str (for getter)
Returns:a new key, cloned from self., with the given name (setter); or self’s name (getter).
namespace(namespace=None)[source]#

Namespace setter / getter.

Parameters:namespace (str) – A namespace identifier for the key.
Return type:Key (for setter); or str (for getter)
Returns:a new key, cloned from self., with the given namespace (setter); or self’s namespace (getter).
parent()[source]#

Getter: return a new key for the next highest element in path.

Return type:gcloud.datastore.key.Key
Returns:a new Key instance, whose path consists of all but the last element of self’s path. If self has only one path element, return None.
path(path=None)[source]#

Path setter / getter.

Parameters:path (sequence of dicts) – Each dict must have keys ‘kind’ (a string) and optionally ‘name’ (a string) or ‘id’ (an integer).
Return type:Key (for setter); or str (for getter)
Returns:a new key, cloned from self., with the given path (setter); or self’s path (getter).
to_protobuf()[source]#

Return a protobuf corresponding to the key.

Return type:gcloud.datastore.datastore_v1_pb2.Key
Returns:The Protobuf representing the key.