API Documentation

class pyprobs.Probability

The Probability class has useful functions that return True or False based on the given probability.

Functions:

  • prob

  • iprob

  • set_constant

  • get

  • clear

  • count_values

Note: All of them require creating an instance except the prob function

Examples

Simple Usage:

>>> from pyprobs import Probability as pr
>>> pr.prob(50/100)  # You can pass float (i.e. 0.5, 0.157), int (i.e. 1, 0) or str (i.e. '50%', '3/11')
False
>>> pr.prob(50/100, num=5)
[False, False, False, True, False]

Suggested and More Advanced Usage:

>>> from pyprobs import Probability as pr
>>> p = pr()
>>> p.iprob('3/7', 0.25, num=2)
[[True, True], [False, False]]
>>> p.history
{'3/7': [True, True], 0.25: [False, False]}
>>> p.count_values('all')
{True: 2, False: 2}
>>> p.set_constant(1/1000, mutable=True)  # If you set the mutable parameter to False, you won't be able to change the constant again.
>>> p.get()  # You can get the constant and mutable value, also you can use it like "p.get(how='constant')" or "p.get(how='mutable')", this only returns the desired value.
{'constant': 0.001, 'mutable': True}
>>> p._constant # You can more easily get the constant value.
0.001
clear() None

Clears the instance’s history.

Basically does this:

>>> <instance>.history.clear()
count_values(which: str = 'last') Dict[bool, int]

Count the values in the instance’s history

Parameters:

which (str, optional) – What values you want. Can be ‘last’ or ‘all’. Defaults to ‘last’.

Raises:
  • InvalidParameterValue – When the which parameter is not ‘all’ or ‘last’, this error raises.

  • NotUsedError – Unless you use iprob function (and if the which parameter is set to ‘last’), this error raises.

Returns:

Returns a dict that contains True values in the key, and False values in the value.

Return type:

Dict[bool, int]

get(how: str = 'constant&mutable') Union[Dict[str, Union[int, float, bool, str]], int, float, bool, str]

You can get the constant and/or mutable by calling this function.

Parameters:

how (str, optional) – How you get the values. Can be ‘constant&mutable’, ‘mutable&constant’, ‘constant’ or ‘mutable’. Defaults to ‘constant&mutable’.

Raises:

InvalidParameterValue – If the how parameter is not among ‘constant&mutable’, ‘mutable&constant’, ‘constant’ or ‘mutable’, this error raises.

Returns:

  • If you set the how parameter to ‘constant&mutable’, returns a dict that contains the constant in the key and mutable in the value.

  • If you set the how parameter to ‘mutable&constant’, returns a dict that contains the mutable in the key and constant in the value.

  • If you set the how parameter to ‘constant’, returns only the constant value.

  • If you set the how parameter to ‘mutable’, returns only the mutable value.

Return type:

Union[Dict[str, Union[int, float, bool, str]], int, float, bool, str]

iprob(*args, num: int = 1) Union[bool, Iterable[bool]]

General decision function that returns True or False based on the given probability. This function can be only used when an instance was created from Probability.

Parameters:

num (int, optional) – The number of how many times the function will run. Defaults to 1.

Raises:
  • NotGivenValueError – When no value was given

  • NumError – When the num parameter was less than one and not int

  • ProbabilityTypeError – When the type of the given values are not among int, float, or str

Returns:

If only one arg was given, returns a bool value. Otherwise, returns a list that contains bool values.

Return type:

Union[bool, Iterable[bool]]

Examples

>>> from pyprobs import Probability as pr
>>> p = pr()
>>> p.iprob(1/5)
True
>>> p.iprob(3/5, 0.15, num=2)
[[True, True], [False, False]]

You can set a constant and use iprob by not giving any args:

>>> p.set_constant(0.5)  # You can also set a str constant, i.e "50%", "3/11". For more accurate results, give them as str.
>>> p.iprob()
True

You can see the history:

>>> p.history
{0.2: [False], 0.6: [False, True], 0.15: [False, True], 0.5: [True]}

You can count the values in the history: >>> p.count_values(which=”all”) # the which parameter defaults to “last” and returns the last value in the history {True: 1, False: 5}

classmethod prob(*args, num: int = 1) Union[bool, Iterable[bool]]

General decision function that returns True or False based on the given probability.

Parameters:

num (int, optional) – The number of how many times the function will run. Defaults to 1.

Raises:
  • NotGivenValueError – When no value was given

  • NumError – When the num parameter was less than one

  • ProbabilityTypeError – When the type of the given values are not among int, float, or str

Returns:

If only one arg was given, returns a bool value. Otherwise, returns a list that contains bool values.

Return type:

Union[bool, Iterable[bool]]

Examples

>>> from pyprobs import Probability as pr
>>> pr.prob(1/2)
True
>>> pr.prob(0.778)
False
>>> pr.prob("25%")
False
>>> pr.prob("25%", num=5)
[False, False, True, False, False]
set_constant(constant: Union[int, float, str], mutable: bool = True) None

You can set an int, float, or str constant by calling this function. After setting a constant you don’t need to pass any arguments to iprob. But if you pass any arguments to iprob, the arguments will be accepted not the constant. After setting the constant, you can get the constant by using the ‘get’ function.

Parameters:
  • constant (Union[int, float, str]) – The constant value, can be int, float, or str

  • mutable (bool, optional) – If you set this False, you won’t be allowed to change the instance’s constant. Defaults to True.

Raises:
  • ConstantError – The constant parameter must be int, float, or str.

  • ImmutableConstantVariableError – If the mutable was set to False, when you call this function again, this error raises.