Dict factory
You can create a factory that generates dict
. The elements of the dict are always randomly generated each time.
Like any other factory, a dict factory can be built in two ways: randdict, from_example.
Factory by randdict
If you use randdict, for example, the code would look like this:
>>> import randog.factory
>>> from randog.factory import DictItem
>>> # create a factory
>>> factory = randog.factory.randdict(
... name=randog.factory.randstr(length=16),
... sex=randog.factory.randchoice("F", "M"),
... age=DictItem( # by DictItem, key existence is also at random.
... randog.factory.randint(0, 100),
... 0.9,
... ),
... )
>>> generated = factory.next()
>>> assert isinstance(generated, dict)
>>> assert isinstance(generated["name"], str)
>>> assert generated["sex"] in ["F", "M"]
>>> assert "age" not in generated or isinstance(generated["age"], int)
As in this example, by passing value factories as keyword arguments to randdict
, you can create a dict factory that randomly generates each element. By passing a DictItem as an argument instead of a factory, it is also possible to randomize whether the key is generated in the generated dictionary; like age
in the example above.
Note
See also here for how to build each factory.
Note
If you want to generate a dict object corresponding to a model of SQLAlchemy, you can use randog.sqlalchemy.factory(as_dict=True).
Factory by from_example
If you use from_example, for example, the code would look like this:
>>> import randog.factory
>>> from randog import DictItemExample
>>> # create a factory
>>> factory = randog.factory.from_example({
... "name": "Smith",
... "sex": randog.factory.randchoice("F", "M"),
... "age": DictItemExample(22, 0.9)
... })
>>> generated = factory.next()
>>> assert isinstance(generated, dict)
>>> assert isinstance(generated["name"], str)
>>> assert generated["sex"] in ["F", "M"]
>>> assert "age" not in generated or isinstance(generated["age"], int)
Not limited to dictionaries, from_example
is a function that, given an object that is an example output, returns a factory that generates an object similar to that example. If a dict is given to from_example
, each of its values can be either an example value or a factory that generates values. Or, if you pass an example wrapped in DictItemExample, you can also randomize whether or not a key is generated; like age
in the example above.