package_name_to_import_with.calculator_sub_package package#

Subpackages#

Submodules#

Module contents#

Expose binary operations.

pydantic model BinaryArithmeticExpression[source]#

Bases: CustomPydanticBaseModel

Define binary arithmetic expression.

left_operand#

first number of binary arithmetic expression

Type:

float

binary_operator#

arithmetic operator of binary arithmetic expression

Type:

BinaryArithmeticOperator

right_operand#

second number of binary arithmetic expression

Type:

float

operation#

function to perform arithmetic operation corresponding to binary_operator

Type:

BinaryArithmeticOperation

result#

result of binary arithmetic expression

Type:

float

Show JSON schema
{
   "title": "BinaryArithmeticExpression",
   "description": "Define binary arithmetic expression.\n\nAttributes\n----------\nleft_operand : float\n    first number of binary arithmetic expression\nbinary_operator : BinaryArithmeticOperator\n    arithmetic operator of binary arithmetic expression\nright_operand : float\n    second number of binary arithmetic expression\noperation : BinaryArithmeticOperation\n    function to perform arithmetic operation corresponding to `binary_operator`\nresult : float\n    result of binary arithmetic expression",
   "type": "object",
   "properties": {
      "left_operand": {
         "description": "first number of binary arithmetic expression",
         "title": "Left Operand",
         "type": "number"
      },
      "binary_operator": {
         "allOf": [
            {
               "$ref": "#/$defs/BinaryArithmeticOperator"
            }
         ],
         "description": "arithmetic operator of binary arithmetic expression"
      },
      "right_operand": {
         "description": "second number of binary arithmetic expression",
         "title": "Right Operand",
         "type": "number"
      }
   },
   "$defs": {
      "BinaryArithmeticOperator": {
         "description": "Define supported arithmetic operators.",
         "enum": [
            "+",
            "-",
            "*",
            "/"
         ],
         "title": "BinaryArithmeticOperator",
         "type": "string"
      }
   },
   "additionalProperties": false,
   "required": [
      "left_operand",
      "binary_operator",
      "right_operand"
   ]
}

Config:
  • extra: str = forbid

Fields:
Validators:
field left_operand: float [Required]#

first number of binary arithmetic expression

Validated by:
field binary_operator: BinaryArithmeticOperator [Required]#

arithmetic operator of binary arithmetic expression

Validated by:
field right_operand: float [Required]#

second number of binary arithmetic expression

Validated by:
validator validate_zero_division  »  all fields[source]#

Validate that division by zero is not attempted.

Returns:

unchanged instance if validation passes

Return type:

BinaryArithmeticExpression

Raises:

ValueError -- if division by zero is attempted

Validates:
  • all fields

property operation: Callable[[float, float], float]#

Store implementation of binary arithmetic operation.

Returns:

implementation of binary arithmetic operation corresponding to binary_operator

Return type:

BinaryArithmeticOperation

property result: float#

Store result of binary arithmetic expression.

Returns:

result of binary arithmetic expression

Return type:

float

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {'operation': ComputedFieldInfo(wrapped_property=<property object>, return_type=collections.abc.Callable[[float, float], float], alias=None, alias_priority=None, title=None, description='Store implementation of binary arithmetic operation.\n\nReturns\n-------\nBinaryArithmeticOperation\n    implementation of binary arithmetic operation corresponding to `binary_operator`', examples=None, json_schema_extra=None, repr=True), 'result': ComputedFieldInfo(wrapped_property=<functools.cached_property object>, return_type=<class 'float'>, alias=None, alias_priority=None, title=None, description='Store result of binary arithmetic expression.\n\nReturns\n-------\nfloat\n    result of binary arithmetic expression', examples=None, json_schema_extra=None, repr=True)}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

BinaryArithmeticOperation#

alias of Callable[float, float, float]

class BinaryArithmeticOperator(value)[source]#

Bases: CustomStrEnum

Define supported arithmetic operators.

ADDITION = '+'#
SUBTRACTION = '-'#
MULTIPLICATION = '*'#
DIVISION = '/'#
class IdentityElements(value)[source]#

Bases: CustomFloatEnum

Define assumed identity elements.

