Skip to main content

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.

Python License Status


๐Ÿ“– 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.

  1. Fork the repository
  2. Create a feature branch
git checkout -b feature/new-feature
  1. Commit your changes
git commit -m "Add new feature"
  1. Push the branch
git push origin feature/new-feature
  1. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fraction_raj-1.0.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fraction_raj-1.0.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

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

Hashes for fraction_raj-1.0.2.tar.gz
Algorithm Hash digest
SHA256 176ea89cf6a9785374e132b7e11ca472f794d2a537053fa3897cee8d01d2d0fd
MD5 42a87752f0a0f2f9d56fb148a0806d95
BLAKE2b-256 26893780010758104c21c1add9654a99b3aa691d5c15c41aab348e04f925ee1e

See more details on using hashes here.

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

Hashes for fraction_raj-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 06a5e86440a5309a5fec4316c4d5cd345600188dcfa44642e55dbeed8dacc480
MD5 b78e6af0b8b29b3303ff110851a74657
BLAKE2b-256 c6714b2876033d0418dc2ec61039f7c95917aaf5b31b5a44491090475ede8cd6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page