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
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 numberssubtract
- returns the difference of two numbersmultiply
- returns the product of two numbersdivide
- returns the quotient of two numberspower
- returns the base to the power of the exponentsqrt
- 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 |
|
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 |
|
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 |
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 |
|
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 |
|
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 |
|
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 |
Returns:
Type | Description |
---|---|
float
|
the square root of |
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 |
|
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 |
|
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 |
|
divide(a, b)
Divide two numbers.
Source code in src/app/main.py
44 45 46 47 48 49 |
|
multiply(a, b)
Multiply two numbers.
Source code in src/app/main.py
36 37 38 39 40 41 |
|
power(a, b)
Raise a number to a power.
Source code in src/app/main.py
52 53 54 55 56 57 |
|
sqrt(a)
Calculate the square root of a number.
Source code in src/app/main.py
60 61 62 63 64 65 |
|
subtract(a, b)
Subtract two numbers.
Source code in src/app/main.py
28 29 30 31 32 33 |
|
version()
Show version.
Source code in src/app/main.py
12 13 14 15 16 17 |
|