ADDITIVE_IDENTITY = 0.0#
MULTIPLICATIVE_IDENTITY = 1.0#
class InverseElements(value)[source]#

Bases: CustomFloatEnum

Define supported inverse elements.

ADDITIVE_INVERSE = -1.0#
MULTIPLICATIVE_INVERSE = 1.0#
add_numbers(left_addend: float, right_addend: float) float[source]#

Perform addition of two real numbers.

Parameters:
  • left_addend (float) -- first number to be added

  • right_addend (float) -- second number to be added

Returns:

sum of left_addend and right_addend

Return type:

float

Examples

>>> from package_name_to_import_with.calculator_sub_package import add_numbers
>>> add_numbers(1, 2)
3.0
>>> add_numbers(1, -2)
-1.0
>>> add_numbers(-1, 2)
1.0
>>> add_numbers(-1, -2)
-3.0
calculate_results(first_input: float, operator: BinaryArithmeticOperator, second_input: float) float[source]#

Perform basic binary arithmetic expressions.

Parameters:
  • first_input (float) -- left operand of binary arithmetic expression

  • operator (BinaryArithmeticOperator) -- kind of binary arithmetic expression

  • second_input (float) -- right operand of binary arithmetic expression

Returns:

result of binary arithmetic expression

Return type:

float

Examples

>>> from package_name_to_import_with import calculate_results
>>> calculate_results(1, "+", 2)
3.0
>>> calculate_results(1, "-", 2)
-1.0
>>> calculate_results(1, "*", 2)
2.0
>>> calculate_results(1, "/", 2)
0.5
divide_numbers(dividend: float, divisor: float) float[source]#

Perform division of two real numbers.

Parameters:
  • dividend (float) -- number which is divided

  • divisor (float) -- number which divides

Returns:

quotient of dividend by divisor

Return type:

float

Examples

>>> from package_name_to_import_with.calculator_sub_package import divide_numbers
>>> divide_numbers(1, 2)
0.5
>>> divide_numbers(1, -2)
-0.5
>>> divide_numbers(-1, 2)
-0.5
>>> divide_numbers(-1, -2)
0.5
get_negative(input_number: float) float[source]#

Get additive inverse of a real number.

Parameters:

input_number (float) -- number for which additive inverse is required

Returns:

negative of input_number

Return type:

float

Examples

>>> from package_name_to_import_with.calculator_sub_package import get_negative
>>> get_negative(1)
-1.0
>>> get_negative(-1)
1.0
get_reciprocal(input_number: float) float[source]#

Get multiplicative inverse of a real number.

Parameters:

input_number (float) -- number for which multiplicative inverse is required

Returns:

reciprocal of input_number

Return type:

float

Raises:

ValueError -- if input_number is additive identity, viz. zero

Examples

>>> from package_name_to_import_with.calculator_sub_package import get_reciprocal
>>> get_reciprocal(2)
0.5
>>> get_reciprocal(0.5)
2.0
multiply_numbers(left_multiplicand: float, right_multiplicand: float) float[source]#

Perform multiplication of two real numbers.

Parameters:
  • left_multiplicand (float) -- first number to be multiplied

  • right_multiplicand (float) -- second number to be multiplied

Returns:

product of two left_multiplicand and right_multiplicand

Return type:

float

Examples

>>> from package_name_to_import_with.calculator_sub_package import multiply_numbers
>>> multiply_numbers(1, 2)
2.0
>>> multiply_numbers(1, -2)
-2.0
>>> multiply_numbers(-1, 2)
-2.0
>>> multiply_numbers(-1, -2)
2.0
subtract_numbers(minuend: float, subtrahend: float) float[source]#

Perform subtraction of two real numbers.

Parameters:
  • minuend (float) -- number which is subtracted from

  • subtrahend (float) -- number which is subtracted

Returns:

difference of minuend from subtrahend

Return type:

float

Examples

>>> from package_name_to_import_with.calculator_sub_package import subtract_numbers
>>> subtract_numbers(1, 2)
-1.0
>>> subtract_numbers(1, -2)
3.0
>>> subtract_numbers(-1, 2)
-3.0
>>> subtract_numbers(-1, -2)
1.0