-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.py
88 lines (70 loc) · 2.54 KB
/
startup.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Startup file for interactive prompt, used by "make python".
from ndb import utils
utils.tweak_logging()
import os
from google.appengine.api import apiproxy_stub_map
from google.appengine.api.blobstore import dict_blob_storage
from google.appengine.api.blobstore import blobstore_stub
from google.appengine.api import datastore_file_stub
from google.appengine.api import memcache
from google.appengine.api.memcache import memcache_stub
from google.appengine.api import taskqueue
from google.appengine.api.taskqueue import taskqueue_stub
from google.appengine.api import urlfetch_stub
from ndb import *
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
ds_stub = datastore_file_stub.DatastoreFileStub('_', None)
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', ds_stub)
mc_stub = memcache_stub.MemcacheServiceStub()
apiproxy_stub_map.apiproxy.RegisterStub('memcache', mc_stub)
tq_stub = taskqueue_stub.TaskQueueServiceStub()
apiproxy_stub_map.apiproxy.RegisterStub('taskqueue', tq_stub)
uf_stub = urlfetch_stub.URLFetchServiceStub()
apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', uf_stub)
bs_storage = dict_blob_storage.DictBlobStorage()
bs_stub = blobstore_stub.BlobstoreServiceStub(bs_storage)
apiproxy_stub_map.apiproxy.RegisterStub('blobstore', bs_stub)
os.environ['APPLICATION_ID'] = '_'
class Employee(Model):
name = StringProperty()
age = IntegerProperty()
rank = IntegerProperty()
@classmethod
def demographic(cls, min_age, max_age):
return cls.query().filter(AND(cls.age >= min_age, cls.age <= max_age))
@classmethod
def ranked(cls, rank):
return cls.query(cls.rank == rank).order(cls.age)
class Manager(Employee):
report = StructuredProperty(Employee, repeated=True)
reports = []
for (name, age, rank) in [('Joe', 21, 1), ('Jim', 30, 2), ('Jane', 23, 1)]:
emp = Employee(name=name, age=age, rank=rank)
reports.append(emp)
f1 = put_multi_async(reports)
boss = Manager(name='Fred', age=42, rank=4, report=reports)
f2 = boss.put_async()
f2.get_result()
for f in f1:
f.get_result()
class BlobTest(Model):
data = BlobProperty(indexed=True)
b1 = BlobTest(data='a')
b1.put()
b2 = BlobTest(data='\xff\x00')
b2.put()
from ndb import tasklets
ctx = tasklets.get_context()
conn = ctx._conn
E = Employee
M = Manager
B = BlobTest
class Node(Expando):
pass
Node.left = StructuredProperty(Node)
Node.right = StructuredProperty(Node, 'rite')
Node._fix_up_properties()
anode = Node(left=Node(label='a'), right=Node(label='b'), tag='root')
anode.put()
bnode = Node(left=Node(tag='x'), right=Node(tag='y'), tag='root')
bnode.put()