Primitive generators

This notebook contains tests for tohu's primitive generators.


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

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


Tohu version: v0.6.7+17.g2d4b619.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=99999)


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=99999)
print_generated_sequence(g2, num=20, seed=99999)


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

Incremental

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=99999)


Generated sequence: 200, 204, 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 248, 252, 256, 260, 264, 268, 272, 276

Integer

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=99999)


Generated sequence: 120, 124, 170, 147, 192, 172, 119, 167, 184, 157, 133, 169, 136, 187, 184, 163, 183, 191, 144, 158

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, lowercase=True)

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 [ ]: