Skip to content

Commit 8caac14

Browse files
committed
Merge pull request #504 from tseaver/fixup-demo-20150107
Fixup datastore demo to match API changes
2 parents 743f78c + ec034e1 commit 8caac14

3 files changed

Lines changed: 60 additions & 34 deletions

File tree

‎gcloud/datastore/demo/__init__.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
from gcloud import datastore
1717

1818

19-
__all__ = ['get_dataset', 'DATASET_ID']
19+
__all__ = ['initialize', 'DATASET_ID']
2020

2121

2222
DATASET_ID = os.getenv('GCLOUD_TESTS_DATASET_ID')
2323

2424

25-
def get_dataset():
26-
return datastore.get_dataset(DATASET_ID)
25+
def initialize():
26+
datastore.set_default_connection()
27+
datastore.set_default_dataset_id(DATASET_ID)

‎gcloud/datastore/demo/demo.py‎

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,86 +11,111 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
# pragma NO COVER
1614
# Welcome to the gCloud Datastore Demo! (hit enter)
17-
1815
# We're going to walk through some of the basics...
1916
# Don't worry though. You don't need to do anything, just keep hitting enter...
2017

21-
# Let's start by importing the demo module and getting a dataset:
18+
# Let's start by importing the demo module and initializing our connection.
2219
from gcloud.datastore import demo
23-
dataset = demo.get_dataset()
20+
demo.initialize()
2421

2522
# Let's create a new entity of type "Thing" and name it 'Toy':
26-
toy = dataset.entity('Thing')
23+
from gcloud.datastore.key import Key
24+
key = Key('Thing')
25+
from gcloud.datastore.entity import Entity
26+
toy = Entity(key)
2727
toy.update({'name': 'Toy'})
2828

2929
# Now let's save it to our datastore:
3030
toy.save()
3131

3232
# If we look it up by its key, we should find it...
33-
print(dataset.get_entities([toy.key()]))
33+
from gcloud.datastore import get_entities
34+
print(get_entities([toy.key]))
3435

3536
# And we should be able to delete it...
36-
toy.delete()
37+
toy.key.delete()
3738

3839
# Since we deleted it, if we do another lookup it shouldn't be there again:
39-
print(dataset.get_entities([toy.key()]))
40+
print(get_entities([toy.key]))
4041

4142
# Now let's try a more advanced query.
43+
# First, let's create some entities.
44+
SAMPLE_DATA = [
45+
(1234, 'Computer', 10),
46+
(2345, 'Computer', 8),
47+
(3456, 'Laptop', 10),
48+
(4567, 'Printer', 11),
49+
(5678, 'Printer', 12),
50+
(6789, 'Computer', 13)]
51+
samples = []
52+
for id, name, age in SAMPLE_DATA:
53+
key = Key('Thing', id)
54+
samples.append(key)
55+
entity = Entity(key)
56+
entity['name'] = name
57+
entity['age'] = age
58+
entity.save()
4259
# We'll start by look at all Thing entities:
43-
query = dataset.query().kind('Thing')
60+
from gcloud.datastore.query import Query
61+
query = Query(kind='Thing')
4462

4563
# Let's look at the first two.
46-
print(query.limit(2).fetch())
64+
print(list(query.fetch(limit=2)))
4765

4866
# Now let's check for Thing entities named 'Computer'
49-
print(query.filter('name', '=', 'Computer').fetch())
67+
query.add_filter('name', '=', 'Computer')
68+
print(list(query.fetch()))
5069

5170
# If you want to filter by multiple attributes,
52-
# you can string .filter() calls together.
53-
print(query.filter('name', '=', 'Computer').filter('age', '=', 10).fetch())
71+
# you can call .add_filter multiple times on the query.
72+
query.add_filter('age', '=', 10)
73+
print(list(query.fetch()))
74+
75+
# Now delete them.
76+
print([key.delete() for key in samples])
5477

5578
# You can also work inside a transaction.
5679
# (Check the official docs for explanations of what's happening here.)
57-
with dataset.transaction():
80+
from gcloud.datastore.transaction import Transaction
81+
with Transaction():
5882
print('Creating and savng an entity...')
59-
thing = dataset.entity('Thing')
60-
thing.key(thing.key().name('foo'))
83+
key = Key('Thing', 'foo')
84+
thing = Entity(key)
6185
thing['age'] = 10
6286
thing.save()
6387

6488
print('Creating and saving another entity...')
65-
thing2 = dataset.entity('Thing')
66-
thing2.key(thing2.key().name('bar'))
89+
key2 = Key('Thing', 'bar')
90+
thing2 = Entity(key2)
6791
thing2['age'] = 15
6892
thing2.save()
6993

7094
print('Committing the transaction...')
7195

7296
# Now that the transaction is commited, let's delete the entities.
73-
print(thing.delete(), thing2.delete())
97+
print(key.delete(), key2.delete())
7498

7599
# To rollback a transaction, just call .rollback()
76-
with dataset.transaction() as t:
77-
thing = dataset.entity('Thing')
78-
thing.key(thing.key().name('another'))
100+
with Transaction() as t:
101+
key = Key('Thing', 'another')
102+
thing = Entity(key)
79103
thing.save()
80104
t.rollback()
81105

82106
# Let's check if the entity was actually created:
83-
created = dataset.get_entities([thing.key()])
107+
created = get_entities([key])
84108
print('yes' if created else 'no')
85109

86110
# Remember, a key won't be complete until the transaction is commited.
87-
# That is, while inside the transaction block, thing.key() will be incomplete.
88-
with dataset.transaction():
89-
thing = dataset.entity('Thing')
111+
# That is, while inside the transaction block, thing.key will be incomplete.
112+
with Transaction():
113+
key = Key('Thing') # partial
114+
thing = Entity(key)
90115
thing.save()
91-
print(thing.key()) # This will be partial
116+
print(thing.key) # This will still be partial
92117

93-
print(thing.key()) # This will be complete
118+
print(thing.key) # This will be complete
94119

95120
# Now let's delete the entity.
96-
thing.delete()
121+
thing.key.delete()

‎gcloud/demo.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class DemoRunner(object):
2525
"""An interactive runner of demo scripts."""
2626

27-
KEYPRESS_DELAY = 0.05
27+
KEYPRESS_DELAY = 0.02
2828
GLOBALS, LOCALS = globals(), locals()
2929
CODE, COMMENT = 'code', 'comment'
3030

0 commit comments

Comments
 (0)