Skip to content

Commit 317d66d

Browse files
committed
Merge pull request #1517 from dhermes/fix-1378
Update datastore docstrings to use client pattern.
2 parents 9c4807d + 8f0e3a7 commit 317d66d

File tree

6 files changed

+34
-30
lines changed

6 files changed

+34
-30
lines changed

gcloud/datastore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
You'll typically use these to get started with the API:
1818
1919
>>> from gcloud import datastore
20-
20+
>>>
2121
>>> client = datastore.Client()
2222
>>> key = client.key('EntityKind', 1234)
2323
>>> entity = datastore.Entity(key)

gcloud/datastore/batch.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ class Batch(object):
3535
operations and the ``delete`` operation into the same mutation, and send
3636
them to the server in a single API request::
3737
38-
>>> from gcloud.datastore.batch import Batch
39-
>>> batch = Batch()
38+
>>> from gcloud import datastore
39+
>>> client = datastore.Client()
40+
>>> batch = client.batch()
4041
>>> batch.put(entity1)
4142
>>> batch.put(entity2)
4243
>>> batch.delete(key3)
@@ -46,14 +47,14 @@ class Batch(object):
4647
:meth:`commit` will be called automatically if its block exits without
4748
raising an exception::
4849
49-
>>> with Batch() as batch:
50+
>>> with batch:
5051
... batch.put(entity1)
5152
... batch.put(entity2)
5253
... batch.delete(key3)
5354
5455
By default, no updates will be sent if the block exits with an error::
5556
56-
>>> with Batch() as batch:
57+
>>> with batch:
5758
... do_some_work(batch)
5859
... raise Exception() # rolls back
5960

gcloud/datastore/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ def run_query(self, project, query_pb, namespace=None,
220220
uses this method to fetch data:
221221
222222
>>> from gcloud import datastore
223-
>>> query = datastore.Query(kind='MyKind')
223+
>>> client = datastore.Client()
224+
>>> query = client.query(kind='MyKind')
224225
>>> query.add_filter('property', '=', 'val')
225226
226227
Using the query iterator's

gcloud/datastore/entity.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class Entity(dict):
3939
4040
Use :func:`gcloud.datastore.get` to retrieve an existing entity.
4141
42-
>>> datastore.get(key)
42+
>>> from gcloud import datastore
43+
>>> client = datastore.Client()
44+
>>> client.get(key)
4345
<Entity[{'kind': 'EntityKind', id: 1234}] {'property': 'value'}>
4446
4547
You can the set values on the entity just like you would on any
@@ -67,9 +69,7 @@ class Entity(dict):
6769
any decoding / encoding step.
6870
6971
:type key: :class:`gcloud.datastore.key.Key`
70-
:param key: Optional key to be set on entity. Required for
71-
:func:`gcloud.datastore.put()` and
72-
:func:`gcloud.datastore.put_multi()`
72+
:param key: Optional key to be set on entity.
7373
7474
:type exclude_from_indexes: tuple of string
7575
:param exclude_from_indexes: Names of fields whose values are not to be

gcloud/datastore/query.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def add_filter(self, property_name, operator, value):
194194
(ie, ``=``, ``<``, ``<=``, ``>``, ``>=``)::
195195
196196
>>> from gcloud import datastore
197-
>>> query = datastore.Query('Person')
197+
>>> client = datastore.Client()
198+
>>> query = client.query(kind='Person')
198199
>>> query.add_filter('name', '=', 'James')
199200
>>> query.add_filter('age', '>', 50)
200201
@@ -311,7 +312,8 @@ def fetch(self, limit=None, offset=0, start_cursor=None, end_cursor=None,
311312
For example::
312313
313314
>>> from gcloud import datastore
314-
>>> query = datastore.Query('Person')
315+
>>> client = datastore.Client()
316+
>>> query = client.query(kind='Person')
315317
>>> query.add_filter('name', '=', 'Sally')
316318
>>> list(query.fetch())
317319
[<Entity object>, <Entity object>, ...]

gcloud/datastore/transaction.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ class Transaction(Batch):
2828
mutation, and execute those within a transaction::
2929
3030
>>> from gcloud import datastore
31-
32-
>>> with datastore.Transaction():
33-
... datastore.put_multi([entity1, entity2])
31+
>>> client = datastore.Client()
32+
>>> with client.transaction():
33+
... client.put_multi([entity1, entity2])
3434
3535
Because it derives from :class:`Batch <.datastore.batch.Batch>`,
3636
:class:`Transaction` also provides :meth:`put` and :meth:`delete` methods::
3737
38-
>>> with datastore.Transaction() as xact:
38+
>>> with client.transaction() as xact:
3939
... xact.put(entity1)
4040
... xact.delete(entity2.key)
4141
4242
By default, the transaction is rolled back if the transaction block
4343
exits with an error::
4444
45-
>>> with datastore.Transaction():
45+
>>> with client.transaction():
4646
... do_some_work()
4747
... raise SomeException() # rolls back
4848
@@ -53,34 +53,34 @@ class Transaction(Batch):
5353
entities will not be available at save time! That means, if you
5454
try::
5555
56-
>>> with datastore.Transaction():
57-
... entity = datastore.Entity(key=Key('Thing'))
58-
... datastore.put(entity)
56+
>>> with client.transaction():
57+
... entity = datastore.Entity(key=client.key('Thing'))
58+
... client.put(entity)
5959
60-
``entity`` won't have a complete Key until the transaction is
60+
``entity`` won't have a complete key until the transaction is
6161
committed.
6262
6363
Once you exit the transaction (or call :meth:`commit`), the
6464
automatically generated ID will be assigned to the entity::
6565
66-
>>> with datastore.Transaction():
67-
... entity = datastore.Entity(key=Key('Thing'))
68-
... datastore.put(entity)
69-
... print entity.key.is_partial # There is no ID on this key.
66+
>>> with client.transaction():
67+
... entity = datastore.Entity(key=client.key('Thing'))
68+
... client.put(entity)
69+
... print(entity.key.is_partial) # There is no ID on this key.
7070
...
7171
True
72-
>>> print entity.key.is_partial # There *is* an ID.
72+
>>> print(entity.key.is_partial) # There *is* an ID.
7373
False
7474
7575
If you don't want to use the context manager you can initialize a
7676
transaction manually::
7777
78-
>>> transaction = datastore.Transaction()
78+
>>> transaction = client.transaction()
7979
>>> transaction.begin()
80-
81-
>>> entity = datastore.Entity(key=Key('Thing'))
80+
>>>
81+
>>> entity = datastore.Entity(key=client.key('Thing'))
8282
>>> transaction.put(entity)
83-
83+
>>>
8484
>>> if error:
8585
... transaction.rollback()
8686
... else:

0 commit comments

Comments
 (0)