Skip to content

Code

This part of the project documentation focuses on an developer audience. It contains information about the project structure, the project modules, and the project code. Use it as a reference for the technical implementation of the calculator project code.

Project Modules Overview

Does implementation of the app.

Table of contents

App

Do math calculations.

This script can be imported as a module and allows the user to make mathematical calc.

Examples:

>>> from app import calc.Calculator sd as calc
>>> calc.add(2, 4)
6.0
>>> calc.multiply(2.0, 4.0)
8.0
>>> from app import calc
>>> calc.divide(4.0, 2)
2.0

The module contains the following functions:

  • add - returns the sum of two numbers
  • subtract - returns the difference of two numbers
  • multiply - returns the product of two numbers
  • divide - returns the quotient of two numbers
  • power - returns the base to the power of the exponent
  • sqrt - returns the square root of a number

Calculator

Calculator class.

Source code in src/app/calc.py
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class Calculator:
    """Calculator class."""

    def add(self, a: Union[float, int], b: Union[float, int]) -> float:
        """
        Do addition of two numbers.

        Examples:
            >>> add(4.0, 2.0)
            6.0
            >>> add(4, 2)
            6.0

        Args:
            a: first number
            b: second number

        Returns:
            sum of the first and the second number
        """
        return float(a + b)

    def subtract(
        self, a: Union[float, int], b: Union[float, int]
    ) -> float:  # pylint: disable=invalid-name
        """
        Do subtraction of two numbers.

        Examples:
            >>> subtract(4.0, 2.0)
            2.0
            >>> subtract(4, 2)
            2.0

        Args:
            a: minuend
            b: subtrahend

        Returns:
            the difference between the minuend minus the subtrahend
        """
        return float(a - b)

    def multiply(
        self, a: Union[float, int], b: Union[float, int]
    ) -> float:  # pylint: disable=invalid-name
        """
        Do multiplication of two numbers.

        Examples:
            >>> multiply(4.0, 2.0)
            8.0
            >>> multiply(4, 2)
            8.0

        Args:
            a: first number
            b: second number

        Returns:
            the product of the two numbers
        """
        return float(a * b)

    def divide(
        self, a: Union[float, int], b: Union[float, int]
    ) -> float:  # pylint: disable=invalid-name
        """
        Do division of two numbers.

        Examples:
            >>> divide(4.0, 2.0)
            2.0
            >>> divide(4, 2)
            2.0
            >>> divide(4, 0)
            Traceback (most recent call last):
            ...
            ZeroDivisionError: division by zero

        Args:
            a: dividend
            b: divisor

        Raises:
            ZeroDivisionError: gets raised when the divisor is `0`

        Returns:
            the quotient
        """
        if b == 0:
            raise ZeroDivisionError("division by zero")
        return float(a / b)

    def power(
        self, base: Union[float, int], exponent: Union[float, int] = 2.0
    ) -> float:  # pylint: disable=invalid-name
        """
        Do exponentiation of a number.

        Examples:
            >>> power(4.0, 2.0)
            16.0
            >>> power(4, 2)
            16.0
            >>> power(4)
            16.0

        Args:
            base: the base number
            exponent: the exponent used

        Returns:
            the result of taking the base to the exponent
        """
        return float(base**exponent)

    def sqrt(
        self, a: Union[float, int]
    ) -> float:  # pylint: disable=invalid-name
        """
        Do square root of a number.

        Examples:
            >>> sqrt(4.0)
            2.0
            >>> sqrt(4)
            2.0

        Args:
            a: the number that you want to take the square root of

        Raises:
            ValueError: raises if `a` is below `0`

        Returns:
            the square root of `a`
        """
        if a < 0:
            raise ValueError("math domain error")
        return float(a ** (1 / 2))

add(a, b)

Do addition of two numbers.

Examples:

>>> add(4.0, 2.0)
6.0
>>> add(4, 2)
6.0

Parameters:

Name Type Description Default
a Union[float, int]

first number

required
b Union[float, int]

second number

required

Returns:

Type Description
float

sum of the first and the second number

Source code in src/app/calc.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def add(self, a: Union[float, int], b: Union[float, int]) -> float:
    """
    Do addition of two numbers.

    Examples:
        >>> add(4.0, 2.0)
        6.0
        >>> add(4, 2)
        6.0

    Args:
        a: first number
        b: second number

    Returns:
        sum of the first and the second number
    """
    return float(a + b)

divide(a, b)

Do division of two numbers.

Examples:

>>> divide(4.0, 2.0)
2.0
>>> divide(4, 2)
2.0
>>> divide(4, 0)
Traceback (most recent call last):
...
ZeroDivisionError: division by zero

Parameters:

Name Type Description Default
a Union[float, int]

dividend

required
b Union[float, int]

divisor

required

Raises:

Type Description
ZeroDivisionError

gets raised when the divisor is 0

Returns:

Type Description
float

the quotient

Source code in src/app/calc.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def divide(
    self, a: Union[float, int], b: Union[float, int]
) -> float:  # pylint: disable=invalid-name
    """
    Do division of two numbers.

    Examples:
        >>> divide(4.0, 2.0)
        2.0
        >>> divide(4, 2)
        2.0
        >>> divide(4, 0)
        Traceback (most recent call last):
        ...
        ZeroDivisionError: division by zero

    Args:
        a: dividend
        b: divisor

    Raises:
        ZeroDivisionError: gets raised when the divisor is `0`

    Returns:
        the quotient
    """
    if b == 0:
        raise ZeroDivisionError("division by zero")
    return float(a / b)

