In [1]:
import tohu
from tohu.v4.primitive_generators import *
from tohu.v4.derived_generators import *
from tohu.v4.dispatch_generators import *
from tohu.v4.custom_generator import *
from tohu.v4.utils import print_generated_sequence, make_dummy_tuples
In [2]:
print(f'Tohu version: {tohu.__version__}')
In [3]:
class QuuxGenerator(CustomGenerator):
aa = Integer(100, 200)
bb = HashDigest(length=6)
cc = FakerGenerator(method='name')
In [4]:
g = QuuxGenerator()
In [5]:
print_generated_sequence(g, num=10, sep='\n', seed=12345)
Let's repeat the previous example, but explicitly set the name of generated items by setting the __tohu_items_name__
attribute inside the custom generator.
In [6]:
class SomeGeneratorWithExplicitItemsName(CustomGenerator):
__tohu_items_name__ = 'Foobar'
aa = Integer(100, 200)
bb = HashDigest(length=6)
cc = FakerGenerator(method='name')
In [7]:
g = SomeGeneratorWithExplicitItemsName()
The generated sequence is the same as above, but the name of the items has changed from Quux
to Foobar
.
In [8]:
print_generated_sequence(g, num=10, sep='\n', seed=12345)
In [9]:
class QuuxGenerator(CustomGenerator):
aa = Integer(100, 200)
def __init__(self, faker_method):
self.bb = FakerGenerator(method=faker_method)
# Note: the call to super().__init__() needs to be at the end,
# and it needs to be passed the same arguments as the __init__()
# method from which it is called (here: `faker_method`).
super().__init__(faker_method)
In [10]:
g1 = QuuxGenerator(faker_method='first_name')
g2 = QuuxGenerator(faker_method='city')
In [11]:
print_generated_sequence(g1, num=10, sep='\n', seed=12345); print()
print_generated_sequence(g2, num=10, sep='\n', seed=12345)
In [12]:
some_tuples = make_dummy_tuples('abcdefghijklmnopqrstuvwxyz')
#some_tuples[:5]
In [13]:
class QuuxGenerator(CustomGenerator):
aa = SelectOne(some_tuples)
bb = GetAttribute(aa, 'x')
cc = GetAttribute(aa, 'y')
In [14]:
g = QuuxGenerator()
In [15]:
print_generated_sequence(g, num=10, sep='\n', seed=12345)
In [16]:
def square(x):
return x * x
In [17]:
def add(x, y):
return x + y
In [18]:
class QuuxGenerator(CustomGenerator):
aa = Integer(0, 20)
bb = Integer(0, 20)
cc = Apply(add, aa, Apply(square, bb))
In [19]:
g = QuuxGenerator()
In [20]:
print_generated_sequence(g, num=10, sep='\n', seed=12345)
In [21]:
df = g.generate(num=100, seed=12345).to_df()
print(list(df['aa'][:20]))
print(list(df['bb'][:20]))
print(list(df['cc'][:20]))
In [22]:
all(df['aa'] + df['bb']**2 == df['cc'])
Out[22]:
In [23]:
class QuuxGenerator(CustomGenerator):
name = FakerGenerator(method="name")
tag = SelectOne(['a', 'bb', 'ccc'])
In [24]:
g = QuuxGenerator()
In [25]:
quux_items = g.generate(num=100, seed=12345)
In [26]:
quux_items.to_df().head(5)
Out[26]:
In [27]:
tag_lookup = {
'a': [1, 2, 3, 4, 5],
'bb': [10, 20, 30, 40, 50],
'ccc': [100, 200, 300, 400, 500],
}
In [28]:
class FoobarGenerator(CustomGenerator):
some_quux = SelectOne(quux_items)
number = SelectOneDerived(Lookup(GetAttribute(some_quux, 'tag'), tag_lookup))
In [29]:
h = FoobarGenerator()
In [30]:
h_items = h.generate(10000, seed=12345)
In [31]:
df = h_items.to_df(fields={'name': 'some_quux.name', 'tag': 'some_quux.tag', 'number': 'number'})
df.head()
Out[31]:
In [32]:
print(df.query('tag == "a"')['number'].isin([1, 2, 3, 4, 5]).all())
print(df.query('tag == "bb"')['number'].isin([10, 20, 30, 40, 50]).all())
print(df.query('tag == "ccc"')['number'].isin([100, 200, 300, 400, 500]).all())
In [33]:
df.query('tag == "a"').head(5)
Out[33]:
In [34]:
df.query('tag == "bb"').head(5)
Out[34]:
In [35]:
df.query('tag == "ccc"').head(5)
Out[35]:
In [ ]: