2323
2424class Entity (dict ):
2525 """
26- :type dataset: :class:`gcloud.datastore.dataset.Dataset`
27- :param dataset: The dataset in which this entity belongs.
26+ :type dataset: :class:`gcloud.datastore.dataset.Dataset`
27+ :param dataset: The dataset in which this entity belongs.
2828
29- :type kind: string
30- :param kind: The kind of entity this is, akin to a table name in a
31- relational database.
29+ :type kind: string
30+ :param kind: The kind of entity this is, akin to a table name in a
31+ relational database.
3232
33- Entities are mutable and act like a subclass of a dictionary.
34- This means you could take an existing entity and change the key
35- to duplicate the object.
33+ Entities are mutable and act like a subclass of a dictionary.
34+ This means you could take an existing entity and change the key
35+ to duplicate the object.
3636
37- This can be used on its own, however it is likely easier to use
38- the shortcut methods provided by :class:`gcloud.datastore.dataset.Dataset`
39- such as:
37+ This can be used on its own, however it is likely easier to use
38+ the shortcut methods provided by :class:`gcloud.datastore.dataset.Dataset`
39+ such as:
4040
41- - :func:`gcloud.datastore.dataset.Dataset.entity` to create a new entity.
41+ - :func:`gcloud.datastore.dataset.Dataset.entity` to create a new entity.
4242
43- >>> dataset.entity('MyEntityKind')
44- <Entity[{'kind': 'MyEntityKind'}] {}>
43+ >>> dataset.entity('MyEntityKind')
44+ <Entity[{'kind': 'MyEntityKind'}] {}>
4545
46- - :func:`gcloud.datastore.dataset.Dataset.get_entity`
47- to retrieve an existing entity.
46+ - :func:`gcloud.datastore.dataset.Dataset.get_entity`
47+ to retrieve an existing entity.
4848
49- >>> dataset.get_entity(key)
50- <Entity[{'kind': 'EntityKind', id: 1234}] {'property': 'value'}>
49+ >>> dataset.get_entity(key)
50+ <Entity[{'kind': 'EntityKind', id: 1234}] {'property': 'value'}>
5151
52- You can the set values on the entity
53- just like you would on any other dictionary.
52+ You can the set values on the entity
53+ just like you would on any other dictionary.
5454
55- >>> entity['age'] = 20
56- >>> entity['name'] = 'JJ'
57- >>> entity
58- <Entity[{'kind': 'EntityKind', id: 1234}] {'age': 20, 'name': 'JJ'}>
55+ >>> entity['age'] = 20
56+ >>> entity['name'] = 'JJ'
57+ >>> entity
58+ <Entity[{'kind': 'EntityKind', id: 1234}] {'age': 20, 'name': 'JJ'}>
5959
60- And you can cast an entity to a regular Python dictionary
61- with the `dict` builtin:
60+ And you can cast an entity to a regular Python dictionary
61+ with the `dict` builtin:
6262
63- >>> dict(entity)
64- {'age': 20, 'name': 'JJ'}
65- """
63+ >>> dict(entity)
64+ {'age': 20, 'name': 'JJ'}
65+ """
6666
6767 def __init__ (self , dataset = None , kind = None ):
6868 if dataset and kind :
@@ -73,28 +73,32 @@ def __init__(self, dataset=None, kind=None):
7373 def dataset (self ):
7474 """Get the :class:`gcloud.datastore.dataset.Dataset` in which this entity belongs.
7575
76- .. note::
77- This is based on the :class:`gcloud.datastore.key.Key` set on the entity.
78- That means that if you have no key set, the dataset might be `None`.
79- It also means that if you change the key on the entity, this will refer
80- to that key's dataset.
81- """
76+ :rtype: :class:`gcloud.datastore.dataset.Dataset`
77+ :returns: The Dataset containing the entity if there is a key,
78+ else None.
79+
80+ .. note::
81+ This is based on the :class:`gcloud.datastore.key.Key` set on the
82+ entity. That means that if you have no key set, the dataset might
83+ be `None`. It also means that if you change the key on the entity,
84+ this will refer to that key's dataset.
85+ """
8286 if self .key ():
8387 return self .key ().dataset ()
8488
8589 def key (self , key = None ):
8690 """Get or set the :class:`gcloud.datastore.key.Key` on the current entity.
8791
88- :type key: :class:`glcouddatastore.key.Key`
89- :param key: The key you want to set on the entity.
92+ :type key: :class:`glcouddatastore.key.Key`
93+ :param key: The key you want to set on the entity.
9094
91- :returns: Either the current key or the :class:`Entity`.
95+ :returns: Either the current key or the :class:`Entity`.
9296
93- >>> entity.key(my_other_key) # This returns the original entity.
94- <Entity[{'kind': 'OtherKeyKind', 'id': 1234}] {'property': 'value'}>
95- >>> entity.key() # This returns the key.
96- <Key[{'kind': 'OtherKeyKind', 'id': 1234}]>
97- """
97+ >>> entity.key(my_other_key) # This returns the original entity.
98+ <Entity[{'kind': 'OtherKeyKind', 'id': 1234}] {'property': 'value'}>
99+ >>> entity.key() # This returns the key.
100+ <Key[{'kind': 'OtherKeyKind', 'id': 1234}]>
101+ """
98102
99103 if key :
100104 self ._key = key
@@ -105,14 +109,14 @@ def key(self, key=None):
105109 def kind (self ):
106110 """Get the kind of the current entity.
107111
108- .. note::
109- This relies entirely on
110- the :class:`gcloud.datastore.key.Key`
111- set on the entity.
112- That means that we're not storing the kind of the entity at all,
113- just the properties and a pointer to a Key
114- which knows its Kind.
115- """
112+ .. note::
113+ This relies entirely on
114+ the :class:`gcloud.datastore.key.Key`
115+ set on the entity.
116+ That means that we're not storing the kind of the entity at all,
117+ just the properties and a pointer to a Key
118+ which knows its Kind.
119+ """
116120
117121 if self .key ():
118122 return self .key ().kind ()
@@ -121,27 +125,28 @@ def kind(self):
121125 def from_key (cls , key ):
122126 """Factory method for creating an entity based on the :class:`gcloud.datastore.key.Key`.
123127
124- :type key: :class:`gcloud.datastore.key.Key`
125- :param key: The key for the entity.
128+ :type key: :class:`gcloud.datastore.key.Key`
129+ :param key: The key for the entity.
126130
127- :returns: The :class:`Entity` derived from the
128- :class:`gcloud.datastore.key.Key`.
129- """
131+ :returns: The :class:`Entity` derived from the
132+ :class:`gcloud.datastore.key.Key`.
133+ """
130134
131135 return cls ().key (key )
132136
133137 @classmethod
134138 def from_protobuf (cls , pb , dataset = None ):
135139 """Factory method for creating an entity based on a protobuf.
136140
137- The protobuf should be one returned from the Cloud Datastore Protobuf API.
141+ The protobuf should be one returned from the Cloud Datastore
142+ Protobuf API.
138143
139- :type key: :class:`gcloud.datastore.datastore_v1_pb2.Entity`
140- :param key: The Protobuf representing the entity.
144+ :type key: :class:`gcloud.datastore.datastore_v1_pb2.Entity`
145+ :param key: The Protobuf representing the entity.
141146
142- :returns: The :class:`Entity` derived from the
143- :class:`gcloud.datastore.datastore_v1_pb2.Entity`.
144- """
147+ :returns: The :class:`Entity` derived from the
148+ :class:`gcloud.datastore.datastore_v1_pb2.Entity`.
149+ """
145150
146151 # This is here to avoid circular imports.
147152 from gcloud .datastore import helpers
@@ -158,15 +163,15 @@ def from_protobuf(cls, pb, dataset=None):
158163 def reload (self ):
159164 """Reloads the contents of this entity from the datastore.
160165
161- This method takes the :class:`gcloud.datastore.key.Key`, loads all
162- properties from the Cloud Datastore, and sets the updated properties on
163- the current object.
166+ This method takes the :class:`gcloud.datastore.key.Key`, loads all
167+ properties from the Cloud Datastore, and sets the updated properties on
168+ the current object.
164169
165- .. warning::
166- This will override any existing properties if a different value exists
167- remotely, however it will *not* override any properties that exist
168- only locally.
169- """
170+ .. warning::
171+ This will override any existing properties if a different value
172+ exists remotely, however it will *not* override any properties that
173+ exist only locally.
174+ """
170175
171176 # Note that you must have a valid key, otherwise this makes no sense.
172177 entity = self .dataset ().get_entity (self .key ().to_protobuf ())
@@ -178,10 +183,12 @@ def reload(self):
178183 def save (self ):
179184 """Save the entity in the Cloud Datastore.
180185
181- :rtype: :class:`gcloud.datastore.entity.Entity`
182- :returns: The entity with a possibly updated Key.
183- """
184- key_pb = self .dataset ().connection ().save_entity (dataset_id = self .dataset ().id (), key_pb = self .key ().to_protobuf (), properties = dict (self ))
186+ :rtype: :class:`gcloud.datastore.entity.Entity`
187+ :returns: The entity with a possibly updated Key.
188+ """
189+ key_pb = self .dataset ().connection ().save_entity (
190+ dataset_id = self .dataset ().id (),
191+ key_pb = self .key ().to_protobuf (), properties = dict (self ))
185192
186193 # If we are in a transaction and the current entity needs an
187194 # automatically assigned ID, tell the transaction where to put that.
@@ -200,16 +207,18 @@ def save(self):
200207 def delete (self ):
201208 """Delete the entity in the Cloud Datastore.
202209
203- .. note::
204- This is based entirely off of the :class:`gcloud.datastore.key.Key` set
205- on the entity. Whatever is stored remotely using the key on the entity
206- will be deleted.
207- """
208- self .dataset ().connection ().delete_entity (dataset_id = self .dataset ().id (), key_pb = self .key ().to_protobuf ())
210+ .. note::
211+ This is based entirely off of the :class:`gcloud.datastore.key.Key`
212+ set on the entity. Whatever is stored remotely using the key on the
213+ entity will be deleted.
214+ """
215+ self .dataset ().connection ().delete_entity (
216+ dataset_id = self .dataset ().id (), key_pb = self .key ().to_protobuf ())
209217
210218 def __repr__ (self ): # pragma NO COVER
211219 # An entity should have a key all the time (even if it's partial).
212220 if self .key ():
213- return '<Entity%s %s>' % (self .key ().path (), super (Entity , self ).__repr__ ())
221+ return '<Entity%s %s>' % (self .key ().path (),
222+ super (Entity , self ).__repr__ ())
214223 else :
215224 return '<Entity %s>' % (super (Entity , self ).__repr__ ())
0 commit comments