Fraction datatype implemented using Python magic methods
Project description
Fraction
A lightweight Python library that implements rational numbers from scratch using Python's magic methods and operator overloading.
๐ Overview
Fraction is a pure Python implementation of rational numbers designed to demonstrate Python's object-oriented programming model, operator overloading, and special (magic) methods.
Unlike Python's built-in fractions.Fraction, this project was built from scratch for educational purposes to understand how Python's data model works internally.
โจ Features
- โ Automatic fraction simplification
- โ Denominator validation
- โ Arithmetic operator overloading
- โ Comparison operators
- โ Reverse arithmetic operators
- โ Integer and float conversion
- โ Canonical representation of fractions
- โ Clean and Pythonic API
๐ฆ Installation
Install using pip:
pip install fraction-raj
or install from source:
git clone https://github.com/RajeshPhulwaria06/fraction-package.git
cd fraction-pacakge
pip install .
๐ Quick Start
from fraction import Fraction
a = Fraction(1, 2)
b = Fraction(3, 4)
print(a)
print(b)
print(a + b)
print(a - b)
print(a * b)
print(a / b)
Output
1/2
3/4
5/4
-1/4
3/8
2/3
Arithmetic Operations
from fraction import Fraction
a = Fraction(2,3)
b = Fraction(5,6)
print(a+b)
print(a-b)
print(a*b)
print(a/b)
Comparison Operations
from fraction import Fraction
a = Fraction(1,2)
b = Fraction(2,4)
c = Fraction(3,4)
print(a == b)
print(a < c)
print(c > a)
print(a <= b)
print(c >= b)
Output
True
True
True
True
True
Mixed Integer Operations
from fraction import Fraction
a = Fraction(3,4)
print(a + 2)
print(2 + a)
print(a * 3)
print(3 * a)
print(2 - a)
print(2 / a)
Automatic Simplification
Fraction(4,8)
Output
1/2
Negative Denominator Handling
Fraction(3,-6)
Output
-1/2
Type Conversion
Integer
a = Fraction(7,2)
print(int(a))
Output
3
Float
a = Fraction(1,8)
print(float(a))
Output
0.125
Error Handling
Division by zero denominator
Fraction(5,0)
Raises
ZeroDivisionError:
Denominator cannot be zero.
API
Constructor
Fraction(numerator, denominator)
Supported Operators
| Operation | Supported |
|---|---|
| + | โ |
| - | โ |
| * | โ |
| / | โ |
| == | โ |
| < | โ |
| > | โ |
| <= | โ |
| >= | โ |
| int() | โ |
| float() | โ |
Project Structure
fraction-raj/
โ
โโโ src/
โ โโโ fraction/
โ โโโ __init__.py
โ โโโ fraction.py
โ
โโโ tests/
โ โโโ test_fraction.py
โ
โโโ examples.py
โโโ pyproject.toml
โโโ README.md
โโโ LICENSE
โโโ .gitignore
Motivation
This project was built to gain a deeper understanding of:
- Object-Oriented Programming
- Python Data Model
- Special (Magic) Methods
- Operator Overloading
- Package Development
- Python Packaging (PyPI)
- Writing reusable Python libraries
Instead of relying on Python's built-in fractions module, every core feature was implemented manually as a learning exercise.
Future Improvements
- Hash support (
__hash__) - Boolean conversion (
__bool__) - Unary operators (
__neg__,__abs__) - Decimal interoperability
- Rich documentation
- 100% unit test coverage
- GitHub Actions CI/CD
- Benchmark against Python's
fractions.Fraction
Contributing
Contributions, feature requests, and bug reports are welcome.
- Fork the repository
- Create a feature branch
git checkout -b feature/new-feature
- Commit your changes
git commit -m "Add new feature"
- Push the branch
git push origin feature/new-feature
- Open a Pull Request
Running Tests
Install pytest
pip install pytest
Run
pytest
Author
Rajesh Phulwaria
Research-oriented Software Engineer passionate about Python, AI, backend systems, and building developer tools from scratch.
GitHub: https://github.com/RajeshPhulwaria006/
LinkedIn: https://linkedin.com/in/rajesh-phulwaria-b61093315/
License
This project is licensed under the MIT License.
โญ If you found this project helpful, consider starring the repository to support future development.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fraction_raj-1.0.2.tar.gz.
File metadata
- Download URL: fraction_raj-1.0.2.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
176ea89cf6a9785374e132b7e11ca472f794d2a537053fa3897cee8d01d2d0fd
|
|
| MD5 |
42a87752f0a0f2f9d56fb148a0806d95
|
|
| BLAKE2b-256 |
26893780010758104c21c1add9654a99b3aa691d5c15c41aab348e04f925ee1e
|
File details
Details for the file fraction_raj-1.0.2-py3-none-any.whl.
File metadata
- Download URL: fraction_raj-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06a5e86440a5309a5fec4316c4d5cd345600188dcfa44642e55dbeed8dacc480
|
|
| MD5 |
b78e6af0b8b29b3303ff110851a74657
|
|
| BLAKE2b-256 |
c6714b2876033d0418dc2ec61039f7c95917aaf5b31b5a44491090475ede8cd6
|