Primitive generators

This notebook contains tests for tohu's primitive generators.


In [1]:
import tohu
from tohu.v5.primitive_generators import *
from tohu.v5.utils import print_generated_sequence

In [2]:
print(f'Tohu version: {tohu.__version__}')


Tohu version: v0.5.1+29.g5c13aaf.dirty

Constant

Constant simply returns the same, constant value every time.


In [3]:
g = Constant('quux')

In [4]:
print_generated_sequence(g, num=10, seed=12345)


Generated sequence: quux, quux, quux, quux, quux, quux, quux, quux, quux, quux

Boolean

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)


Generated sequence: True, False, True, False, True, True, True, False, False, True, False, False, True, True, False, False, False, False, False, True
Generated sequence: False, True, True, False, True, False, True, True, True, True, True, True, False, True, True, True, True, True, True, True

Integer

Integer returns a random integer between low and high (both inclusive).


In [7]:
g = Integer(low=100, high=200)

In [8]:
print_generated_sequence(g, num=20, seed=12345)


Generated sequence: 102, 164, 118, 185, 182, 124, 149, 158, 100, 160, 162, 179, 145, 109, 122, 196, 197, 141, 147, 106

Float

Float returns a random float between low and high (both inclusive).


In [9]:
g = Float(low=2.3, high=4.2)

In [10]:
print_generated_sequence(g, num=10, sep='\n', fmt='.12f', seed=12345)


Generated sequence:

2.341986973439
3.261322119546
2.581541755571
4.111379193697
2.665260903014
3.173686486562
3.199295039721
3.477560969389
4.054422660690
2.445241620604

HashDigest

HashDigest returns hex strings representing hash digest values (or alternatively raw bytes).

HashDigest hex strings (uppercase)


In [11]:
g = HashDigest(length=6)

In [12]:
print_generated_sequence(g, num=10, seed=12345)


Generated sequence: E251FB, E52DE1, 1DFDFD, 810876, A44D15, A9AD2D, FE0F5E, 7E5191, 656D56, 224236

HashDigest hex strings (lowercase)


In [13]:
g = HashDigest(length=6, uppercase=False)

In [14]:
print_generated_sequence(g, num=10, seed=12345)


Generated sequence: e251fb, e52de1, 1dfdfd, 810876, a44d15, a9ad2d, fe0f5e, 7e5191, 656d56, 224236

HashDigest byte strings


In [15]:
g = HashDigest(length=10, as_bytes=True)

In [16]:
print_generated_sequence(g, num=5, seed=12345, sep='\n')


Generated sequence:

b'\xe2Q\xfb\xed\xe5-\xe1\xe3\x1d\xfd'
b'\x81\x08v!\xa4M\x15/\xa9\xad'
b'\xfe\x0f^4~Q\x91\xd3em'
b'"B6\x88\x1d\x9eu\x98\x01\xbb'
b'vl\xea\xf6q\xcd@v;\x9d'

FakerGenerator

FakerGenerator gives access to any of the methods supported by the faker module. Here are a couple of examples.

Example: random names


In [17]:
g = FakerGenerator(method='name')

In [18]:
print_generated_sequence(g, num=8, seed=12345)


Generated sequence: Adam Bryan, Jacob Lee, Candice Martinez, Justin Thompson, Heather Rubio, William Jenkins, Brittany Ball, Glenn Johnson

Example: random addresses


In [19]:
g = FakerGenerator(method='address')

In [20]:
print_generated_sequence(g, num=8, seed=12345, sep='\n---\n')


Generated sequence:

453 Ryan Islands
Greenstad, FL 97251
---
USS Irwin
FPO AA 66552
---
55075 William Rest
North Elizabeth, NH 38062
---
926 Alexandra Road
Romanberg, HI 99597
---
8202 Michelle Branch
Baileyborough, AL 08481
---
205 William Coves
Alexanderport, WI 72565
---
821 Patricia Hill Apt. 242
Apriltown, MO 24730
---
486 Karen Lodge Apt. 205
West Gregory, MT 33130

In [ ]: