-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Z. D. Smith
committed
May 28, 2014
0 parents
commit 13ccce4
Showing
2 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" | ||
Certain types of numerical values don't understand the concept of 0, usually ordinal | ||
Values: years, streets, storeys. | ||
""" | ||
|
||
class nz(int): | ||
def check_for_0(s): | ||
def f(self, other): | ||
method = getattr(super(), s) | ||
if self < 0 and method(other) >= 0: | ||
return type(self)(method(other) + 1) | ||
elif self > 0 and method(other) <= 0: | ||
return type(self)(method(other) - 1) | ||
else: | ||
return type(self)(method(other)) | ||
return f | ||
|
||
def __new__(cls, x): | ||
if x == 0: | ||
raise ValueError("0 is not a valid value") | ||
else: | ||
return super().__new__(cls, x) | ||
|
||
__add__ = check_for_0('__add__') | ||
__sub__ = check_for_0('__sub__') |