multiply(a, b)

Do multiplication of two numbers.

Examples:

>>> multiply(4.0, 2.0)
8.0
>>> multiply(4, 2)
8.0

Parameters:

Name Type Description Default
a Union[float, int]

first number

required
b Union[float, int]

second number

required

Returns:

Type Description
float

the product of the two numbers

Source code in src/app/calc.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def multiply(
    self, a: Union[float, int], b: Union[float, int]
) -> float:  # pylint: disable=invalid-name
    """
    Do multiplication of two numbers.

    Examples:
        >>> multiply(4.0, 2.0)
        8.0
        >>> multiply(4, 2)
        8.0

    Args:
        a: first number
        b: second number

    Returns:
        the product of the two numbers
    """
    return float(a * b)

power(base, exponent=2.0)

Do exponentiation of a number.

Examples:

>>> power(4.0, 2.0)
16.0
>>> power(4, 2)
16.0
>>> power(4)
16.0

Parameters:

Name Type Description Default
base Union[float, int]

the base number

required
exponent Union[float, int]

the exponent used

2.0

Returns:

Type Description
float

the result of taking the base to the exponent

Source code in src/app/calc.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def power(
    self, base: Union[float, int], exponent: Union[float, int] = 2.0
) -> float:  # pylint: disable=invalid-name
    """
    Do exponentiation of a number.

    Examples:
        >>> power(4.0, 2.0)
        16.0
        >>> power(4, 2)
        16.0
        >>> power(4)
        16.0

    Args:
        base: the base number
        exponent: the exponent used

    Returns:
        the result of taking the base to the exponent
    """
    return float(base**exponent)

sqrt(a)

Do square root of a number.

Examples:

>>> sqrt(4.0)
2.0
>>> sqrt(4)
2.0

Parameters:

Name Type Description Default
a Union[float, int]

the number that you want to take the square root of

required

Raises:

Type Description
ValueError

raises if a is below 0

Returns:

Type Description
float

the square root of a

Source code in src/app/calc.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def sqrt(
    self, a: Union[float, int]
) -> float:  # pylint: disable=invalid-name
    """
    Do square root of a number.

    Examples:
        >>> sqrt(4.0)
        2.0
        >>> sqrt(4)
        2.0

    Args:
        a: the number that you want to take the square root of

    Raises:
        ValueError: raises if `a` is below `0`

    Returns:
        the square root of `a`
    """
    if a < 0:
        raise ValueError("math domain error")
    return float(a ** (1 / 2))

subtract(a, b)

Do subtraction of two numbers.

Examples:

>>> subtract(4.0, 2.0)
2.0
>>> subtract(4, 2)
2.0

Parameters:

Name Type Description Default
a Union[float, int]

minuend

required
b Union[float, int]

subtrahend

required

Returns:

Type Description
float

the difference between the minuend minus the subtrahend

Source code in src/app/calc.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def subtract(
    self, a: Union[float, int], b: Union[float, int]
) -> float:  # pylint: disable=invalid-name
    """
    Do subtraction of two numbers.

    Examples:
        >>> subtract(4.0, 2.0)
        2.0
        >>> subtract(4, 2)
        2.0

    Args:
        a: minuend
        b: subtrahend

    Returns:
        the difference between the minuend minus the subtrahend
    """
    return float(a - b)

Main

Main module for the app package, it contains the cmd utility.

add(a, b)

Add two numbers.

Source code in src/app/main.py
20
21
22
23
24
25
@app.command()
def add(a: int, b: int):
    """Add two numbers."""
    result = calc.add(a, b)
    typer.echo(result)
    return result

divide(a, b)

Divide two numbers.

Source code in src/app/main.py
44
45
46
47
48
49
@app.command()
def divide(a: int, b: int):
    """Divide two numbers."""
    result = calc.divide(a, b)
    typer.echo(result)
    return result

multiply(a, b)

Multiply two numbers.

Source code in src/app/main.py
36
37
38
39
40
41
@app.command()
def multiply(a: int, b: int):
    """Multiply two numbers."""
    result = calc.multiply(a, b)
    typer.echo(result)
    return result

power(a, b)

Raise a number to a power.

Source code in src/app/main.py
52
53
54
55
56
57
@app.command()
def power(a: int, b: int):
    """Raise a number to a power."""
    result = calc.power(a, b)
    typer.echo(result)
    return result

sqrt(a)

Calculate the square root of a number.

Source code in src/app/main.py
60
61
62
63
64
65
@app.command()
def sqrt(a: int):
    """Calculate the square root of a number."""
    result = calc.sqrt(a)
    typer.echo(result)
    return result

subtract(a, b)

Subtract two numbers.

Source code in src/app/main.py
28
29
30
31
32
33
@app.command()
def subtract(a: int, b: int):
    """Subtract two numbers."""
    result = calc.subtract(a, b)
    typer.echo(result)
    return result

version()

Show version.

Source code in src/app/main.py
12
13
14
15
16
17
@app.command()
def version():
    """Show version."""
    app_version = metadata.version("app")
    typer.echo(app_version)
    return app_version