-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore_example.py
executable file
·44 lines (31 loc) · 1.15 KB
/
datastore_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import logging
import sys
import json
import googledatastore as datastore
def get_data(dataset_id):
# Set the dataset from the command line parameters.
datastore.set_options(dataset=dataset_id)
try:
req = datastore.RunQueryRequest()
query = req.query
query.kind.add().name = 'Hackathlon'
results = datastore.run_query(req).batch.entity_result
# results[0].entity.property[0].value.string_value
useful_res = []
for res in results:
ent = res.entity
dict_res = {}
for prop in ent.property:
dict_res[prop.name] = prop.value.string_value
useful_res.append(dict_res)
return json.dumps(useful_res)
except datastore.RPCError as e:
logging.error('Error while doing datastore operation')
logging.error('RPCError: %(method)s %(reason)s',
{'method': e.method, 'reason': e.reason})
logging.error('HTTPError: %(status)s %(reason)s',
{'status': e.response.status,
'reason': e.response.reason})
return
if __name__ == '__main__':
main()