Skip to content

Commit 62ca2a7

Browse files
author
Jerjou Cheng
committed
Move projection queries snippets to github
1 parent 7321446 commit 62ca2a7

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## App Engine Datastore NDB Projection Queries Samples
2+
3+
This contains snippets used in the NDB projection queries documentation,
4+
demonstrating various ways to make ndb projection queries.
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/projectionqueries
10+
11+
<!-- end-auto-doc-link -->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 Article(ndb.Model):
19+
title = ndb.StringProperty()
20+
author = ndb.StringProperty()
21+
tags = ndb.StringProperty(repeated=True)
22+
23+
24+
def print_author_tags():
25+
query = Article.query()
26+
articles = query.fetch(20, projection=[Article.author, Article.tags])
27+
for article in articles:
28+
print(article.author)
29+
print(article.tags)
30+
# article.title will raise a ndb.UnprojectedPropertyError
31+
32+
33+
class Address(ndb.Model):
34+
type = ndb.StringProperty() # E.g., 'home', 'work'
35+
street = ndb.StringProperty()
36+
city = ndb.StringProperty()
37+
38+
39+
class Contact(ndb.Model):
40+
name = ndb.StringProperty()
41+
addresses = ndb.StructuredProperty(Address, repeated=True)
42+
43+
44+
def fetch_sub_properties():
45+
Contact.query().fetch(projection=["name", "addresses.city"])
46+
Contact.query().fetch(projection=[Contact.name, Contact.addresses.city])
47+
48+
49+
def demonstrate_ndb_grouping():
50+
Article.query(projection=[Article.author], group_by=[Article.author])
51+
Article.query(projection=[Article.author], distinct=True)
52+
53+
54+
class Foo(ndb.Model):
55+
A = ndb.IntegerProperty(repeated=True)
56+
B = ndb.StringProperty(repeated=True)
57+
58+
59+
def declare_multiple_valued_property():
60+
entity = Foo(A=[1, 1, 2, 3], B=['x', 'y', 'x'])
61+
return entity
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
import snippets
16+
17+
18+
def test_print_author_tags(testbed, capsys):
19+
snippets.Article(title="one", author="Two", tags=["three"]).put()
20+
21+
snippets.print_author_tags()
22+
23+
stdout, _ = capsys.readouterr()
24+
assert 'Two' in stdout
25+
assert 'three' in stdout
26+
assert 'one' not in stdout
27+
28+
29+
def test_fetch_sub_properties(testbed):
30+
address = snippets.Address(type="home", street="college", city="staten")
31+
address.put()
32+
address2 = snippets.Address(type="home", street="brighton", city="staten")
33+
address2.put()
34+
snippets.Contact(name="one", addresses=[address, address2]).put()
35+
36+
snippets.fetch_sub_properties()
37+
38+
39+
def test_demonstrate_ndb_grouping(testbed):
40+
snippets.Article(title="one", author="Two", tags=["three"]).put()
41+
42+
snippets.demonstrate_ndb_grouping()
43+
44+
45+
def test_declare_multiple_valued_property(testbed):
46+
snippets.declare_multiple_valued_property()

0 commit comments

Comments
 (0)