Simple Calculator Module
Building a python package using simple calculator program
In this repo you will git to know :
- How to build a python program into package?
- How tp publish your package to PyPi repo?
- How to create unit test and automate testing for python program?
- How to check for code quailty and styling using pylint on python program?
- How to using GitHub Action :
- Publish Python Package
- By GitHub Actions
- Publish a Python Package to PyPI on release.
- Pylint
- By GitHub Actions
- Lint a Python application with pylint.
- Python application
- By GitHub Actions
- Create and test a Python application.
- Python package
- By GitHub Actions
- Create and test a Python package on multiple Python versions.
- Publish Python Package
Program Structure
Displaying Interface Options
# print the calculator interface
print("Select operation::")
print("0. Exit")
print("1. Add (+) ")
print("2. Subtract (-)")
print("3. Multiply (*)")
print("4. Divide (/)")
Interaction with users
while True:
# take input from the user
choice = input("Enter choice(0/1/2/3/4): ")
# check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
x = (input("Enter first number: "))
y = (input("Enter second number: "))
# creating object from class
cal = Calculator(x, y)
if choice == '1':
print("Result: ",cal.add())
elif choice == '2':
print("Result: ",cal.sub())
elif choice == '3':
print("Result: ",cal.mul())
elif choice == '4':
print("Result: ",round(cal.div(),2))
elif choice == '0':
print("Exiting!!")
break
else:
print("Invalid Input, please try again")
Class Definition
class Calculator():
'''
Create a class and using a constructor to initialize values of that class.
'''
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
# This methos add two nubers
def add(self):
return self.x + self.y
# This methos subtracting two numbers
def sub(self):
return self.x - self.y
# This methos multiplying two numbers
def mul(self):
return self.x * self.y
# This methos dividing two numbers
def div(self):
return self.x / self.y
Unit Testing
A Typical directory structure for running tests using pytest
Ref : stackoverflow
The best solution in my opinion is to use the unittest command line interface
which will add the directory to the sys.path so you don't have to (done in the TestLoader class).
For example for a directory structure like this:
cal_project
├── calculator.py
└── test_calculator.py
You can just run:
$ cd new_project
$ python -m pytest test_antigravity
For a directory structure like yours:
new_project
├── antigravity
│ ├── __init__.py # make it a package
│ └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py
And in the test modules inside the test package, you can import the antigravity package and its modules as usual:
# import the package
import antigravity
# import the antigravity module
from antigravity import antigravity
# or an object inside the antigravity module
from antigravity.antigravity import my_object
Running a single test module:
To run a single test module, in this case test_antigravity.py:
$ cd new_project
$ python -m unittest test.test_antigravity
Just reference the test module the same way you import it.
Running a single test case or test method:
Also you can run a single TestCase or a single test method:
$ python -m unittest test.test_antigravity.GravityTestCase
$ python -m unittest test.test_antigravity.GravityTestCase.test_method
Running all tests:
You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py (can be changed with the -p, --pattern flag):
$ cd new_project
$ python -m unittest discover
This will run all the test*.py modules inside the test package.
Metadata
Release files for calculator-mlotfic 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| calculator_mlotfic-0.0.1.tar.gz | 4.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| calculator_mlotfic-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / calculator_mlotfic-0.0.1.tar.gz
| Download URL | calculator_mlotfic-0.0.1.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
482498547d9cf6eaf7367fab9285046a80ca2d0f48f49705eb7665f87e80c10f
|
|
BLAKE2b-256 checksum How to use checksums |
87db8e5e9069e6dde8b08af4fc8443da822bfafd1674038ea03be25f648559f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.12
|
Release files / calculator_mlotfic-0.0.1-py3-none-any.whl
| Download URL | calculator_mlotfic-0.0.1-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b2e92aa301dcd58675b2a1f8a7450ffe3972590fab6f114c243996b287b093cb
|
|
BLAKE2b-256 checksum How to use checksums |
361ab49dc16d77695f8f669dbc450bfe84196377b27ade90c58e5e2fca5a4e05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.12
|