pip_services3_commons.random package

Submodules

Module contents

pip_services3_commons.random.__init__

Contains implementation of random args generators that are used for functional as well as non-functional testing. Used to generate random objects and fill databases with unique objects.

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_commons.random.RandomArray

Bases: object

Random generator for array objects.

Example:

value1 = RandomArray.pick([1, 2, 3, 4]) # Possible result: 3
static pick(values: List[Any]) → Any

Picks a random element from specified array.

Parameters

values – an array of any type

Returns

a randomly picked item.

class pip_services3_commons.random.RandomBoolean

Bases: object

Random generator for boolean values.

Example:

value1 = RandomBoolean.next_boolean()   # Possible result: true
value2 = RandomBoolean.chance(1,3)      # Possible result: false
static chance(chances: float, max_chances: float)bool

Calculates “chance” out of “max chances”. Example: 1 chance out of 3 chances (or 33.3%)

Parameters
  • chances – a chance proportional to maxChances.

  • max_chances – a maximum number of chances

Returns

random boolean “chance”

static next_boolean()bool

Generates a random boolean args.

Returns

a random boolean.

class pip_services3_commons.random.RandomDateTime

Bases: object

Random generator for Date time values.

Example:

(month must be in 1..12)
value1 = RandomDateTime.next_date(datetime.datetime(2010,1,1))       # Possible result: 2008-01-03
value2 = RandomDateTime.next_datetime(datetime.datetime(2017,1,1))   # Possible result: 2007-03-11 11:20:32
value3 = RandomDateTime.update_datetime(datetime.datetime(2010,1,2)) # Possible result: 2010-02-05 11:33:23
static next_date(min_year: <module 'datetime' from '/usr/local/lib/python3.10/datetime.py'>, max_year: <module 'datetime' from '/usr/local/lib/python3.10/datetime.py'> = None) → <module ‘datetime’ from ‘/usr/local/lib/python3.10/datetime.py’>

