fraction-raj 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fraction_raj-1.0.1/LICENSE.txt +7 -0
- fraction_raj-1.0.1/PKG-INFO +384 -0
- fraction_raj-1.0.1/README.md +366 -0
- fraction_raj-1.0.1/pyproject.toml +38 -0
- fraction_raj-1.0.1/setup.cfg +4 -0
- fraction_raj-1.0.1/src/fraction_raj.egg-info/PKG-INFO +384 -0
- fraction_raj-1.0.1/src/fraction_raj.egg-info/SOURCES.txt +8 -0
- fraction_raj-1.0.1/src/fraction_raj.egg-info/dependency_links.txt +1 -0
- fraction_raj-1.0.1/src/fraction_raj.egg-info/top_level.txt +1 -0
- fraction_raj-1.0.1/test/test_fraction.py +16 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Rajesh Phulwaria
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the βSoftwareβ), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED βAS ISβ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fraction-raj
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Fraction datatype implemented using Python magic methods
|
|
5
|
+
Author: Rajesh Phulwaria
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/RajeshPhulwaria006/fraction-raj
|
|
8
|
+
Project-URL: Repository, https://github.com/RajeshPhulwaria006/fraction-raj
|
|
9
|
+
Project-URL: Issues, https://github.com/RajeshPhulwaria006/fraction-raj/issues
|
|
10
|
+
Keywords: fraction,math,python,magic methods
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE.txt
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Fraction
|
|
20
|
+
|
|
21
|
+
> A lightweight Python library that implements rational numbers from scratch using Python's magic methods and operator overloading.
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+

|
|
25
|
+

