8000 Merge pull request #184 from tseaver/add_key_docstrings · googleapis/google-cloud-python@0a5fe6b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a5fe6b

Browse files
committed
Merge pull request #184 from tseaver/add_key_docstrings
Add docstrings for API method of g.datastore.key.Key.
2 parents 1095168 + d1280f3 commit 0a5fe6b

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

gcloud/datastore/key.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ class Key(object):
1111
"""
1212

1313
def __init__(self, dataset=None, namespace=None, path=None):
14+
"""Constructor / initializer for a key.
15+
16+
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
17+
:param dataset: A dataset instance for the key.
18+
19+
:type namespace: :class:`str`
20+
:param namespace: A namespace identifier for the key.
21+
22+
:type path: sequence of dicts
23+
:param path: Each dict must have keys 'kind' (a string) and optionally
24+
'name' (a string) or 'id' (an integer).
25+
"""
1426
self._dataset = dataset
1527
self._namespace = namespace
1628
self._path = path or [{'kind': ''}]
@@ -28,6 +40,20 @@ def _clone(self):
2840

2941
@classmethod
3042
def from_protobuf(cls, pb, dataset=None):
43+
"""Factory method for creating a key based on a protobuf.
44+
45+
The protobuf should be one returned from the Cloud Datastore Protobuf API.
46+
47+
:type pb: :class:`gcloud.datastore.datastore_v1_pb2.Key`
48+
:param pb: The Protobuf representing the key.
49+
50+
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
51+
:param dataset: A dataset instance. If not passed, defaults to an
52+
instance whose ID is derived from pb.
53+
54+
:rtype: :class:`gcloud.datastore.key.Key`
55+
:returns: a new `Key` instance
56+
"""
3157
path = []
3258
for element in pb.path_element:
3359
element_dict = {'kind': element.kind}
@@ -46,6 +72,11 @@ def from_protobuf(cls, pb, dataset=None):
4672
return cls(path=path, dataset=dataset)
4773

4874
def to_protobuf(self):
75+
"""Return a protobuf corresponding to the key.
76+
77+
:rtype: :class:`gcloud.datastore.datastore_v1_pb2.Key`
78+
:returns: The Protobuf representing the key.
79+
"""
4980
key = datastore_pb.Key()
5081

5182
# Technically a dataset is required to do anything with the key,
@@ -77,6 +108,20 @@ def to_protobuf(self):
77108

78109
@classmethod
79110
def from_path(cls, *args, **kwargs):
111+
"""Factory method for creating a key based on a path.
112+
113+
:type args: :class:`tuple
114+
:param args: sequence of even length, where the first of each
115+
pair is a string representing the 'kind' of the path element, and the
116+
second of the pair is either a string (for the path element's name)
117+
or an integer (for its id).
118+
119+
:type kwargs: :class:`dict`
120+
:param kwargs: Other named parameters which can be passed to `__init__()`.
121+
122+
:rtype: :class:`gcloud.datastore.key.Key`
123+
:returns: a new `Key` instance
124+
"""
80125
path = []
81126
items = iter(args)
82127

@@ -92,9 +137,25 @@ def from_path(cls, *args, **kwargs):
92137
return cls(**kwargs)
93138

94139
def is_partial(self):
140+
"""Boolean test: is the key fully mapped onto a backend entity?
141+
142+
:rtype: :class:`bool`
143+
:returns: True if the last element of the key's path does not have an 'id'
144+
or a 'name'.
145+
"""
95146
return (self.id_or_name() is None)
96147

97148
def dataset(self, dataset=None):
149+
"""Setter / getter.
150+
151+
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
152+
:param dataset: A dataset instance for the key.
153+
154+
:rtype: :class:`Key` (for setter); or
155+
:class:`gcloud.datastore.dataset.Dataset` (for getter)
156+
:returns: a new key, cloned from self., with the given dataset (setter);
157+
or self's dataset (getter).
158+
"""
98159
if dataset:
99160
clone = self._clone()
100161
clone._dataset = dataset
@@ -103,6 +164,15 @@ def dataset(self, dataset=None):
103164
return self._dataset
104165

105166
def namespace(self, namespace=None):
167+
"""Setter / getter.
168+
169+
:type namespace: :class:`str`
170+
:param namespace: A namespace identifier for the key.
171+
172+
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
173+
:returns: a new key, cloned from self., with the given namespace (setter);
174+
or self's namespace (getter).
175+
"""
106176
if namespace:
107177
clone = self._clone()
108178
clone._namespace = namespace
@@ -111,6 +181,16 @@ def namespace(self, namespace=None):
111181
return self._namespace
112182

113183
def path(self, path=None):
184+
"""Setter / getter.
185+
186+
:type path: sequence of dicts
187+
:param path: Each dict must have keys 'kind' (a string) and optionally
188+
'name' (a string) or 'id' (an integer).
189+
190+
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
191+
:returns: a new key, cloned from self., with the given path (setter);
192+
or self's path (getter).
193+
"""
114194
if path:
115195
clone = self._clone()
116196
clone._path = path
@@ -119,6 +199,15 @@ def path(self, path=None):
119199
return self._path
120200

121201
def kind(self, kind=None):
202+
"""Setter / getter. Based on the last element of path.
203+
204+
:type kind: :class:`str`
205+
:param kind: The new kind for the key.
206+
207+
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
208+
:returns: a new key, cloned from self., with the given kind (setter);
209+
or self's kind (getter).
210+
"""
122211
if kind:
123212
clone = self._clone()
124213
clone._path[-1]['kind'] = kind
@@ -127,6 +216,15 @@ def kind(self, kind=None):
127216
return self._path[-1]['kind']
128217

129218
def id(self, id=None):
219+
"""Setter / getter. Based on the last element of path.
220+
221+
:type kind: :class:`str`
222+
:param kind: The new kind for the key.
223+
224+
:rtype: :class:`Key` (for setter); or :class:`int` (for getter)
225+
:returns: a new key, cloned from self., with the given id (setter);
226+
or self's id (getter).
227+
"""
130228
if id:
131229
clone = self._clone()
132230
clone._path[-1]['id'] = id
@@ -135,6 +233,15 @@ def id(self, id=None):
135233
return self._path[-1].get('id')
136234

137235
def name(self, name=None):
236+
"""Setter / getter. Based on the last element of path.
237+
238+
:type kind: :class:`str`
239+
:param kind: The new name for the key.
240+
241+
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
242+
:returns: a new key, cloned from self., with the given name (setter);
243+
or self's name (getter).
244+
"""
138245
if name:
139246
clone = self._clone()
140247
clone._path[-1]['name'] = name
@@ -143,6 +250,12 @@ def name(self, name=None):
143250
return self._path[-1].get('name')
144251

145252
def id_or_name(self):
253+
"""Getter. Based on the last element of path.
254+
255+
:rtype: :class:`int` (if 'id' is set); or :class:`str` (the 'name')
256+
:returns: True if the last element of the key's path has either an 'id'
257+
or a 'name'.
258+
"""
146259
return self.id() or self.name()
147260

148261
def parent(self):#pragma NO COVER

0 commit comments

Comments
 (0)
0