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 --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ 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.1.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.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file fraction_raj-1.0.1.tar.gz.

File metadata

  • Download URL: fraction_raj-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 ccc71cf15004ac2cd1c63217b027aba05d33b40e0e8186688824f3adda69175c
MD5 24407dad4abb8bed581ea9bd5554b0d0
BLAKE2b-256 fc5c2b6caae36f04b769fe75b04a8b1947f5cc4433242a504065fdb5ddf90b66

See more details on using hashes here.

File details

Details for the file fraction_raj-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: fraction_raj-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e9eca61e97fd1636159452bb1dc9f21b093a657b0260000a91b779f112a116bf
MD5 df212d46701724e41e38ae99c7b1a016
BLAKE2b-256 c9f643724d09f5554b1c7ce2d1a07d1c7e9f71e6de75e69cb87d58d4b1749d7f

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