@@ -47,6 +47,7 @@ def __init__(self, path=None, namespace=None, dataset_id=None):
47
47
**must** treat any value set by the back-end as opaque.
48
48
"""
49
49
self ._path = path or [{'kind' : '' }]
50
+ self ._parent = None
50
51
self ._namespace = namespace
51
52
self ._dataset_id = dataset_id
52
53
@@ -166,21 +167,35 @@ def dataset_id(self):
166
167
"""
167
168
return self ._dataset_id
168
169
170
+ def _make_parent (self ):
171
+ """Creates a parent key for the current path.
172
+
173
+ Extracts all but the last element in the key path and creates a new
174
+ key, while still matching the namespace and the dataset ID.
175
+
176
+ :rtype: :class:`gcloud.datastore.key.Key` or `NoneType`
177
+ :returns: a new `Key` instance, whose path consists of all but the last
178
+ element of self's path. If self has only one path element,
179
+ returns None.
180
+ """
181
+ parent_path = self .path [:- 1 ]
182
+ if parent_path :
183
+ return Key (path = parent_path , dataset_id = self .dataset_id ,
184
+ namespace = self .namespace )
185
+
169
186
@property
170
187
def parent (self ):
171
188
"""Getter: return a new key for the next highest element in path.
172
189
173
- :rtype: :class:`gcloud.datastore.key.Key`
190
+ :rtype: :class:`gcloud.datastore.key.Key` or `NoneType`
174
191
:returns: a new `Key` instance, whose path consists of all but the last
175
192
element of self's path. If self has only one path element,
176
193
returns None.
177
194
"""
178
- if len (self ._path ) <= 1 :
179
- return None
180
- # This is temporary. Will be addressed throughout #451.
181
- clone = self ._clone ()
182
- clone ._path = self .path [:- 1 ]
183
- return clone
195
+ if self ._parent is None :
196
+ self ._parent = self ._make_parent ()
197
+
198
+ return self ._parent
184
199
185
200
def __repr__ (self ):
186
201
return '<Key%s>' % self .path
0 commit comments