我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用__future__.generators()。
def enableAttributes(genfunc): """Wrapper for generators to enable classlike attribute access. The generator definition should specify 'self' as the first parameter. Calls to a wrapped generator should ignore the self parameter. """ old = getargspec(genfunc) old[0].pop(0) new = getargspec(genfunc) new[0][0] = 'wrapped' specs = {'name': genfunc.func_name, 'oldargs': formatargspec(*old), 'newargs': formatargspec(*new)} exec(_redefinition % specs, genfunc.func_globals) #### A minimal, complete example
def transform(self, node, results): remove_future_import(u"with_statement", node) remove_future_import(u"nested_scopes", node) remove_future_import(u"generators", node)
def testGenerators(self): """equals works for generators""" def f(): yield 1 yield "hello" def g(): yield 1 yield "hello" assert Iter.equal(f(), g())
def generate_children(count, parent, life_min, life_max): """We are using a generator to randomly seed a sequence of specifications for the factory. While this is not strictly part of the Factory Pattern, it can be useful for testing the factory or for generating objects based on some pre-defined algorithm. It is perfectly acceptable to implement factories without generators (depends on your specific use-case).""" types = parent.__subclasses__() for i in range(count): yield { "class_name": random.choice(types).__name__, "lifespan": random.randint(life_min, life_max) }