以下职能:
KEYS = {}
def get(kind):
"returns a new key of a particular kind"
global KEYS
try:
return KEYS[kind].pop(0)
except (KeyError, IndexError):
handmade_key = Key.from_path(kind, 1)
start, end = allocate_ids(handmade_key, 3)
id_range = range(start, end+1)
KEYS[kind] = [Key.from_path(kind, id) for id in id_range]
for key in KEYS[kind]:
print "within get() -> %s:%s"%(key, key.id())
return get(kind)
我撰写了以下单位试验:
def testget2000(self):
s = set()
for i in range(0, 7):
key = keyfactory.get("Model1")
print "from get() -> %s:%s"%(key, key.id())
s.add(key)
self.assertEqual(len(s), 7)
self.assertEqual(len([k.id for k in s]), 2000)
得出以下错误:
FAIL: testget2000 (keyfactory_test.ModelTest)
Traceback (most recent call last): File "/home/vertegal/work/ei-sc/appengine/keyfactory_test.py",
line 36, in testget2000 self.assertEqual(len(s), 7) AssertionError: AssertionError: 5 != 7
-------------------- >> begin captured stdout <<
from get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAgw:2 from get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAww:3 within get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAQw:1 within get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAgw:2 within get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAww:3 from get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAQw:1 from get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAgw:2 from get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYAww:3 within get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYBAw:4 within get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYBQw:5 within get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYBgw:6 from get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYBAw:4 from get() -> agpkZXZ-cXVpenJ5cgwLEgZNb2RlbDEYBQw:5
我完全不理解为什么“从”中首次印发。 而且,为什么它分给同样的头几个孩子? 我是否制造了一些令人望而却步的关闭? KEYS是个例外的手稿而不是外部物体吗? 我已经去世。