Python
Keys#
Create / interact with gcloud datastore keys.
-
class
gcloud.datastore.key.Key(*path_args, **kwargs)[source]# Bases:
objectAn immutable representation of a datastore Key.
To create a basic key:
>>> Key('EntityKind', 1234) <Key[{'kind': 'EntityKind', 'id': 1234}]> >>> Key('EntityKind', 'foo') <Key[{'kind': 'EntityKind', 'name': 'foo'}]>
To create a key with a parent:
>>> Key('Parent', 'foo', 'Child', 1234) <Key[{'kind': 'Parent', 'name': 'foo'}, {'kind': 'Child', 'id': 1234}]> >>> Key('Child', 1234, parent=parent_key) <Key[{'kind': 'Parent', 'name': 'foo'}, {'kind': 'Child', 'id': 1234}]>
To create a partial key:
>>> Key('Parent', 'foo', 'Child') <Key[{'kind': 'Parent', 'name': 'foo'}, {'kind': 'Child'}]>
-
__init__(*path_args, **kwargs)[source]# Constructor / initializer for a key.
Parameters: - path_args (tuple of string and integer) – May represent a partial (odd length) or full (even length) key path.
- namespace (string) – A namespace identifier for the key. Can only be passed as a keyword argument.
- dataset_id (string) – The dataset ID associated with the key. Required, unless the implicit dataset ID has been set. Can only be passed as a keyword argument.
- parent (
gcloud.datastore.key.Key) – The parent of the key. Can only be passed as a keyword argument.
Constructor / initializer for a key.
Parameters: - path_args (tuple of string and integer) – May represent a partial (odd length) or full (even length) key path.
- namespace (string) – A namespace identifier for the key. Can only be passed as a keyword argument.
- dataset_id (string) – The dataset ID associated with the key. Required, unless the implicit dataset ID has been set. Can only be passed as a keyword argument.
- parent (
gcloud.datastore.key.Key) – The parent of the key. Can only be passed as a keyword argument.
-
completed_key(id_or_name)[source]# Creates new key from existing partial key by adding final ID/name.
Parameters: id_or_name (string or integer) – ID or name to be added to the key. Return type: gcloud.datastore.key.KeyReturns: A new Keyinstance with the same data as the current one and an extra ID or name added.Raises: ValueErrorif the current key is not partial or ifid_or_nameis not a string or integer.
-
dataset_id# Dataset ID getter.
Return type: string Returns: The key’s dataset ID.
-
flat_path# Getter for the key path as a tuple.
Return type: tuple of string and integer Returns: The tuple of elements in the path.
-
id# ID getter. Based on the last element of path.
Return type: integer Returns: The (integer) ID of the key.
-
id_or_name# Getter. Based on the last element of path.
Return type: integer (if id) or string (ifname)Returns: The last element of the key’s path if it is either an idor aname.
-
is_partial# Boolean indicating if the key has an ID (or name).
Return type: boolean Returns: Trueif the last element of the key’s path does not have anidor aname.
-
kind# Kind getter. Based on the last element of path.
Return type: string Returns: The kind of the current key.
-
name# Name getter. Based on the last element of path.
Return type: string Returns: The (string) name of the key.
-
namespace# Namespace getter.
Return type: string Returns: The namespace of the current key.
-
parent# The parent of the current key.
Return type: gcloud.datastore.key.KeyorNoneTypeReturns: A new Keyinstance, whose path consists of all but the last element of current path. If the current key has only one path element, returnsNone.
-
path# Path getter.
Returns a copy so that the key remains immutable.
Return type: listofdictReturns: The (key) path of the current key.
-