Generates a random Date in the range [‘min_year’, ‘max_year’]. This method generate dates without time (or time set to 00:00:00

Parameters
  • min_year – min range args

  • max_year – (optional) maximum range args

Returns

a random Date and time args.

static next_datetime(min_year: <module 'datetime' from '/usr/local/lib/python3.10/datetime.py'>, max_year: <module 'datetime' from '/usr/local/lib/python3.10/datetime.py'> = None) → <module ‘datetime’ from ‘/usr/local/lib/python3.10/datetime.py’>

Generates a random Date and time in the range [‘minYear’, ‘maxYear’]. This method generate dates without time (or time set to 00:00:00)

Parameters
  • min_year – min range args

  • max_year – (optional) maximum range args

Returns

a random Date and time args.

static update_datetime(value: <module 'datetime' from '/usr/local/lib/python3.10/datetime.py'>, range: Union[int, float] = None) → <module ‘datetime’ from ‘/usr/local/lib/python3.10/datetime.py’>

Updates (drifts) a Date args within specified range defined

Parameters
  • value – a Date args to drift.

  • range – (optional) a range in milliseconds. Default: 10 days

Returns

an updated DateTime args.

class pip_services3_commons.random.RandomDouble

Bases: object

Random generator for double values.

Example:

value1 = RandomDouble.next_double(5, 10)     # Possible result: 7.3
value2 = RandomDouble.next_double(10)        # Possible result: 3.7
value3 = RandomDouble.next_double(10, 3)     # Possible result: 9.2
static next_double(mmin: float, mmax: float = None)float

Generates a random double args in the range [‘minYear’, ‘maxYear’].

Parameters
  • mmin – (optional) minimum range args

  • mmax – max range args

Returns

a random double args.

static update_double(value: float, rrange: float = None)float

Updates (drifts) a double args within specified range defined

Parameters
  • rrange

  • value – a double args to drift.

  • range – (optional) a range. Default: 10% of the args

class pip_services3_commons.random.RandomFloat

Bases: object

Random generator for float values.

Example:

value1 = RandomFloat.next_float(5, 10)     # Possible result: 7.3
value2 = RandomFloat.next_float(10)        # Possible result: 3.7
value3 = RandomFloat.update_float(10, 3)   # Possible result: 9.2
static next_float(min: float, max: float = None)float
static update_float(value: float, range: float = None)float

Updates (drifts) a float args within specified range defined

Parameters
  • value – a float args to drift.

  • range – (optional) a range. Default: 10% of the args

Returns

updated random float args.

class pip_services3_commons.random.RandomInteger

Bases: object

Random generator for integer values.

Example:

value1 = RandomInteger.next_integer(5, 10)     # Possible result: 7
value2 = RandomInteger.next_integer(10)        # Possible result: 3
value3 = RandomInteger.update_integer(10, 3)   # Possible result: 9
static next_integer(min: int, max: int = None)int

Generates a integer in the range [‘min’, ‘max’]. If ‘max’ is omitted, then the range will be set to [0, ‘min’].

Parameters
  • min – minimum args of the integer that will be generated. If ‘max’ is omitted, then ‘max’ is set to ‘min’ and ‘min’ is set to 0.

  • max – (optional) maximum args of the float that will be generated. Defaults to ‘min’ if omitted.

Returns

generated random integer args.

static sequence(min: int, max: int = None) → List[int]

Generates a random sequence of integers starting from 0 like: [0,1,2,3…??]

Parameters
  • min – minimum args of the integer that will be generated. If ‘max’ is omitted, then ‘max’ is set to ‘min’ and ‘min’ is set to 0.

  • max – (optional) maximum args of the float that will be generated. Defaults to ‘min’ if omitted.

Returns

generated array of integers.

static update_integer(value: int, range: int = None)int

Updates (drifts) a integer args within specified range defined

Parameters
  • value – a integer args to drift.

  • range – (optional) a range. Default: 10% of the args

Returns

updated integer args.

class pip_services3_commons.random.RandomString

Bases: object

Random generator for string values.

Example:

value1 = RandomString.pickChar("ABC")     # Possible result: "C"
value2 = RandomString.pick(["A","B","C"]) # Possible result: "gBW"
static distort(value: str)str

Distorts a string by randomly replacing characters in it.

Parameters

value – a string to distort.

Returns

a distored string.

static next_alpha_char()str

Generates random alpha characted [A-Za-z] :return: a random characted.

static next_string(min_size: int, max_size: int)str

Generates a random string, consisting of upper and lower case letters (of the English alphabet), digits (0-9), and symbols (“_,.:-/.[].{},#-!,$=%.+^.&*-() “).

Parameters
  • min_size – (optional) minimum string length.

  • max_size – maximum string length.

Returns

a random string.

static pick(values: List[str])str

Picks a random string from an array of string.

Parameters

values – a string to pick from

Returns

a randomly picked string.

static pick_char(values: str)str

Picks a random character from a string.

Parameters

values – a string to pick a char from

Returns

a randomly picked char.

class pip_services3_commons.random.RandomText

Bases: object

Random generator for various text values like names, addresses or phone numbers.

Example:

value1 = RandomText.name()      # Possible result: "Sergio"
value2 = RandomText.verb()      # Possible result: "Run"
value3 = RandomText.text(50)    # Possible result: "Run jorge. Red high scream?"
static adjective()str

Generates a random adjective. The result args is capitalized.

Returns

a random adjective.

static color()str

Generates a random color name. The result args is capitalized.

Returns

a random color name.

static email()str

Generates a random email address.

Returns

a random email address.

static full_name()str

Generates a random person’s name which has the following structure <optional prefix> <first name> <second name> <optional suffix>

Returns

a random name.

static name()str

Generates a random person’s name which has the following structure <optional prefix> <first name> <second name> <optional suffix>

Returns

a random name.

static noun()str

Generates a random noun. The result value is capitalized.

Returns

a random noun

static phone()str

Generates a random phone number. The phone number has the format: (XXX) XXX-YYYY

Returns

a random phone number.

static phrase(min_size: int, max_size: int = None)str

Generates a random phrase which consists of few words separated by spaces. The first word is capitalized, others are not.

Parameters
  • min_size – (optional) minimum string length.

  • max_size – maximum string length.

Returns

a random phrase.

static stuff()str

Generates a random noun. The result args is capitalized.

Returns

a random noun.

static text(min_size: int, max_size: int = None)str

Generates a random text, consisting of first names, last names, colors, stuffs, adjectives, verbs, and punctuation marks.

Parameters
  • min_size – minimum amount of words to generate. Text will contain ‘minSize’ words if ‘maxSize’ is omitted.

  • max_size – (optional) maximum amount of words to generate.

Returns

a random text.

static verb()str

Generates a random verb. The result args is capitalized.

Returns

a random verb.

static word()str

Generates a random word from available first names, last names, colors, stuffs, adjectives, or verbs.

Returns

a random word.

static words(min_size: int, max_size: int = None)str

Generates a random text that consists of random number of random words separated by spaces.

Parameters
  • min_size – (optional) a minimum number of words.

  • max_size – a maximum number of words.

Returns

a random text.