Skip to content

Commit c2b6401

Browse files
author
Jerjou Cheng
committed
Move ndb entities code snippets into github
1 parent 8dfe42a commit c2b6401

File tree

3 files changed

+463
-0
lines changed

3 files changed

+463
-0
lines changed

appengine/ndb/entities/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## App Engine Datastore NDB Entities Samples
2+
3+
This contains snippets used in the NDB entity documentation, demonstrating
4+
various operation on ndb entities.
5+
6+
<!-- auto-doc-link -->
7+
These samples are used on the following documentation page:
8+
9+
> https://cloud.google.com/appengine/docs/python/ndb/entities
10+
11+
<!-- end-auto-doc-link -->

appengine/ndb/entities/snippets.py

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.ext import ndb
16+
17+
18+
class Account(ndb.Model):
19+
username = ndb.StringProperty()
20+
userid = ndb.IntegerProperty()
21+
email = ndb.StringProperty()
22+
23+
24+
def create_sandy_with_keywords():
25+
sandy = Account(username='Sandy',
26+
userid=123,
27+
28+
return sandy
29+
30+
31+
def create_sandy_manually():
32+
sandy = Account()
33+
sandy.username = 'Sandy'
34+
sandy.userid = 123
35+
sandy.email = '[email protected]'
36+
return sandy
37+
38+
39+
def create_with_type_error_in_constructor():
40+
bad = Account(username='Sandy',
41+
userid='not integer') # raises an exception
42+
return bad
43+
44+
45+
def assign_with_type_error(sandy):
46+
sandy.username = 42 # raises an exception
47+
48+
49+
def store_sandy(sandy):
50+
sandy_key = sandy.put()
51+
return sandy_key
52+
53+
54+
def url_safe_sandy_key(sandy_key):
55+
url_string = sandy_key.urlsafe()
56+
return url_string
57+
58+
59+
def get_sandy_from_urlsafe(url_string):
60+
sandy_key = ndb.Key(urlsafe=url_string)
61+
sandy = sandy_key.get()
62+
return sandy
63+
64+
65+
def id_from_urlsafe(url_string):
66+
key = ndb.Key(urlsafe=url_string)
67+
kind_string = key.kind()
68+
ident = key.id()
69+
return key, ident, kind_string
70+
71+
72+
def update_key(key):
73+
sandy = key.get()
74+
sandy.email = '[email protected]'
75+
sandy.put()
76+
77+
78+
def delete_sandy(sandy):
79+
sandy.key.delete()
80+
81+
82+
def create_sandy_named_key():
83+
account = Account(username='Sandy', userid=1234, email='[email protected]',
84+
85+
86+
return account.key.id() # returns '[email protected]'
87+
88+
89+
def set_key_directly(account):
90+
account.key = ndb.Key('Account', '[email protected]')
91+
92+
# You can also use the model class object itself, rather than its name,
93+
# to specify the entity's kind:
94+
account.key = ndb.Key(Account, '[email protected]')
95+
96+
97+
def create_sandy_with_generated_numeric_id():
98+
# note: no id kwarg
99+
account = Account(username='Sandy', userid=1234, email='[email protected]')
100+
account.put()
101+
# Account.key will now have a key of the form: ndb.Key(Account, 71321839)
102+
# where the value 71321839 was generated by Datastore for us.
103+
return account
104+
105+
106+
class Revision(ndb.Model):
107+
message_text = ndb.StringProperty()
108+
109+
110+
def example_message_revisions():
111+
ndb.Key('Account', '[email protected]', 'Message', 123, 'Revision', '1')
112+
ndb.Key('Account', '[email protected]', 'Message', 123, 'Revision', '2')
113+
ndb.Key('Account', '[email protected]', 'Message', 456, 'Revision', '1')
114+
ndb.Key('Account', '[email protected]', 'Message', 789, 'Revision', '2')
115+
116+
117+
def example_revision_equivalents():
118+
ndb.Key('Account', '[email protected]', 'Message', 123, 'Revision', '1')
119+
120+
ndb.Key('Revision', '1', parent=ndb.Key(
121+
'Account', '[email protected]', 'Message', 123))
122+
123+
ndb.Key('Revision', '1', parent=ndb.Key(
124+
'Message', 123, parent=ndb.Key('Account', '[email protected]')))
125+
126+
sandy_key = ndb.Key(Account, '[email protected]')
127+
return sandy_key
128+
129+
130+
def insert_message():
131+
account_key = ndb.Key(Account, '[email protected]')
132+
133+
# Lets ask Datastore to allocate an ID.
134+
new_id = ndb.Model.allocate_ids(size=1, parent=account_key)[0]
135+
136+
# Datastore returns us an integer ID that we can use to create the message
137+
# key
138+
message_key = ndb.Key('Message', new_id, parent=account_key)
139+
140+
# Now we can put the message into Datastore
141+
initial_revision = Revision(
142+
message_text='Hello', id='1', parent=message_key)
143+
initial_revision.put()
144+
145+
return initial_revision
146+
147+
148+
def get_parent_key(initial_revision):
149+
message_key = initial_revision.key.parent()
150+
return message_key
151+
152+
153+
def multi_key_ops(list_of_entities):
154+
list_of_keys = ndb.put_multi(list_of_entities)
155+
list_of_entities = ndb.get_multi(list_of_keys)
156+
ndb.delete_multi(list_of_keys)
157+
158+
159+
class Mine(ndb.Expando):
160+
pass
161+
162+
163+
def create_expando():
164+
e = Mine()
165+
e.foo = 1
166+
e.bar = 'blah'
167+
e.tags = ['exp', 'and', 'oh']
168+
e.put()
169+
170+
return e
171+
172+
173+
def expando_properties(e):
174+
return e._properties
175+
# {'foo': GenericProperty('foo'), 'bar': GenericProperty('bar'),
176+
# 'tags': GenericProperty('tags', repeated=True)}
177+
178+
179+
class FlexEmployee(ndb.Expando):
180+
name = ndb.StringProperty()
181+
age = ndb.IntegerProperty()
182+
183+
184+
def create_flex_employee():
185+
emp = FlexEmployee(name='Sandy', location='SF')
186+
return emp
187+
188+
189+
class Specialized(ndb.Expando):
190+
_default_indexed = False
191+
192+
193+
def create_specialized():
194+
e = Specialized(foo='a', bar=['b'])
195+
return e._properties
196+
# {'foo': GenericProperty('foo', indexed=False),
197+
# 'bar': GenericProperty('bar', indexed=False, repeated=True)}
198+
199+
200+
def non_working_flex_query():
201+
FlexEmployee.query(FlexEmployee.location == 'SF')
202+
203+
204+
def working_flex_employee():
205+
FlexEmployee.query(ndb.GenericProperty('location') == 'SF')
206+
207+
208+
notification = None
209+
210+
211+
def notify(message):
212+
global notification
213+
notification = message
214+
215+
216+
class Friend(ndb.Model):
217+
name = ndb.StringProperty()
218+
219+
def _pre_put_hook(self):
220+
notify('Gee wiz I have a new friend!')
221+
222+
@classmethod
223+
def _post_delete_hook(cls, key, future):
224+
notify('I suck and nobody likes me.')
225+
226+
227+
def demonstrate_hook():
228+
f = Friend()
229+
f.name = 'Carole King'
230+
f.put() # _pre_put_hook is called
231+
yield f
232+
fut = f.key.delete_async() # _post_delete_hook not yet called
233+
fut.get_result() # _post_delete_hook is called
234+
yield f
235+
236+
237+
class MyModel(ndb.Model):
238+
pass
239+
240+
241+
def reserve_ids():
242+
first, last = MyModel.allocate_ids(100)
243+
return first, last
244+
245+
246+
def reserve_ids_with_parent(p):
247+
first, last = MyModel.allocate_ids(100, parent=p)
248+
return first, last
249+
250+
251+
def construct_keys(first, last):
252+
keys = [ndb.Key(MyModel, id) for id in range(first, last+1)]
253+
return keys
254+
255+
256+
def reserve_ids_up_to(N):
257+
first, last = MyModel.allocate_ids(max=N)
258+
return first, last

0 commit comments

Comments
 (0)