module

headfake.field.core

Fake/mock field generation logic

Classes
  • Field(transformers, final_transformers, name, hidden, error_value, transform) Acts as abstract Field class.
  • FakerField(transformers, final_transformers, name, hidden, error_value, transform, fake) Abstract base field for Faker-based value creation.
  • OptionValueField Field to generate option values, based on a provided dictionary of probabilities.
  • DerivedField(transformers, final_transformers, name, hidden, error_value, transform) Base field which uses an existing field type (_internal_field) to generate values.
  • ConstantField Field which generates constant values. Automatically replaces scalar values (e.g. string, number) when fieldsets are created.
  • ConcatField Field which concatenates values generated by multiple fields together.
  • MapFileField Field which provides randomised file lookup based on a particular field.
  • LookupMapFileField Field which works with a specified MapFileField to get a data value from a specified column, using the key value from that field.
  • IfElseField Field which generates a value based on the results of a condition.
  • Condition Dependent field condition which compares the results of a field expression with a value, using an operator function.
  • RepeatField Field which repeats the generation of field values a random number of times within a specified range.
  • NumberField Field which generates a number based on a random float selection from a scipy statistical distribution.
  • BooleanField Field which generate a boolean (e.g. true/false) value based on a true_probability (default=0.5).
  • DateField Field which generates a date based on a random number of days from a scipy statistical distribution similarly to NumberField.
  • OperationField Field which generates a value using an operation to process two values (e.g. add, subtract). Like the 'Condition' used in IfElseField, it is easiest to use builtin Python "operator" functions such as operator.add, operator.sub but a custom function can be used if needed.
  • LookupField Field which returns the value of another specified field.
Functions
  • extract_date(value, row, date_format) Extracts date to use for min/max or other input parameter.
  • extract_number(value, row) Extracts number to use for min/max or other input parameter.
abstract class

headfake.field.core.Field(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING)

Acts as abstract Field class.

Fields use the attrs module to handle available/required class attributes/defaults.

Parameters
  • transformers (list) Optional list of transformer objects which act upon on field values at various points.
  • name (str) Optional name of field (defaults to incremental number)
Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

abstract class

headfake.field.core.FakerField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, fake=NOTHING)

Bases
headfake.field.core.Field

Abstract base field for Faker-based value creation.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.OptionValueField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, probabilities, option_picks=NOTHING)

Bases
headfake.field.core.Field

Field to generate option values, based on a provided dictionary of probabilities.

Attributes
  • probabilities (dict) Dictionary of values/probabilities (e.g. {"A":0.2,"B":0.8})
Raises
  • ValueError when probabilities do not add up to 1
Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

abstract class

headfake.field.core.DerivedField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING)

Bases
headfake.field.core.Field

Base field which uses an existing field type (_internal_field) to generate values.

The class can be extended with new properties which can be used to setup the _internal_field.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.ConstantField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, value)

Bases
headfake.field.core.Field

Field which generates constant values. Automatically replaces scalar values (e.g. string, number) when fieldsets are created.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.ConcatField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, fields=NOTHING, glue='')

Bases
headfake.field.core.Field

Field which concatenates values generated by multiple fields together.

You can also provide an optional glue string.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset Fieldset to initialise from
Returns

None

class

headfake.field.core.MapFileField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, mapping_file, key_field, key_field_store=NOTHING)

Bases
headfake.field.core.Field

Field which provides randomised file lookup based on a particular field.

On initialisation this loads a CSV-based mapping file and randomises the rows based on a particular 'key_field'. It is then used in conjuction with one or more LookupMapFileFields to lookup values in other fields in the mapping file.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.LookupMapFileField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, lookup_value_field, map_file_field, map_file_field_obj=None)

Bases
headfake.field.core.Field

Field which works with a specified MapFileField to get a data value from a specified column, using the key value from that field.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset Fieldset to initialise from
Returns

None

class

headfake.field.core.IfElseField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, condition, true_value, false_value, cond_obj=NOTHING)

Bases
headfake.field.core.Field

Field which generates a value based on the results of a condition.

If the result is true, the true_value is generated/returned and if false, the false_value is generated. The true and false values can be field definitions or scalar values (e.g. strings, numbers, dates).

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.Condition(name=None, field, operator, value, operator_fn=NOTHING)

