English 中文(简体)
为什么这样列举?
原标题:Why does this enumerate this way?

以下职能:

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是个例外的手稿而不是外部物体吗? 我已经去世。

最佳回答

So it looks like when your captured output begins, the datastore is empty but KEYS[kind] has two values already populated. The datastore isn t allocating the same IDs twice, you just have leftover IDs that have never been allocated. Either track down what else is writing to KEYS, or just wipe it out at the beginning of your test.

另外,你似乎通过实际的同类模式。 Key. From_path 希望在网上打字,例如/ Model1 而不是Model1

问题回答

As Drew suggests, you might add

global KEYS
KEYS = {}

问题的根源在于设计: 难以测试使用高射入的全球状态的功能。 如果把科索沃统计局作为案例变量,使关键Factory阶层更好地服务于你。

你们试图通过这样做实现什么?

在你的单位测试开始采集之前,它就像有人叫get(kind),或KEYS

Maybe just add a raise Exception or otherwise print the traceback in get(kind) and make sure the first time it runs is from within your code.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签