https://github.com/ebukari/comparative
Implement comparison magic methods in one line!
https://github.com/ebukari/comparative
decorators magic-methods
Last synced: 3 months ago
JSON representation
Implement comparison magic methods in one line!
- Host: GitHub
- URL: https://github.com/ebukari/comparative
- Owner: ebukari
- License: gpl-3.0
- Created: 2019-03-16T21:21:15.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-16T22:47:12.000Z (about 7 years ago)
- Last Synced: 2025-12-16T14:19:08.501Z (5 months ago)
- Topics: decorators, magic-methods
- Language: Python
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# comparative
Implement comparison magic methods in one line!
## Usage
```python
from comparative import compare_by
@compare_by("hour", "minute", "second")
class Clock:
def __int__(self, hour=0, minute=0, second=0):
self.hour = hour
self.minute = mintue
self.second = second
def __repr__(self):
return ("Clock(h={0.hour:}, m={0.minute:02d}, "
"s={0.second:02d})").format(self)
def __str__(self):
return "{0.hour:02d}:{0.minute:02d}:{0.second:02d}".format(self)
```
## Just Add Water
```python
>>> clock1 = Clock(8, 0, 0)
>>> clock2 = Clock(8, 0, 1)
>>> clock3 = Clock(8, 0, 0)
>>> clock1 < clock2
True
>>> clock1 == clock2
False
>>> clock1 == clock3
True
```
t@o55bhDr%