module_that_can_be_invoked_from_cli module#

Calculate arithmetic expressions from command line.

class CalculatorType(value)[source]#

Bases: CustomStrEnum

Define supported calculator types.

BINARY = 'binary'#
GENERAL = 'general'#
pydantic model BinaryInputs[source]#

Bases: CustomPydanticBaseModel

Define arguments for binary calculator.

calculator_type#

kind of calculator

Type:

typing.Literal[CalculatorType.BINARY]

first_number#

first number for the calculation

Type:

float

operator#

arithmetic operator to be used

Type:

BinaryArithmeticOperator

second_number#

second number for the calculation

Type:

float

Show JSON schema
{
   "title": "BinaryInputs",
   "description": "Define arguments for binary calculator.\n\nAttributes\n----------\ncalculator_type : typing.Literal[CalculatorType.BINARY]\n    kind of calculator\nfirst_number : float\n    first number for the calculation\noperator : BinaryArithmeticOperator\n    arithmetic operator to be used\nsecond_number : float\n    second number for the calculation",
   "type": "object",
   "properties": {
      "calculator_type": {
         "const": "binary",
         "description": "kind of calculator",
         "title": "Calculator Type"
      },
      "first_number": {
         "description": "first number for the calculation",
         "title": "First Number",
         "type": "number"
      },
      "operator": {
         "allOf": [
            {
               "$ref": "#/$defs/BinaryArithmeticOperator"
            }
         ],
         "description": "arithmetic operator to be used"
      },
      "second_number": {
         "description": "second number for the calculation",
         "title": "Second Number",
         "type": "number"
      }
   },
   "$defs": {
      "BinaryArithmeticOperator": {
         "description": "Define supported arithmetic operators.",
         "enum": [
            "+",
            "-",
            "*",
            "/"
         ],
         "title": "BinaryArithmeticOperator",
         "type": "string"
      }
   },
   "additionalProperties": false,
   "required": [
      "calculator_type",
      "first_number",
      "operator",
      "second_number"
   ]
}

Config:
  • extra: str = forbid

Fields:
field calculator_type: Literal[CalculatorType.BINARY] [Required]#

kind of calculator

field first_number: float [Required]#

first number for the calculation

field operator: BinaryArithmeticOperator [Required]#

arithmetic operator to be used

field second_number: float [Required]#

second number for the calculation

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

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

pydantic model GeneralInputs[source]#

Bases: CustomPydanticBaseModel

Define arguments of general calculator.

calculator_type#

kind of calculator

Type:

typing.Literal[CalculatorType.GENERAL]

expression#

mathematical expression to be evaluated

Type:

str

Show JSON schema
{
   "title": "GeneralInputs",
   "description": "Define arguments of general calculator.\n\nAttributes\n----------\ncalculator_type : typing.Literal[CalculatorType.GENERAL]\n    kind of calculator\nexpression : str\n    mathematical expression to be evaluated",
   "type": "object",
   "properties": {
      "calculator_type": {
         "const": "general",
         "description": "kind of calculator",
         "title": "Calculator Type"
      },
      "expression": {
         "description": "mathematical expression to be evaluated",
         "title": "Expression",
         "type": "string"
      }
   },
   "additionalProperties": false,
   "required": [
      "calculator_type",
      "expression"
   ]
}

Config:
  • extra: str = forbid

Fields:
field calculator_type: Literal[CalculatorType.GENERAL] [Required]#

kind of calculator

field expression: str [Required]#

mathematical expression to be evaluated

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

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

pydantic model UserInputs[source]#

Bases: CustomPydanticBaseModel

Define sub-commands and arguments of CLI calculator.

inputs#

inputs for the calculator

Type:

BinaryInputs | GeneralInputs

Show JSON schema
{
   "title": "UserInputs",
   "description": "Define sub-commands and arguments of CLI calculator.\n\nAttributes\n----------\ninputs : BinaryInputs | GeneralInputs\n    inputs for the calculator",
   "type": "object",
   "properties": {
      "inputs": {
         "description": "inputs for the calculator",
         "discriminator": {
            "mapping": {
               "binary": "#/$defs/BinaryInputs",
               "general": "#/$defs/GeneralInputs"
            },
            "propertyName": "calculator_type"
         },
         "oneOf": [
            {
               "$ref": "#/$defs/BinaryInputs"
            },
            {
               "$ref": "#/$defs/GeneralInputs"
            }
         ],
         "title": "Inputs"
      }
   },
   "$defs": {
      "BinaryArithmeticOperator": {
         "description": "Define supported arithmetic operators.",
         "enum": [
            "+",
            "-",
            "*",
            "/"
         ],
         "title": "BinaryArithmeticOperator",
         "type": "string"
      },
      "BinaryInputs": {
         "additionalProperties": false,
         "description": "Define arguments for binary calculator.\n\nAttributes\n----------\ncalculator_type : typing.Literal[CalculatorType.BINARY]\n    kind of calculator\nfirst_number : float\n    first number for the calculation\noperator : BinaryArithmeticOperator\n    arithmetic operator to be used\nsecond_number : float\n    second number for the calculation",
         "properties": {
            "calculator_type": {
               "const": "binary",
               "description": "kind of calculator",
               "title": "Calculator Type"
            },
            "first_number": {
               "description": "first number for the calculation",
               "title": "First Number",
               "type": "number"
            },
            "operator": {
               "allOf": [
                  {
                     "$ref": "#/$defs/BinaryArithmeticOperator"
                  }
               ],
               "description": "arithmetic operator to be used"
            },
            "second_number": {
               "description": "second number for the calculation",
               "title": "Second Number",
               "type": "number"
            }
         },
         "required": [
            "calculator_type",
            "first_number",
            "operator",
            "second_number"
         ],
         "title": "BinaryInputs",
         "type": "object"
      },
      "GeneralInputs": {
         "additionalProperties": false,
         "description": "Define arguments of general calculator.\n\nAttributes\n----------\ncalculator_type : typing.Literal[CalculatorType.GENERAL]\n    kind of calculator\nexpression : str\n    mathematical expression to be evaluated",
         "properties": {
            "calculator_type": {
               "const": "general",
               "description": "kind of calculator",
               "title": "Calculator Type"
            },
            "expression": {
               "description": "mathematical expression to be evaluated",
               "title": "Expression",
               "type": "string"
            }
         },
         "required": [
            "calculator_type",
            "expression"
         ],
         "title": "GeneralInputs",
         "type": "object"
      }
   },
   "additionalProperties": false,
   "required": [
      "inputs"
   ]
}

Config:
  • extra: str = forbid

Fields:
field inputs: BinaryInputs | GeneralInputs [Required]#

inputs for the calculator

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

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

capture_user_inputs() UserInputs[source]#

Capture user inputs for arithmetic expression.

Returns:

captured user inputs

Return type:

UserInputs

console_calculator() None[source]#

Calculate arithmetic expressions.