Skip to content

Commit 5a4e766

Browse files
committed
Merge pull request #1053 from dhermes/fix-1048
Updating datastore demo for client pattern.
2 parents d3956db + c2791ab commit 5a4e766

3 files changed

Lines changed: 26 additions & 33 deletions

File tree

gcloud/datastore/demo/__init__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313
# limitations under the License.
1414

1515
import os
16-
from gcloud import datastore
16+
from gcloud.environment_vars import TESTS_DATASET
1717

18+
__all__ = ['DATASET_ID']
1819

19-
__all__ = ['initialize', 'DATASET_ID']
20-
21-
22-
DATASET_ID = os.getenv('GCLOUD_TESTS_DATASET_ID')
23-
24-
25-
def initialize():
26-
datastore.set_default_dataset_id(DATASET_ID)
20+
DATASET_ID = os.getenv(TESTS_DATASET)

gcloud/datastore/demo/demo.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,28 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818

19-
# Let's start by importing the demo module and initializing our connection.
19+
# Let's start by importing the demo module and initializing our client.
2020
from gcloud import datastore
2121
from gcloud.datastore import demo
2222

23-
demo.initialize()
24-
25-
# Let's import the package containing our helper classes:
23+
client = datastore.Client(dataset_id=demo.DATASET_ID)
2624

2725
# Let's create a new entity of type "Thing" and name it 'Toy':
28-
key = datastore.Key('Thing')
26+
key = client.key('Thing')
2927
toy = datastore.Entity(key)
3028
toy.update({'name': 'Toy'})
3129

3230
# Now let's save it to our datastore:
33-
datastore.put(toy)
31+
client.put(toy)
3432

3533
# If we look it up by its key, we should find it...
36-
print(datastore.get(toy.key))
34+
print(client.get(toy.key))
3735

3836
# And we should be able to delete it...
39-
datastore.delete(toy.key)
37+
client.delete(toy.key)
4038

4139
# Since we deleted it, if we do another lookup it shouldn't be there again:
42-
print(datastore.get(toy.key))
40+
print(client.get(toy.key))
4341

4442
# Now let's try a more advanced query.
4543
# First, let's create some entities.
@@ -52,14 +50,14 @@
5250
(6789, 'Computer', 13)]
5351
sample_keys = []
5452
for id, name, age in SAMPLE_DATA:
55-
key = datastore.Key('Thing', id)
53+
key = client.key('Thing', id)
5654
sample_keys.append(key)
5755
entity = datastore.Entity(key)
5856
entity['name'] = name
5957
entity['age'] = age
60-
datastore.put(entity)
58+
client.put(entity)
6159
# We'll start by look at all Thing entities:
62-
query = datastore.Query(kind='Thing')
60+
query = client.query(kind='Thing')
6361

6462
# Let's look at the first two.
6563
print(list(query.fetch(limit=2)))
@@ -74,48 +72,48 @@
7472
print(list(query.fetch()))
7573

7674
# Now delete them.
77-
datastore.delete_multi(sample_keys)
75+
client.delete_multi(sample_keys)
7876

7977
# You can also work inside a transaction.
8078
# (Check the official docs for explanations of what's happening here.)
81-
with datastore.Transaction() as xact:
79+
with client.transaction() as xact:
8280
print('Creating and saving an entity...')
83-
key = datastore.Key('Thing', 'foo')
81+
key = client.key('Thing', 'foo')
8482
thing = datastore.Entity(key)
8583
thing['age'] = 10
8684
xact.put(thing)
8785

8886
print('Creating and saving another entity...')
89-
key2 = datastore.Key('Thing', 'bar')
87+
key2 = client.key('Thing', 'bar')
9088
thing2 = datastore.Entity(key2)
9189
thing2['age'] = 15
9290
xact.put(thing2)
9391

9492
print('Committing the transaction...')
9593

9694
# Now that the transaction is commited, let's delete the entities.
97-
datastore.delete_multi([key, key2])
95+
client.delete_multi([key, key2])
9896

9997
# To rollback a transaction, just call .rollback()
100-
with datastore.Transaction() as xact:
101-
key = datastore.Key('Thing', 'another')
98+
with client.transaction() as xact:
99+
key = client.key('Thing', 'another')
102100
thing = datastore.Entity(key)
103101
xact.put(thing)
104102
xact.rollback()
105103

106104
# Let's check if the entity was actually created:
107-
created = datastore.get(key)
105+
created = client.get(key)
108106
print('yes' if created else 'no')
109107

110108
# Remember, a key won't be complete until the transaction is commited.
111109
# That is, while inside the transaction block, thing.key will be incomplete.
112-
with datastore.Transaction() as xact:
113-
key = datastore.Key('Thing') # partial
110+
with client.transaction() as xact:
111+
key = client.key('Thing') # partial
114112
thing = datastore.Entity(key)
115113
xact.put(thing)
116114
print(thing.key) # This will still be partial
117115

118116
print(thing.key) # This will be complete
119117

120118
# Now let's delete the entity.
121-
datastore.delete(thing.key)
119+
client.delete(thing.key)

gcloud/storage/demo/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# limitations under the License.
1414

1515
import os
16+
from gcloud.environment_vars import TESTS_PROJECT
1617

1718
__all__ = ['PROJECT_ID']
1819

19-
PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')
20+
PROJECT_ID = os.getenv(TESTS_PROJECT)

0 commit comments

Comments
 (0)