Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added non-inplace math dunders #10

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Changes from 13 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f88e9e8
Added example for context managers
FuexFollets Apr 15, 2023
a84af66
Simplified enter_exit.py
FuexFollets Apr 15, 2023
a684f3c
Rewrote example to include only dunders and exception handling
FuexFollets Apr 15, 2023
0d37d2c
Renamed context class
FuexFollets Apr 16, 2023
785ca03
Added return to `__enter__` method
FuexFollets Apr 16, 2023
2c5963f
Added usage of `ctx` within context
FuexFollets Apr 16, 2023
5abbbf1
Added return type of bool for `__exit__` method
FuexFollets Apr 16, 2023
779628c
Added comments
FuexFollets Apr 16, 2023
42504ed
Update src/feature-reef/enter_exit.py
FuexFollets Apr 16, 2023
a27008b
Merge branch 'trag1c:main' into main
FuexFollets Apr 17, 2023
eb5e7ad
Implemented Number class with all non-implace math dunders
FuexFollets Apr 17, 2023
368e362
Changed container class representation and added additional math dunders
FuexFollets Apr 17, 2023
de32b69
Update dunder listing comment at top
FuexFollets Apr 17, 2023
767def8
Added missing __pow__ showcase
FuexFollets Apr 18, 2023
4f6536d
Fixed wording of print statments
FuexFollets Apr 18, 2023
46ea2ca
Added spaces for linebreak f-strings
FuexFollets Apr 18, 2023
8513e5d
Removed __int__, __float__, and __complex__ as examples
FuexFollets Apr 19, 2023
8573d69
Formatted file with black
FuexFollets Apr 19, 2023
4cb2e8c
Merge branch 'trag1c:main' into main
FuexFollets Apr 19, 2023
289c4de
Merge branch 'trag1c:main' into main
FuexFollets Apr 19, 2023
a37f6d3
Update src/math-land/non_inplace_math.py
FuexFollets Apr 19, 2023
5473888
Formatted files
FuexFollets Apr 19, 2023
f5f8a44
Fixed import statement sorting
FuexFollets Apr 19, 2023
d9f7b1e
Changed land exmple to number wrapper, fixed strings and variable names
FuexFollets May 5, 2023
1740e62
Formatted 'non_inplace_math.py'
FuexFollets May 5, 2023
774e83e
Removed class variable
FuexFollets May 5, 2023
ad63b48
Implemented fraction type for all dunders
FuexFollets Jun 25, 2023
0fa8512
Formatted code
FuexFollets Jun 25, 2023
23de871
Fixed some bugs, added examples
FuexFollets Jun 25, 2023
e31b5da
Reformatted code
FuexFollets Jun 25, 2023
e971e7d
Fixed __mod__
FuexFollets Jun 25, 2023
680ea3a
Changed __mod__
FuexFollets Jun 25, 2023
6c19f85
Fixed __mod__ implementation for fractions
FuexFollets Jun 25, 2023
3b988d4
Formatted code
FuexFollets Jun 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/math-land/non_inplace_math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Dunders __add__, __sub__, __pos__, __neg__,
# __mul__, __floordiv__, __truediv__, __divmod__, __mod__, __int__
# __complex__, __abs__, __trunc__, __pow__

from __future__ import annotations
import math


class Land:
area: int | float = 0
FuexFollets marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, area: int | float) -> None:
self.area = area

def __int__(self) -> int:
return int(self.area)

def __float__(self) -> float:
return float(self.area)

def __add__(self, other: Land) -> Land:
return Land(self.area + other.area)

def __sub__(self, other: Land) -> Land:
return Land(self.area - other.area)

def __pos__(self) -> Land:
return Land(+self.area)

def __neg__(self) -> Land:
return Land(-self.area)

def __abs__(self) -> Land:
return Land(abs(self.area))

def __mul__(self, scalar: int | float) -> Land:
return Land(self.area * scalar)

def __floordiv__(self, dividend: int | float) -> Land:
return Land(self.area // dividend)

def __truediv__(self, dividend: int | float) -> Land:
return Land(self.area / dividend)

def __divmod__(self, divided_and_modded: int | float) -> tuple[Land, Land]:
return (Land(self.area // divided_and_modded),
Land(self.area % divided_and_modded))

def __mod__(self, modded: int | float) -> Land:
return Land(self.area % modded)

def __pow__(self, power: int | float) -> Land:
return Land(self.area ** power)

def __floor__(self) -> Land:
return Land(math.floor(self.area))

def __ceil__(self) -> Land:
return Land(math.ceil(self.area))

def __complex__(self) -> complex:
return complex(self.area, 0)
FuexFollets marked this conversation as resolved.
Show resolved Hide resolved

def __trunc__(self) -> int:
return int(self)

def __str__(self) -> str: # For printing
return str(self.area)


def main():
plot1 = Land(100)
plot2 = Land(-200) # Owes land to the government
plot3 = Land(5.5)

# Calls __int__ which constructs an integer
print(f"plot1 area as intenger: {int(plot1)}")

# Calls __float__ which constructs a float
print(f"plot2 area as float: {float(plot2)}")

print(f"Combined area of plot1, plot2: {plot1 + plot3}") # Calls __add__

# Calls __sub__
print(f"The area of plot1 taken away from plot 2: {plot1 - plot3}")

print(f"Positive of plot1 area +(plot1.area): {+plot1}") # Calls __pos__
print(f"Negative of plot1 area (-plot1): {-plot1}") # Calls __neg__
print("Absolute value plot2 area: {abs(plot2)}") # Calls __abs__
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("Absolute value plot2 area: {abs(plot2)}") # Calls __abs__
print(f"Absolute value plot2 area: {abs(plot2)}") # Calls __abs__

print(f"plot1 area being scaled by 3: {plot1 * 3}") # Calls __mul__

# Calls __floordiv__
print(f"Divisions into plots of land from plot1 of exactly 9 area:"
f"{plot1 // 9}")
print(f"Divides plot2 into 5 pieces: {plot2 / 5}") # Calls __truediv__

divisions, remainder = divmod(plot1, 7) # Calls __divmod__
print(f"plot1 can be divided into {divisions} plots of 7 area each"
f"with a remainder of {remainder} area")

# Calls __mod__
print(f"The remainder of dividing plot1 into 22 pieces is {plot1 % 22}")

# Calls __floor__
print(f"The area of plot3 rounded down is: {math.floor(plot3)}")

# calls __ceil__
print(f"The are of plot3 rounded up is: {math.ceil(plot3)}")

# calls __complex__
print(f"Area of plot3 represented as a complex number {complex(plot3)}")

# calls __trunc__
print(f"The truncated integer area of plot3 {math.trunc(plot3)}")


if __name__ == "__main__":
main()