This notebook contains tests for tohu's primitive generators.
In [1]:
import tohu
from tohu.v6.primitive_generators import *
from tohu.v6.generator_dispatch import *
from tohu.v6.utils import print_generated_sequence
In [2]:
print(f'Tohu version: {tohu.__version__}')
Constant
simply returns the same, constant value every time.
In [3]:
g = Constant('quux')
In [4]:
print_generated_sequence(g, num=10, seed=12345)
Boolean
returns either True
or False
, optionally with different probabilities.
In [5]:
g1 = Boolean()
g2 = Boolean(p=0.8)
In [6]:
print_generated_sequence(g1, num=20, seed=12345)
print_generated_sequence(g2, num=20, seed=99999)
Incremental
returns a sequence of numbers that increase in regular steps.
In [7]:
g = Incremental(start=200, step=4)
In [8]:
print_generated_sequence(g, num=20, seed=12345)
Integer
returns a random integer between low
and high
(both inclusive).
In [9]:
g = Integer(low=100, high=200)
In [10]:
print_generated_sequence(g, num=20, seed=12345)
Float
returns a random float between low
and high
(both inclusive).
In [11]:
g = Float(low=2.3, high=4.2)
In [12]:
print_generated_sequence(g, num=10, sep='\n', fmt='.12f', seed=12345)
In [13]:
g = CharString(length=15)
print_generated_sequence(g, num=5, seed=12345)
print_generated_sequence(g, num=5, seed=99999)
It is possible to explicitly specify the character set.
In [14]:
g = CharString(length=12, charset="ABCDEFG")
print_generated_sequence(g, num=5, sep='\n', seed=12345)
There are also a few pre-defined character sets.
In [15]:
g1 = CharString(length=12, charset="<lowercase>")
g2 = CharString(length=12, charset="<alphanumeric_uppercase>")
print_generated_sequence(g1, num=5, sep='\n', seed=12345); print()
print_generated_sequence(g2, num=5, sep='\n', seed=12345)
DigitString
is the same as CharString
with charset='0123456789'
.
In [16]:
g = DigitString(length=15)
print_generated_sequence(g, num=5, seed=12345)
print_generated_sequence(g, num=5, seed=99999)
Generates a sequence of sequentially numbered strings with a given prefix.
In [17]:
g = Sequential(prefix='Foo_', digits=3)
Calling reset()
on the generator makes the numbering start from 1 again.
In [18]:
g.reset()
print_generated_sequence(g, num=5)
print_generated_sequence(g, num=5)
print()
g.reset()
print_generated_sequence(g, num=5)
Note that the method Sequential.reset()
supports the seed
argument for consistency with other generators, but its value is ignored - the generator is simply reset to its initial value. This is illustrated here:
In [19]:
g.reset(seed=12345); print_generated_sequence(g, num=5)
g.reset(seed=99999); print_generated_sequence(g, num=5)
HashDigest
returns hex strings representing hash digest values (or alternatively raw bytes).
In [20]:
g = HashDigest(length=6)
In [21]:
print_generated_sequence(g, num=10, seed=12345)
In [22]:
g = HashDigest(length=6, uppercase=False)
In [23]:
print_generated_sequence(g, num=10, seed=12345)
In [24]:
g = HashDigest(length=10, as_bytes=True)
In [25]:
print_generated_sequence(g, num=5, seed=12345, sep='\n')
This generator can produce random numbers using any of the random number generators supported by numpy.
In [26]:
g1 = NumpyRandomGenerator(method="normal", loc=3.0, scale=5.0)
g2 = NumpyRandomGenerator(method="poisson", lam=30)
g3 = NumpyRandomGenerator(method="exponential", scale=0.3)
In [27]:
g1.reset(seed=12345); print_generated_sequence(g1, num=4)
g2.reset(seed=12345); print_generated_sequence(g2, num=15)
g3.reset(seed=12345); print_generated_sequence(g3, num=4)
FakerGenerator
gives access to any of the methods supported by the faker module. Here are a couple of examples.
In [28]:
g = FakerGenerator(method='name')
In [29]:
print_generated_sequence(g, num=8, seed=12345)
In [30]:
g = FakerGenerator(method='address')
In [31]:
print_generated_sequence(g, num=8, seed=12345, sep='\n---\n')
In [32]:
g = Timestamp(start="2018-01-01 11:22:33", end="2018-02-13 12:23:34")
In [33]:
type(next(g))
Out[33]:
In [34]:
print_generated_sequence(g, num=10, seed=12345, sep='\n')
In [35]:
g = Timestamp(start="2018-01-01 11:22:33", end="2018-02-13 12:23:34").strftime("%-d %b %Y, %H:%M (%a)")
In [36]:
type(next(g))
Out[36]:
In [37]:
print_generated_sequence(g, num=10, seed=12345, sep='\n')
In [38]:
g = Date(start="2018-01-01", end="2018-02-13")
In [39]:
type(next(g))
Out[39]:
In [40]:
print_generated_sequence(g, num=10, seed=12345, sep='\n')
In [41]:
g = Date(start="2018-01-01", end="2018-02-13").strftime("%-d %b %Y")
In [42]:
type(next(g))
Out[42]:
In [43]:
print_generated_sequence(g, num=10, seed=12345, sep='\n')
In [ ]: