From db8ea5f38058d6d10cd48f544fc03e7689546e9f Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Wed, 21 May 2014 17:35:36 -0400 Subject: [PATCH] Initial commit --- nonzero.py | 25 +++++++++++++++++++++++++ readme.md | 1 + 2 files changed, 26 insertions(+) create mode 100644 nonzero.py create mode 100644 readme.md diff --git a/nonzero.py b/nonzero.py new file mode 100644 index 0000000..3793687 --- /dev/null +++ b/nonzero.py @@ -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__') \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..ab042c9 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +A class to enable non-zero math.