In [1]:
import tohu
from tohu.generators import BaseGenerator
from tohu.custom_generator import logger, CustomGeneratorMeta
from tohu.debugging import DummyGenerator
tohu.debugging.logger.setLevel('DEBUG')

Custom generator v2


In [2]:
class QuuxGenerator(BaseGenerator, metaclass=CustomGeneratorMeta):
    z = 42
    a = DummyGenerator('DummyA')
    
    def __init__(self):
        logger.debug("[QQQ] Start QuuxGenerator.__init__()")
        self.b = DummyGenerator('DummyB')
        self.foo = 23
        logger.debug("[QQQ] End QuuxGenerator.__init__()")


DEBUG  [DDD]
DEBUG  CustomGeneratorMeta.__new__
DEBUG     - metacls=<class 'tohu.custom_generator.CustomGeneratorMeta'>
DEBUG     - cg_name=QuuxGenerator
DEBUG     - bases=(<class 'tohu.generators.BaseGenerator'>,)
DEBUG     - clsdict={'__module__': '__main__', '__qualname__': 'QuuxGenerator', 'z': 42, 'a': <DummyGenerator: 'DummyA' (id=4629674528)>, '__init__': <function QuuxGenerator.__init__ at 0x113ef88c8>}
DEBUG     - new_obj=<class '__main__.QuuxGenerator'>
DEBUG  Using item class name 'Quux' (derived from custom generator name)

In [3]:
g = QuuxGenerator()


DEBUG  [QQQ] Start QuuxGenerator.__init__()
DEBUG  [QQQ] End QuuxGenerator.__init__()
DEBUG  [FFF]
DEBUG  cls_dict:
DEBUG     __module__: __main__
DEBUG     z: 42
DEBUG     a: <DummyGenerator: 'DummyA' (id=4629674528)>
DEBUG     __init__: <function attach_new_init_method.<locals>.new_init at 0x113ef89d8>
DEBUG     __doc__: None
DEBUG     __tohu_items_name__: Quux
DEBUG     reset: <function attach_new_reset_method.<locals>.new_reset at 0x113ef8e18>
DEBUG     __next__: <function attach_new_next_method.<locals>.new_next at 0x113f01048>
DEBUG     _spawn: <function attach_new_spawn_method.<locals>.new_spawn at 0x113f010d0>
DEBUG  obj_dict:
DEBUG     b: <DummyGenerator: 'DummyB' (id=4405009880)>
DEBUG     foo: 23
DEBUG     _clones: []
DEBUG  Found 2 field generator template(s):
DEBUG     a: <DummyGenerator: 'DummyA' (id=4629674528)>
DEBUG     b: <DummyGenerator: 'DummyB' (id=4405009880)>
DEBUG  Spawning field generator templates...
DEBUG  Field generators attached to custom generator:
DEBUG     a: <DummyGenerator: 'DummyA' (spawned from 4629674528)>
DEBUG     b: <DummyGenerator: 'DummyB' (spawned from 4405009880)>

In [4]:
g.reset(seed=None)


DEBUG  [EEE] Inside automatically generated reset() method for <__main__.QuuxGenerator object at 0x1068f1f28> (seed=None)
Out[4]:
<__main__.QuuxGenerator at 0x1068f1f28>

In [5]:
g.reset(seed=12345)


DEBUG  [EEE] Inside automatically generated reset() method for <__main__.QuuxGenerator object at 0x1068f1f28> (seed=12345)
DEBUG  Resetting field generator a=<DummyGenerator: 'DummyA' (spawned from 4629674528)> with seed=831769172
DEBUG  Resetting field generator b=<DummyGenerator: 'DummyB' (spawned from 4405009880)> with seed=2430986565
Out[5]:
<__main__.QuuxGenerator at 0x1068f1f28>

In [6]:
items = g.generate(10, seed=12345)


DEBUG  [EEE] Inside automatically generated reset() method for <__main__.QuuxGenerator object at 0x1068f1f28> (seed=12345)
DEBUG  Resetting field generator a=<DummyGenerator: 'DummyA' (spawned from 4629674528)> with seed=831769172
DEBUG  Resetting field generator b=<DummyGenerator: 'DummyB' (spawned from 4405009880)> with seed=2430986565

In [7]:
list(items)


Out[7]:
[Quux(a='<dummy_value: 0>', b='<dummy_value: 0>'),
 Quux(a='<dummy_value: 1>', b='<dummy_value: 1>'),
 Quux(a='<dummy_value: 2>', b='<dummy_value: 2>'),
 Quux(a='<dummy_value: 3>', b='<dummy_value: 3>'),
 Quux(a='<dummy_value: 4>', b='<dummy_value: 4>'),
 Quux(a='<dummy_value: 5>', b='<dummy_value: 5>'),
 Quux(a='<dummy_value: 6>', b='<dummy_value: 6>'),
 Quux(a='<dummy_value: 7>', b='<dummy_value: 7>'),
 Quux(a='<dummy_value: 8>', b='<dummy_value: 8>'),
 Quux(a='<dummy_value: 9>', b='<dummy_value: 9>')]

In [ ]: