1+ # Copyright 2022 Google, Inc.
2+ # Licensed under the Apache License, Version 2.0 (the "License");
3+ # you may not use this file except in compliance with the License.
4+ # You may obtain a copy of the License at
5+ #
6+ # http://www.apache.org/licenses/LICENSE-2.0
7+ #
8+ # Unless required by applicable law or agreed to in writing, software
9+ # distributed under the License is distributed on an "AS IS" BASIS,
10+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ # See the License for the specific language governing permissions and
12+ # limitations under the License.
13+
14+ import argparse
15+ from collections import defaultdict
16+ import datetime
17+ from pprint import pprint
18+
19+ import google .cloud .exceptions
20+ from google .cloud import datastore # noqa: I100
21+
22+ def _preamble ():
23+ # [START datastore_size_coloration_query]
24+ from google .cloud import datastore
25+
26+ # For help authenticating your client, visit
27+ # https://cloud.google.com/docs/authentication/getting-started
28+ client = datastore .Client ()
29+
30+ # [END datastore_size_coloration_query]
31+ assert client is not None
32+
33+ def in_query (client ):
34+ # [START datastore_in_query]
35+ query = client .query (kind = "Task" )
36+ query .add_filter ("tag" , "IN" , ["learn" , "study" ])
37+ # [END datastore_in_query]
38+
39+ return list (query .fetch ())
40+
41+ def not_equals_query (client ):
42+ # [START datastore_not_equals_query]
43+ query = client .query (kind = "Task" )
44+ query .add_filter ("category" , "!=" , "work" )
45+ # [END datastore_not_equals_query]
46+
47+ return list (query .fetch ())
48+
49+ def not_equals_query (client ):
50+ # [START datastore_not_in_query]
51+ query = client .query (kind = "Task" )
52+ query .add_filter ("category" , "NOT_IN" , ["work" , "chores" , "school" ])
53+ # [END datastore_not_in_query]
54+
55+ return list (query .fetch ())
56+
57+
58+ def eq_query_sorted (client ):
59+ # [START datastore_eq_query_sorted]
60+ query = client .query (kind = "Task" )
61+ query .add_filter ("tag" , "=" , "learn" )
62+ # Ordering on equality filters are ignored
63+ query .order = ['tag' ]
64+ # [END datastore_eq_query_sorted]
65+
66+ return list (query .fetch ())
67+
68+ def in_query_sorted (client ):
69+ # [START datastore_in_query_sorted]
70+ query = client .query (kind = "Task" )
71+ query .add_filter ("tag" , "IN" , ["learn" , "study" ])
72+ # Ordering on equality filters are ignored
73+ query .order = ['tag' ]
74+ # [END datastore_in_query_sorted]
75+
76+ return list (query .fetch ())
77+
78+ def main (project_id ):
79+ client = datastore .Client (project_id )
80+
81+ for name , function in globals ().items ():
82+ if name in ("main" , "_preamble" , "defaultdict" ) or not callable (function ):
83+ continue
84+
85+ print (name )
86+ pprint (function (client ))
87+ print ("\n -----------------\n " )
88+
89+
90+ if __name__ == "__main__" :
91+ parser = argparse .ArgumentParser (
92+ description = "Demonstrates datastore API operations."
93+ )
94+ parser .add_argument ("project_id" , help = "Your cloud project ID." )
95+
96+ args = parser .parse_args ()
97+
98+ main (args .project_id )
0 commit comments