constrained_numbers#

class polyfactory.value_generators.constrained_numbers.NumberGeneratorProtocol#

Bases: Protocol[T]

Protocol for custom callables used to generate numerical values

__call__(random: Random, minimum: T | None = None, maximum: T | None = None) T#

Signature of the callable.

Parameters:
  • random – An instance of random.

  • minimum – A minimum value.

  • maximum – A maximum value.

Returns:

The generated numeric value.

__init__(*args, **kwargs)#
polyfactory.value_generators.constrained_numbers.almost_equal_floats(value_1: float, value_2: float, *, delta: float = 1e-08) bool#

Return True if two floats are almost equal

Parameters:
  • value_1 – A float value.

  • value_2 – A float value.

  • delta – A minimal delta.

Returns:

Boolean dictating whether the floats can be considered equal - given python’s problematic comparison of floats.

polyfactory.value_generators.constrained_numbers.is_multiply_of_multiple_of_in_range(minimum: T, maximum: T, multiple_of: T) bool#

Determine if at least one multiply of multiple_of lies in the given range.

Parameters:
  • minimum – T: A minimum value.

  • maximum – T: A maximum value.

  • multiple_of – T: A value to use as a base for multiplication.

Returns:

Boolean dictating whether at least one multiply of multiple_of lies in the given range between minimum and maximum.

polyfactory.value_generators.constrained_numbers.is_almost_multiple_of(value: T, multiple_of: T) bool#

Determine whether a given value is a close enough to a multiple of multiple_of.

Parameters:
  • value – A numeric value.

  • multiple_of – Another numeric value.

Returns:

Boolean dictating whether value is a multiple of value.

polyfactory.value_generators.constrained_numbers.get_increment(value: T, t_type: type[T]) T#

Get a small increment base to add to constrained values, i.e. lt/gt entries.

Parameters:
  • value – A value of type T.

  • t_type – The type of value.

Returns:

An increment T.

polyfactory.value_generators.constrained_numbers.get_value_or_none(t_type: type[T], lt: T | None = None, le: T | None = None, gt: T | None = None, ge: T | None = None) tuple[Optional[T], Optional[T]]#

Return an optional value.

Parameters:
  • equal_value – An GE/LE value.

  • constrained – An GT/LT value.

  • increment – increment

Returns:

Optional T.

polyfactory.value_generators.constrained_numbers.get_constrained_number_range(t_type: type[T], random: Random, lt: T | None = None, le: T | None = None, gt: T | None = None, ge: T | None = None, multiple_of: T | None = None) tuple[T | None, T | None]#

Return the minimum and maximum values given a field_meta’s constraints.

Parameters:
  • t_type – A primitive constructor - int, float or Decimal.

  • random – An instance of Random.

  • lt – Less than value.

  • le – Less than or equal value.

  • gt – Greater than value.

  • ge – Greater than or equal value.

  • multiple_of – Multiple of value.

Returns:

a tuple of optional minimum and maximum values.

polyfactory.value_generators.constrained_numbers.generate_constrained_multiple_of(random: Random, minimum: T | None, maximum: T | None, multiple_of: T) T#

Generate a constrained multiple of multiple_of.

Parameters:
  • random – An instance of random.

  • minimum – A minimum value.

  • maximum – A maximum value.

  • multiple_of – A multiple of value.

Returns:

A value of type T.

polyfactory.value_generators.constrained_numbers.handle_constrained_int(random: Random, multiple_of: int | None = None, gt: int | None = None, ge: int | None = None, lt: int | None = None, le: int | None = None) int#

Handle constrained integers.

Parameters:
  • random – An instance of Random.

  • lt – Less than value.

  • le – Less than or equal value.

  • gt – Greater than value.

  • ge – Greater than or equal value.

  • multiple_of – Multiple of value.

Returns:

An integer.

polyfactory.value_generators.constrained_numbers.handle_constrained_float(random: Random, multiple_of: float | None = None, gt: float | None = None, ge: float | None = None, lt: float | None = None, le: float | None = None) float#

Handle constrained floats.

Parameters:
  • random – An instance of Random.

  • lt – Less than value.

  • le – Less than or equal value.

  • gt – Greater than value.

  • ge – Greater than or equal value.

  • multiple_of – Multiple of value.

Returns:

A float.

polyfactory.value_generators.constrained_numbers.validate_max_digits(max_digits: int, minimum: Decimal | None, decimal_places: int | None) None#

Validate that max digits is greater than minimum and decimal places.

Parameters:
  • max_digits – The maximal number of digits for the decimal.

  • minimum – Minimal value.

  • decimal_places – Number of decimal places

Returns:

‘None’

polyfactory.value_generators.constrained_numbers.handle_decimal_length(generated_decimal: Decimal, decimal_places: int | None, max_digits: int | None) Decimal#

Handle the length of the decimal.

Parameters:
  • generated_decimal – A decimal value.

  • decimal_places – Number of decimal places.

  • max_digits – Maximal number of digits.

polyfactory.value_generators.constrained_numbers.handle_constrained_decimal(random: Random, multiple_of: Decimal | None = None, decimal_places: int | None = None, max_digits: int | None = None, gt: Decimal | None = None, ge: Decimal | None = None, lt: Decimal | None = None, le: Decimal | None = None) Decimal#

Handle a constrained decimal.

Parameters:
  • random – An instance of Random.

  • multiple_of – Multiple of value.

  • decimal_places – Number of decimal places.

  • max_digits – Maximal number of digits.

  • lt – Less than value.

  • le – Less than or equal value.

  • gt – Greater than value.

  • ge – Greater than or equal value.

Returns:

A decimal.