|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## π Overview
|
|
30
|
+
|
|
31
|
+
`Fraction` is a pure Python implementation of rational numbers designed to demonstrate Python's object-oriented programming model, operator overloading, and special (magic) methods.
|
|
32
|
+
|
|
33
|
+
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.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## β¨ Features
|
|
38
|
+
|
|
39
|
+
* β
Automatic fraction simplification
|
|
40
|
+
* β
Denominator validation
|
|
41
|
+
* β
Arithmetic operator overloading
|
|
42
|
+
* β
Comparison operators
|
|
43
|
+
* β
Reverse arithmetic operators
|
|
44
|
+
* β
Integer and float conversion
|
|
45
|
+
* β
Canonical representation of fractions
|
|
46
|
+
* β
Clean and Pythonic API
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## π¦ Installation
|
|
51
|
+
|
|
52
|
+
Install using pip:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ fraction-raj
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
or install from source:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
git clone https://github.com/RajeshPhulwaria06/fraction-package.git
|
|
62
|
+
|
|
63
|
+
cd fraction-pacakge
|
|
64
|
+
|
|
65
|
+
pip install .
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## π Quick Start
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from fraction import Fraction
|
|
74
|
+
|
|
75
|
+
a = Fraction(1, 2)
|
|
76
|
+
b = Fraction(3, 4)
|
|
77
|
+
|
|
78
|
+
print(a)
|
|
79
|
+
print(b)
|
|
80
|
+
|
|
81
|
+
print(a + b)
|
|
82
|
+
print(a - b)
|
|
83
|
+
print(a * b)
|
|
84
|
+
print(a / b)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Output
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
1/2
|
|
91
|
+
3/4
|
|
92
|
+
5/4
|
|
93
|
+
-1/4
|
|
94
|
+
3/8
|
|
95
|
+
2/3
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
# Arithmetic Operations
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from fraction import Fraction
|
|
104
|
+
|
|
105
|
+
a = Fraction(2,3)
|
|
106
|
+
b = Fraction(5,6)
|
|
107
|
+
|
|
108
|
+
print(a+b)
|
|
109
|
+
print(a-b)
|
|
110
|
+
print(a*b)
|
|
111
|
+
print(a/b)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
# Comparison Operations
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from fraction import Fraction
|
|
120
|
+
|
|
121
|
+
a = Fraction(1,2)
|
|
122
|
+
b = Fraction(2,4)
|
|
123
|
+
c = Fraction(3,4)
|
|
124
|
+
|
|
125
|
+
print(a == b)
|
|
126
|
+
print(a < c)
|
|
127
|
+
print(c > a)
|
|
128
|
+
print(a <= b)
|
|
129
|
+
print(c >= b)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Output
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
True
|
|
136
|
+
True
|
|
137
|
+
True
|
|
138
|
+
True
|
|
139
|
+
True
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
# Mixed Integer Operations
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from fraction import Fraction
|
|
148
|
+
|
|
149
|
+
a = Fraction(3,4)
|
|
150
|
+
|
|
151
|
+
print(a + 2)
|
|
152
|
+
print(2 + a)
|
|
153
|
+
|
|
154
|
+
print(a * 3)
|
|
155
|
+
print(3 * a)
|
|
156
|
+
|
|
157
|
+
print(2 - a)
|
|
158
|
+
print(2 / a)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
# Automatic Simplification
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
Fraction(4,8)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Output
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
1/2
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
# Negative Denominator Handling
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
Fraction(3,-6)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Output
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
-1/2
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
# Type Conversion
|
|
192
|
+
|
|
193
|
+
## Integer
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
a = Fraction(7,2)
|
|
197
|
+
|
|
198
|
+
print(int(a))
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Output
|
|
202
|
+
|
|
203
|
+
```
|
|
204
|
+
3
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Float
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
a = Fraction(1,8)
|
|
213
|
+
|
|
214
|
+
print(float(a))
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Output
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
0.125
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
# Error Handling
|
|
226
|
+
|
|
227
|
+
Division by zero denominator
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
Fraction(5,0)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Raises
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
ZeroDivisionError:
|
|
237
|
+
Denominator cannot be zero.
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
# API
|
|
243
|
+
|
|
244
|
+
## Constructor
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
Fraction(numerator, denominator)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Supported Operators
|
|
253
|
+
|
|
254
|
+
| Operation | Supported |
|
|
255
|
+
| --------- | --------- |
|
|
256
|
+
| + | β
|
|
|
257
|
+
| - | β
|
|
|
258
|
+
| * | β
|
|
|
259
|
+
| / | β
|
|
|
260
|
+
| == | β
|
|
|
261
|
+
| < | β
|
|
|
262
|
+
| > | β
|
|
|
263
|
+
| <= | β
|
|
|
264
|
+
| >= | β
|
|
|
265
|
+
| int() | β
|
|
|
266
|
+
| float() | β
|
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
# Project Structure
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
fraction-raj/
|
|
274
|
+
β
|
|
275
|
+
βββ src/
|
|
276
|
+
β βββ fraction/
|
|
277
|
+
β βββ __init__.py
|
|
278
|
+
β βββ fraction.py
|
|
279
|
+
β
|
|
280
|
+
βββ tests/
|
|
281
|
+
β βββ test_fraction.py
|
|
282
|
+
β
|
|
283
|
+
βββ examples.py
|
|
284
|
+
βββ pyproject.toml
|
|
285
|
+
βββ README.md
|
|
286
|
+
βββ LICENSE
|
|
287
|
+
βββ .gitignore
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
# Motivation
|
|
293
|
+
|
|
294
|
+
This project was built to gain a deeper understanding of:
|
|
295
|
+
|
|
296
|
+
* Object-Oriented Programming
|
|
297
|
+
* Python Data Model
|
|
298
|
+
* Special (Magic) Methods
|
|
299
|
+
* Operator Overloading
|
|
300
|
+
* Package Development
|
|
301
|
+
* Python Packaging (PyPI)
|
|
302
|
+
* Writing reusable Python libraries
|
|
303
|
+
|
|
304
|
+
Instead of relying on Python's built-in `fractions` module, every core feature was implemented manually as a learning exercise.
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
# Future Improvements
|
|
309
|
+
|
|
310
|
+
* [ ] Hash support (`__hash__`)
|
|
311
|
+
* [ ] Boolean conversion (`__bool__`)
|
|
312
|
+
* [ ] Unary operators (`__neg__`, `__abs__`)
|
|
313
|
+
* [ ] Decimal interoperability
|
|
314
|
+
* [ ] Rich documentation
|
|
315
|
+
* [ ] 100% unit test coverage
|
|
316
|
+
* [ ] GitHub Actions CI/CD
|
|
317
|
+
* [ ] Benchmark against Python's `fractions.Fraction`
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
# Contributing
|
|
322
|
+
|
|
323
|
+
Contributions, feature requests, and bug reports are welcome.
|
|
324
|
+
|
|
325
|
+
1. Fork the repository
|
|
326
|
+
2. Create a feature branch
|
|
327
|
+
|
|
328
|
+
```
|
|
329
|
+
git checkout -b feature/new-feature
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
3. Commit your changes
|
|
333
|
+
|
|
334
|
+
```
|
|
335
|
+
git commit -m "Add new feature"
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
4. Push the branch
|
|
339
|
+
|
|
340
|
+
```
|
|
341
|
+
git push origin feature/new-feature
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
5. Open a Pull Request
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
# Running Tests
|
|
349
|
+
|
|
350
|
+
Install pytest
|
|
351
|
+
|
|
352
|
+
```bash
|
|
353
|
+
pip install pytest
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
Run
|
|
357
|
+
|
|
358
|
+
```bash
|
|
359
|
+
pytest
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
# Author
|
|
365
|
+
|
|
366
|
+
**Rajesh Phulwaria**
|
|
367
|
+
|
|
368
|
+
Research-oriented Software Engineer passionate about Python, AI, backend systems, and building developer tools from scratch.
|
|
369
|
+
|
|
370
|
+
GitHub:
|
|
371
|
+
https://github.com/RajeshPhulwaria006/
|
|
372
|
+
|
|
373
|
+
LinkedIn:
|
|
374
|
+
https://linkedin.com/in/rajesh-phulwaria-b61093315/
|
|
375
|
+
|
|
376
|
+
---
|
|
377
|
+
|
|
378
|
+
# License
|
|
379
|
+
|
|
380
|
+
This project is licensed under the MIT License.
|
|
381
|
+
|
|
382
|
+
---
|
|
383
|
+
|
|
384
|
+
β If you found this project helpful, consider starring the repository to support future development.
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
# Fraction
|
|
2
|
+
|
|
3
|
+
> A lightweight Python library that implements rational numbers from scratch using Python's magic methods and operator overloading.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## π Overview
|
|
12
|
+
|
|
13
|
+
`Fraction` is a pure Python implementation of rational numbers designed to demonstrate Python's object-oriented programming model, operator overloading, and special (magic) methods.
|
|
14
|
+
|
|
15
|
+
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.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## β¨ Features
|
|
20
|
+
|
|
21
|
+
* β
Automatic fraction simplification
|
|
22
|
+
* β
Denominator validation
|
|
23
|
+
* β
Arithmetic operator overloading
|
|
24
|
+
* β
Comparison operators
|
|
25
|
+
* β
Reverse arithmetic operators
|
|
26
|
+
* β
Integer and float conversion
|
|
27
|
+
* β
Canonical representation of fractions
|
|
28
|
+
* β
Clean and Pythonic API
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## π¦ Installation
|
|
33
|
+
|
|
34
|
+
Install using pip:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ fraction-raj
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
or install from source:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
git clone https://github.com/RajeshPhulwaria06/fraction-package.git
|
|
44
|
+
|
|
45
|
+
cd fraction-pacakge
|
|
46
|
+
|
|
47
|
+
pip install .
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## π Quick Start
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from fraction import Fraction
|
|
56
|
+
|
|
57
|
+
a = Fraction(1, 2)
|
|
58
|
+
b = Fraction(3, 4)
|
|
59
|
+
|
|
60
|
+
print(a)
|
|
61
|
+
print(b)
|
|
62
|
+
|
|
63
|
+
print(a + b)
|
|
64
|
+
print(a - b)
|
|
65
|
+
print(a * b)
|
|
66
|
+
print(a / b)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Output
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
1/2
|
|
73
|
+
3/4
|
|
74
|
+
5/4
|
|
75
|
+
-1/4
|
|
76
|
+
3/8
|
|
77
|
+
2/3
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
# Arithmetic Operations
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from fraction import Fraction
|
|
86
|
+
|
|
87
|
+
a = Fraction(2,3)
|
|
88
|
+
b = Fraction(5,6)
|
|
89
|
+
|
|
90
|
+
print(a+b)
|
|
91
|
+
print(a-b)
|
|
92
|
+
print(a*b)
|
|
93
|
+
print(a/b)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
# Comparison Operations
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from fraction import Fraction
|
|
102
|
+
|
|
103
|
+
a = Fraction(1,2)
|
|
104
|
+
b = Fraction(2,4)
|
|
105
|
+
c = Fraction(3,4)
|
|
106
|
+
|
|
107
|
+
print(a == b)
|
|
108
|
+
print(a < c)
|
|
109
|
+
print(c > a)
|
|
110
|
+
print(a <= b)
|
|
111
|
+
print(c >= b)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Output
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
True
|
|
118
|
+
True
|
|
119
|
+
True
|
|
120
|
+
True
|
|
121
|
+
True
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
# Mixed Integer Operations
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from fraction import Fraction
|
|
130
|
+
|
|
131
|
+
a = Fraction(3,4)
|
|
132
|
+
|
|
133
|
+
print(a + 2)
|
|
134
|
+
print(2 + a)
|
|
135
|
+
|
|
136
|
+
print(a * 3)
|
|
137
|
+
print(3 * a)
|
|
138
|
+
|
|
139
|
+
print(2 - a)
|
|
140
|
+
print(2 / a)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
# Automatic Simplification
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
Fraction(4,8)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Output
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
1/2
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
# Negative Denominator Handling
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
Fraction(3,-6)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Output
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
-1/2
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
# Type Conversion
|
|
174
|
+
|
|
175
|
+
## Integer
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
a = Fraction(7,2)
|
|
179
|
+
|
|
180
|
+
print(int(a))
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Output
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
3
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Float
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
a = Fraction(1,8)
|
|
195
|
+
|
|
196
|
+
print(float(a))
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Output
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
0.125
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
# Error Handling
|
|
208
|
+
|
|
209
|
+
Division by zero denominator
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
Fraction(5,0)
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Raises
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
ZeroDivisionError:
|
|
219
|
+
Denominator cannot be zero.
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
# API
|
|
225
|
+
|
|
226
|
+
## Constructor
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
Fraction(numerator, denominator)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Supported Operators
|
|
235
|
+
|
|
236
|
+
| Operation | Supported |
|
|
237
|
+
| --------- | --------- |
|
|
238
|
+
| + | β
|
|
|
239
|
+
| - | β
|
|
|
240
|
+
| * | β
|
|
|
241
|
+
| / | β
|
|
|
242
|
+
| == | β
|
|
|
243
|
+
| < | β
|
|
|
244
|
+
| > | β
|
|
|
245
|
+
| <= | β
|
|
|
246
|
+
| >= | β
|
|
|
247
|
+
| int() | β
|
|
|
248
|
+
| float() | β
|
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
# Project Structure
|
|
253
|
+
|
|
254
|
+
```
|
|
255
|
+
fraction-raj/
|
|
256
|
+
β
|
|
257
|
+
βββ src/
|
|
258
|
+
β βββ fraction/
|
|
259
|
+
β βββ __init__.py
|
|
260
|
+
β βββ fraction.py
|
|
261
|
+
β
|
|
262
|
+
βββ tests/
|
|
263
|
+
β βββ test_fraction.py
|
|
264
|
+
β
|
|
265
|
+
βββ examples.py
|
|
266
|
+
βββ pyproject.toml
|
|
267
|
+
βββ README.md
|
|
268
|
+
βββ LICENSE
|
|
269
|
+
βββ .gitignore
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
# Motivation
|
|
275
|
+
|
|
276
|
+
This project was built to gain a deeper understanding of:
|
|
277
|
+
|
|
278
|
+
* Object-Oriented Programming
|
|
279
|
+
* Python Data Model
|
|
280
|
+
* Special (Magic) Methods
|
|
281
|
+
* Operator Overloading
|
|
282
|
+
* Package Development
|
|
283
|
+
* Python Packaging (PyPI)
|
|
284
|
+
* Writing reusable Python libraries
|
|
285
|
+
|
|
286
|
+
Instead of relying on Python's built-in `fractions` module, every core feature was implemented manually as a learning exercise.
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
# Future Improvements
|
|
291
|
+
|
|
292
|
+
* [ ] Hash support (`__hash__`)
|
|
293
|
+
* [ ] Boolean conversion (`__bool__`)
|
|
294
|
+
* [ ] Unary operators (`__neg__`, `__abs__`)
|
|
295
|
+
* [ ] Decimal interoperability
|
|
296
|
+
* [ ] Rich documentation
|
|
297
|
+
* [ ] 100% unit test coverage
|
|
298
|
+
* [ ] GitHub Actions CI/CD
|
|
299
|
+
* [ ] Benchmark against Python's `fractions.Fraction`
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
# Contributing
|
|
304
|
+
|
|
305
|
+
Contributions, feature requests, and bug reports are welcome.
|
|
306
|
+
|
|
307
|
+
1. Fork the repository
|
|
308
|
+
2. Create a feature branch
|
|
309
|
+
|
|
310
|
+
```
|
|
311
|
+
git checkout -b feature/new-feature
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
3. Commit your changes
|
|
315
|
+
|
|
316
|
+
```
|
|
317
|
+
git commit -m "Add new feature"
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
4. Push the branch
|
|
321
|
+
|
|
322
|
+
```
|
|
323
|
+
git push origin feature/new-feature
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
5. Open a Pull Request
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
# Running Tests
|
|
331
|
+
|
|
332
|
+
Install pytest
|
|
333
|
+
|
|
334
|
+
```bash
|
|
335
|
+
pip install pytest
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
Run
|
|
339
|
+
|
|
340
|
+
```bash
|
|
341
|
+
pytest
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
# Author
|
|
347
|
+
|
|
348
|
+
**Rajesh Phulwaria**
|
|
349
|
+
|
|
350
|
+
Research-oriented Software Engineer passionate about Python, AI, backend systems, and building developer tools from scratch.
|
|
351
|
+
|
|
352
|
+
GitHub:
|
|
353
|
+
https://github.com/RajeshPhulwaria006/
|
|
354
|
+
|
|
355
|
+
LinkedIn:
|
|
356
|
+
https://linkedin.com/in/rajesh-phulwaria-b61093315/
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
# License
|
|
361
|
+
|
|
362
|
+
This project is licensed under the MIT License.
|
|
363
|
+
|
|
364
|
+
---
|
|
365
|
+
|
|
366
|
+
β If you found this project helpful, consider starring the repository to support future development.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fraction-raj"
|
|
3
|
+
version = "1.0.1"
|
|
4
|
+
description = "Fraction datatype implemented using Python magic methods"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
authors = [
|
|
10
|
+
{name = "Rajesh Phulwaria"}
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
license = {text = "MIT"}
|
|
14
|
+
|
|
15
|
+
keywords = [
|
|
16
|
+
"fraction",
|
|
17
|
+
"math",
|
|
18
|
+
"python",
|
|
19
|
+
"magic methods"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Operating System :: OS Independent"
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/RajeshPhulwaria006/fraction-raj"
|
|
30
|
+
Repository = "https://github.com/RajeshPhulwaria006/fraction-raj"
|
|
31
|
+
Issues = "https://github.com/RajeshPhulwaria006/fraction-raj/issues"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["setuptools>=68"]
|
|
35
|
+
build-backend = "setuptools.build_meta"
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.packages.find]
|
|
38
|
+
where = ["src"]
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fraction-raj
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Fraction datatype implemented using Python magic methods
|
|
5
|
+
Author: Rajesh Phulwaria
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/RajeshPhulwaria006/fraction-raj
|
|
8
|
+
Project-URL: Repository, https://github.com/RajeshPhulwaria006/fraction-raj
|
|
9
|
+
Project-URL: Issues, https://github.com/RajeshPhulwaria006/fraction-raj/issues
|
|
10
|
+
Keywords: fraction,math,python,magic methods
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE.txt
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Fraction
|
|
20
|
+
|
|
21
|
+
> A lightweight Python library that implements rational numbers from scratch using Python's magic methods and operator overloading.
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+

|
|
25
|
+

|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## π Overview
|
|
30
|
+
|
|
31
|
+
`Fraction` is a pure Python implementation of rational numbers designed to demonstrate Python's object-oriented programming model, operator overloading, and special (magic) methods.
|
|
32
|
+
|
|
33
|
+
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.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## β¨ Features
|
|
38
|
+
|
|
39
|
+
* β
Automatic fraction simplification
|
|
40
|
+
* β
Denominator validation
|
|
41
|
+
* β
Arithmetic operator overloading
|
|
42
|
+
* β
Comparison operators
|
|
43
|
+
* β
Reverse arithmetic operators
|
|
44
|
+
* β
Integer and float conversion
|
|
45
|
+
* β
Canonical representation of fractions
|
|
46
|
+
* β
Clean and Pythonic API
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## π¦ Installation
|
|
51
|
+
|
|
52
|
+
Install using pip:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ fraction-raj
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
or install from source:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
git clone https://github.com/RajeshPhulwaria06/fraction-package.git
|
|
62
|
+
|
|
63
|
+
cd fraction-pacakge
|
|
64
|
+
|
|
65
|
+
pip install .
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## π Quick Start
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from fraction import Fraction
|
|
74
|
+
|
|
75
|
+
a = Fraction(1, 2)
|
|
76
|
+
b = Fraction(3, 4)
|
|
77
|
+
|
|
78
|
+
print(a)
|
|
79
|
+
print(b)
|
|
80
|
+
|
|
81
|
+
print(a + b)
|
|
82
|
+
print(a - b)
|
|
83
|
+
print(a * b)
|
|
84
|
+
print(a / b)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Output
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
1/2
|
|
91
|
+
3/4
|
|
92
|
+
5/4
|
|
93
|
+
-1/4
|
|
94
|
+
3/8
|
|
95
|
+
2/3
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
# Arithmetic Operations
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from fraction import Fraction
|
|
104
|
+
|
|
105
|
+
a = Fraction(2,3)
|
|
106
|
+
b = Fraction(5,6)
|
|
107
|
+
|
|
108
|
+
print(a+b)
|
|
109
|
+
print(a-b)
|
|
110
|
+
print(a*b)
|
|
111
|
+
print(a/b)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
# Comparison Operations
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from fraction import Fraction
|
|
120
|
+
|
|
121
|
+
a = Fraction(1,2)
|
|
122
|
+
b = Fraction(2,4)
|
|
123
|
+
c = Fraction(3,4)
|
|
124
|
+
|
|
125
|
+
print(a == b)
|
|
126
|
+
print(a < c)
|
|
127
|
+
print(c > a)
|
|
128
|
+
print(a <= b)
|
|
129
|
+
print(c >= b)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Output
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
True
|
|
136
|
+
True
|
|
137
|
+
True
|
|
138
|
+
True
|
|
139
|
+
True
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
# Mixed Integer Operations
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from fraction import Fraction
|
|
148
|
+
|
|
149
|
+
a = Fraction(3,4)
|
|
150
|
+
|
|
151
|
+
print(a + 2)
|
|
152
|
+
print(2 + a)
|
|
153
|
+
|
|
154
|
+
print(a * 3)
|
|
155
|
+
print(3 * a)
|
|
156
|
+
|
|
157
|
+
print(2 - a)
|
|
158
|
+
print(2 / a)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
# Automatic Simplification
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
Fraction(4,8)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Output
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
1/2
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
# Negative Denominator Handling
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
Fraction(3,-6)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Output
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
-1/2
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
# Type Conversion
|
|
192
|
+
|
|
193
|
+
## Integer
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
a = Fraction(7,2)
|
|
197
|
+
|
|
198
|
+
print(int(a))
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Output
|
|
202
|
+
|
|
203
|
+
```
|
|
204
|
+
3
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Float
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
a = Fraction(1,8)
|
|
213
|
+
|
|
214
|
+
print(float(a))
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Output
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
0.125
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
# Error Handling
|
|
226
|
+
|
|
227
|
+
Division by zero denominator
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
Fraction(5,0)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Raises
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
ZeroDivisionError:
|
|
237
|
+
Denominator cannot be zero.
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
# API
|
|
243
|
+
|
|
244
|
+
## Constructor
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
Fraction(numerator, denominator)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Supported Operators
|
|
253
|
+
|
|
254
|
+
| Operation | Supported |
|
|
255
|
+
| --------- | --------- |
|
|
256
|
+
| + | β
|
|
|
257
|
+
| - | β
|
|
|
258
|
+
| * | β
|
|
|
259
|
+
| / | β
|
|
|
260
|
+
| == | β
|
|
|
261
|
+
| < | β
|
|
|
262
|
+
| > | β
|
|
|
263
|
+
| <= | β
|
|
|
264
|
+
| >= | β
|
|
|
265
|
+
| int() | β
|
|
|
266
|
+
| float() | β
|
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
# Project Structure
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
fraction-raj/
|
|
274
|
+
β
|
|
275
|
+
βββ src/
|
|
276
|
+
β βββ fraction/
|
|
277
|
+
β βββ __init__.py
|
|
278
|
+
β βββ fraction.py
|
|
279
|
+
β
|
|
280
|
+
βββ tests/
|
|
281
|
+
β βββ test_fraction.py
|
|
282
|
+
β
|
|
283
|
+
βββ examples.py
|
|
284
|
+
βββ pyproject.toml
|
|
285
|
+
βββ README.md
|
|
286
|
+
βββ LICENSE
|
|
287
|
+
βββ .gitignore
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
# Motivation
|
|
293
|
+
|
|
294
|
+
This project was built to gain a deeper understanding of:
|
|
295
|
+
|
|
296
|
+
* Object-Oriented Programming
|
|
297
|
+
* Python Data Model
|
|
298
|
+
* Special (Magic) Methods
|
|
299
|
+
* Operator Overloading
|
|
300
|
+
* Package Development
|
|
301
|
+
* Python Packaging (PyPI)
|
|
302
|
+
* Writing reusable Python libraries
|
|
303
|
+
|
|
304
|
+
Instead of relying on Python's built-in `fractions` module, every core feature was implemented manually as a learning exercise.
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
# Future Improvements
|
|
309
|
+
|
|
310
|
+
* [ ] Hash support (`__hash__`)
|
|
311
|
+
* [ ] Boolean conversion (`__bool__`)
|
|
312
|
+
* [ ] Unary operators (`__neg__`, `__abs__`)
|
|
313
|
+
* [ ] Decimal interoperability
|
|
314
|
+
* [ ] Rich documentation
|
|
315
|
+
* [ ] 100% unit test coverage
|
|
316
|
+
* [ ] GitHub Actions CI/CD
|
|
317
|
+
* [ ] Benchmark against Python's `fractions.Fraction`
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
# Contributing
|
|
322
|
+
|
|
323
|
+
Contributions, feature requests, and bug reports are welcome.
|
|
324
|
+
|
|
325
|
+
1. Fork the repository
|
|
326
|
+
2. Create a feature branch
|
|
327
|
+
|
|
328
|
+
```
|
|
329
|
+
git checkout -b feature/new-feature
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
3. Commit your changes
|
|
333
|
+
|
|
334
|
+
```
|
|
335
|
+
git commit -m "Add new feature"
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
4. Push the branch
|
|
339
|
+
|
|
340
|
+
```
|
|
341
|
+
git push origin feature/new-feature
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
5. Open a Pull Request
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
# Running Tests
|
|
349
|
+
|
|
350
|
+
Install pytest
|
|
351
|
+
|
|
352
|
+
```bash
|
|
353
|
+
pip install pytest
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
Run
|
|
357
|
+
|
|
358
|
+
```bash
|
|
359
|
+
pytest
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
# Author
|
|
365
|
+
|
|
366
|
+
**Rajesh Phulwaria**
|
|
367
|
+
|
|
368
|
+
Research-oriented Software Engineer passionate about Python, AI, backend systems, and building developer tools from scratch.
|
|
369
|
+
|
|
370
|
+
GitHub:
|
|
371
|
+
https://github.com/RajeshPhulwaria006/
|
|
372
|
+
|
|
373
|
+
LinkedIn:
|
|
374
|
+
https://linkedin.com/in/rajesh-phulwaria-b61093315/
|
|
375
|
+
|
|
376
|
+
---
|
|
377
|
+
|
|
378
|
+
# License
|
|
379
|
+
|
|
380
|
+
This project is licensed under the MIT License.
|
|
381
|
+
|
|
382
|
+
---
|
|
383
|
+
|
|
384
|
+
β If you found this project helpful, consider starring the repository to support future development.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from fractions import Fraction
|
|
2
|
+
|
|
3
|
+
def test_add():
|
|
4
|
+
assert Fraction(1,2) + Fraction(1,2) == Fraction(1,1)
|
|
5
|
+
|
|
6
|
+
def test_sub():
|
|
7
|
+
assert Fraction(3,2) - Fraction(1,2) == Fraction(1,1)
|
|
8
|
+
|
|
9
|
+
def test_mul():
|
|
10
|
+
assert Fraction(2,3) * Fraction(3,4) == Fraction(1,2)
|
|
11
|
+
|
|
12
|
+
def test_div():
|
|
13
|
+
assert Fraction(1,2) / Fraction(3,4) == Fraction(2,3)
|
|
14
|
+
|
|
15
|
+
def test_reduce():
|
|
16
|
+
assert Fraction(2,4) == Fraction(1,2)
|