External File Definition of Factory

In most generation of factories described so far, the definition of factory was directly described in the python file executed, but factories can also be generated by describing them in an external file and loading that file at runtime.

The external definition file is written as follows, in python code, so that the generated factory is bound to the variable FACTORY.

import uuid

FACTORY = randog.factory.from_example({
    "uuid": uuid.uuid4,
    "name": "",
    "age": 20,
})

# (optional) Settings used by 'byfile' mode of command executing
CSV_COLUMNS = ["uuid", "name", "age"]
OUTPUT_LINESEP = "LF"
OUTPUT_ENCODING = "utf8"

Note

In factory definition file, import randog can be omitted.

Warning

As discussed below, import randog cannot be omitted if the factory definition file is used as python code other than in from_pyfile or byfile mode.

If you save this file under the name factory_def.py, you can use it in your python code as follows:

import randog.factory

# load factory definition
factory = randog.factory.from_pyfile("./factory_def.py")

generated = factory.next()
assert isinstance(generated["name"], str)

See also

The definition file can also be used when executing randog as command in byfile mode such as randog byfile ./factory_def.py. See also byfile Mode.

Attributes

The following attributes are defined in external definition file.

  • FACTORY: Factory

    • the factory to be loaded when using from_pyfile and to be used when executing randog as command in byfile mode.

  • CSV_COLUMNS: list[str | (dict) -> Any], optional

    • the definitions of each column value. If it is defined with str, the value is taken from the generated object using that as the key. If it is defined with a function, the function is used with the generated object as an argument, and the return value is used.

    • This attribute is used when executing commands in byfile mode. See also byfile Mode.

  • OUTPUT_LINESEP: ‘CRLF’ | ‘LF’ | ‘CR’, optional

    • line separator for output.

    • This attribute is used when executing commands in byfile mode outputs to a file with the --output/-O option. If the --output-linesep/--O-ls option is specified, that takes precedence. See also Common Options.

  • OUTPUT_ENCODING: str, optional

    • encoding for output, such as ‘utf-8’.

    • This attribute is used when executing commands in byfile mode outputs to a file with the --output/-O option. If the --output-encoding/-X option is specified, that takes precedence. See also Common Options.

Importable definition files

Since the definition file is written in python, it can be imported from other python files, but there are some points to note if it is intended to be imported.

For example, see the definition file below:

import os
import randog

CSV_COLUMNS = ["id", "name"]

# other python file can import and reuse this
def create_factory(initial_id):
    return randog.factory.randdict(
        id=randog.factory.increment(initial_id),
        name=randog.factory.randstr(),
    )

if __name__ == "__randog__":
    initial_id = int(os.environ["INITIAL_ID"])
    FACTORY = create_factory(initial_id)

The following points are noted in this definition file to make it importable:

  • write import randog; This can only be omitted when this file is executed directly in from_pyfile or byfile mode of command execution, not when it is imported.

  • write the process that should not be executed at import in if __name__ == "__randog__"; If this is not done, and the environment variable INITIAL_ID is not set or contains non-numbers when imported, an exception will be raised and the python code importing this file will not be executable.

    • You can avoid it by other means, such as proper handling of exceptions. Choose the method appropriate to your situation.