Dependent field condition which compares the results of a field expression with a value, using an operator function.

In general it is easiest to use builtin Python "operator" functions such as operator.or_, operator.and_, operator.eq etc., however, it is possible to use a custom function if required (provided it accepts two values and returns a boolean value).

class

headfake.field.core.RepeatField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, field, min_repeats, max_repeats, glue=None)

Bases
headfake.field.core.Field

Field which repeats the generation of field values a random number of times within a specified range.

The result is output as either a list or, if a glue string is provided, as a concatenated value separated by that string.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.NumberField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, distribution, mean, sd, min=None, max=None, dp=None, dist_cls=NOTHING)

Bases
headfake.field.core.Field

Field which generates a number based on a random float selection from a scipy statistical distribution.

Available scipy distributions include all those from the scipy.stats module (see https://docs.scipy.org/doc/scipy/reference/stats.html).

For custom distributions, any class can be used provided it follows the same constructor arguments (loc and scale) and has an rvs() function.

Includes support for a mean and standard distribution. Also an optional decimal places (dp), min and max can be provided to produce numbers with these criteria.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.BooleanField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, true_value=1, false_value=0, true_probability=0.5)

Bases
headfake.field.core.Field

Field which generate a boolean (e.g. true/false) value based on a true_probability (default=0.5).

The true_value (default=1) and false_value (default=0) which are returned can be specified. This field forms the basis of the derived GenderField

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.DateField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, distribution, mean, sd, min=None, max=None, dp=None, min_format=None, max_format=None, mean_format=None, format=None, use_years=False, dist_cls=NOTHING)

Bases
headfake.field.core.NumberField headfake.field.core.Field

Field which generates a date based on a random number of days from a scipy statistical distribution similarly to NumberField.

The generated value is based on a mean date and standard deviation and the field 'adds' the generated number of days to the mean date.

Available scipy distributions include all those from the scipy.stats module (see https://docs.scipy.org/doc/scipy/reference/stats.html).

For custom distributions, any class can be used provided it follows the same constructor arguments (loc and scale) and has an rvs() function.

Includes mean, min and max which can be provided as date objects or strings. If the latter, the mean_format, min_format and max_format define the date formats.

If a 'format' parameter is provided, the date is output as a string, if not it is output as a date object.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.OperationField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, operator, first_value, second_value, operator_fn=NOTHING)

Bases
headfake.field.core.Field

Field which generates a value using an operation to process two values (e.g. add, subtract). Like the 'Condition' used in IfElseField, it is easiest to use builtin Python "operator" functions such as operator.add, operator.sub but a custom function can be used if needed.

The first and second values can be field definitions or scalar values (e.g. strings, numbers, dates).

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

class

headfake.field.core.LookupField(transformers=NOTHING, final_transformers=NOTHING, name='field_1', hidden=False, error_value=None, transform=NOTHING, field)

Bases
headfake.field.core.Field

Field which returns the value of another specified field.

Methods
  • init_from_fieldset(fieldset) Initialises field in fieldset.
  • next_value(row) (Union(any, dict(str: any))) Gets next generated value for field.
method
init_from_fieldset(fieldset)

Initialises field in fieldset.

This is run once all fields have been setup. It is generally used by fields in the fieldset to obtain information from other fields in the fieldset for later use.

Parameters
  • fieldset (headfake.Fieldset) Fieldset to initialise from
Returns

None

method
next_value(row)

Gets next generated value for field.

Acts as a decorator around the private '_next_value' method. If 'transformers' have been provided in the constructor they will act on the value after it has been generated.

Parameters
  • row (dict(str: any)) The current data row as a dictionary
Returns (Union(any, dict(str: any)))

Dictionary containing multiple fields OR a single field value

function

headfake.field.core.extract_number(value, row)

Extracts number to use for min/max or other input parameter.

If value is numeric (int or float) then it will be simply returned.

If it is a Field class, then the next_value function will be used to generate the value.

Otherwise an attempt will be made to convert it to a number.

:param value: :param row: :return:

function

headfake.field.core.extract_date(value, row, date_format)

Extracts date to use for min/max or other input parameter.

If value is a datetime then it will be simply returned.

If it is a Field class, then the next_value function will be used to generate the value.

Otherwise an attempt will be made to extract the date from it.

:param value: :param row: :return: