@@ -70,13 +70,13 @@ def to_protobuf(self):
7070 """
7171 key = datastore_pb .Key ()
7272
73- if self ._dataset_id is not None :
74- key .partition_id .dataset_id = self ._dataset_id
73+ if self .dataset_id is not None :
74+ key .partition_id .dataset_id = self .dataset_id
7575
7676 if self ._namespace :
7777 key .partition_id .namespace = self ._namespace
7878
79- for item in self .path () :
79+ for item in self .path :
8080 element = key .path_element .add ()
8181 if 'kind' in item :
8282 element .kind = item ['kind' ]
@@ -87,121 +87,100 @@ def to_protobuf(self):
8787
8888 return key
8989
90+ @property
9091 def is_partial (self ):
91- """Boolean test: is the key fully mapped onto a backend entity?
92+ """Boolean indicating if the key has an ID (or name).
9293
9394 :rtype: :class:`bool`
9495 :returns: True if the last element of the key's path does not have
9596 an 'id' or a 'name'.
9697 """
97- return self .id_or_name () is None
98+ return self .id_or_name is None
9899
99- def namespace (self , namespace = None ):
100- """Namespace setter / getter.
100+ @property
101+ def namespace (self ):
102+ """Namespace getter.
101103
102- :type namespace: :class:`str`
103- :param namespace: A namespace identifier for the key.
104-
105- :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
106- :returns: a new key, cloned from self., with the given namespace
107- (setter); or self's namespace (getter).
104+ :rtype: :class:`str`
105+ :returns: The namespace of the current key.
108106 """
109- if namespace :
110- clone = self ._clone ()
111- clone ._namespace = namespace
112- return clone
113- else :
114- return self ._namespace
107+ return self ._namespace
115108
116- def path (self , path = None ):
117- """Path setter / getter.
109+ @property
110+ def path (self ):
111+ """Path getter.
118112
119- :type path: sequence of dicts
120- :param path: Each dict must have keys 'kind' (a string) and optionally
121- 'name' (a string) or 'id' (an integer).
113+ Returns a copy so that the key remains immutable.
122114
123- :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
124- :returns: a new key, cloned from self., with the given path (setter);
125- or self's path (getter).
115+ :rtype: :class:`str`
116+ :returns: The (key) path of the current key.
126117 """
127- if path :
128- clone = self ._clone ()
129- clone ._path = path
130- return clone
131- else :
132- return self ._path
133-
134- def kind (self , kind = None ):
135- """Kind setter / getter. Based on the last element of path.
136-
137- :type kind: :class:`str`
138- :param kind: The new kind for the key.
139-
140- :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
141- :returns: a new key, cloned from self., with the given kind (setter);
142- or self's kind (getter).
118+ return copy .deepcopy (self ._path )
119+
120+ @property
121+ def kind (self ):
122+ """Kind getter. Based on the last element of path.
123+
124+ :rtype: :class:`str`
125+ :returns: The kind of the current key.
143126 """
144- if kind :
145- clone = self ._clone ()
146- clone ._path [- 1 ]['kind' ] = kind
147- return clone
148- elif self .path ():
149- return self ._path [- 1 ]['kind' ]
150-
151- def id (self , id_to_set = None ):
152- """ID setter / getter. Based on the last element of path.
153-
154- :type id_to_set: :class:`int`
155- :param id_to_set: The new ID for the key.
156-
157- :rtype: :class:`Key` (for setter); or :class:`int` (for getter)
158- :returns: a new key, cloned from self., with the given id (setter);
159- or self's id (getter).
127+ if self .path :
128+ return self .path [- 1 ].get ('kind' )
129+
130+ @property
131+ def id (self ):
132+ """ID getter. Based on the last element of path.
133+
134+ :rtype: :class:`int`
135+ :returns: The (integer) ID of the key.
160136 """
161- if id_to_set :
162- clone = self ._clone ()
163- clone ._path [- 1 ]['id' ] = id_to_set
164- return clone
165- elif self .path ():
166- return self ._path [- 1 ].get ('id' )
167-
168- def name (self , name = None ):
169- """Name setter / getter. Based on the last element of path.
170-
171- :type kind: :class:`str`
172- :param kind: The new name for the key.
173-
174- :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
175- :returns: a new key, cloned from self., with the given name (setter);
176- or self's name (getter).
137+ if self .path :
138+ return self .path [- 1 ].get ('id' )
139+
140+ @property
141+ def name (self ):
142+ """Name getter. Based on the last element of path.
143+
144+ :rtype: :class:`str`
145+ :returns: The (string) name of the key.
177146 """
178- if name :
179- clone = self ._clone ()
180- clone ._path [- 1 ]['name' ] = name
181- return clone
182- elif self .path ():
183- return self ._path [- 1 ].get ('name' )
147+ if self .path :
148+ return self .path [- 1 ].get ('name' )
184149
150+ @property
185151 def id_or_name (self ):
186- """Getter. Based on the last element of path.
152+ """Getter. Based on the last element of path.
187153
188- :rtype: :class:`int` (if 'id' is set); or :class:`str` (the 'name')
189- :returns: True if the last element of the key's path has either an 'id'
154+ :rtype: :class:`int` (if 'id') or :class:`str` (if 'name')
155+ :returns: The last element of the key's path if it is either an 'id'
190156 or a 'name'.
191157 """
192- return self .id () or self .name ()
158+ return self .id or self .name
159+
160+ @property
161+ def dataset_id (self ):
162+ """Dataset ID getter.
163+
164+ :rtype: :class:`str`
165+ :returns: The key's dataset.
166+ """
167+ return self ._dataset_id
193168
169+ @property
194170 def parent (self ):
195171 """Getter: return a new key for the next highest element in path.
196172
197173 :rtype: :class:`gcloud.datastore.key.Key`
198174 :returns: a new `Key` instance, whose path consists of all but the last
199175 element of self's path. If self has only one path element,
200- return None.
176+ returns None.
201177 """
202178 if len (self ._path ) <= 1 :
203179 return None
204- return self .path (self .path ()[:- 1 ])
180+ # This is temporary. Will be addressed throughout #451.
181+ clone = self ._clone ()
182+ clone ._path = self .path [:- 1 ]
183+ return clone
205184
206185 def __repr__ (self ):
207- return '<Key%s>' % self .path ()
186+ return '<Key%s>' % self .path
0 commit comments