a = Noom(5)
b = Noom(6)Noom’s source
noom is python library that defines Noom datatype - an alternative to number type but with limited capabilities. This is developed as part of a learning exercise in literate programmimg - using nbdev and fastcore. The goal here is to create a
noom class whose objects are compatible with common arithmetic (+, -, x, /, //) and comparison(=, < , >, <=, >=) operators.
Noom
Noom (val)
Noom is a number datatype alternative with limited capabilities.
Noom.__add__
Noom.__add__ (oth)
Noom.__sub__
Noom.__sub__ (oth)
Noom.__mul__
Noom.__mul__ (oth)
Noom.__truediv__
Noom.__truediv__ (oth)
Noom.__floordiv__
Noom.__floordiv__ (oth)
Now, we have defined the arithmetic operations for noom objects using Python’s magic methods. Let’s take a look at few examples:
a + b11
a - b-1
a * b30
a / b0.8333333333333334
b // a1
Noom.__eq__
Noom.__eq__ (oth)
Return self==value.
Let’s define a few nooms and look at examples…
p = Noom(5)
q = Noom(5)
r = Noom(6)Here are a couple of example to check equality of noom objects:
p == q, p != q(True, False)
p == r, p != r(False, True)
Equality check can be done for Noom objects only after __eq__ is defined. So now, we can add tests for the arithmetic operators as weel.
test_eq(a + b, Noom(11))We need not define __ne__ , it is auto inferred by python once __eq__ is defined. This is valid for other comparison operators too.
test_ne(a + b, Noom(12))Noom.__lt__
Noom.__lt__ (oth)
Return self<value.
Examples:
p < q, p > q(False, False)
p < r, p > r(True, False)
Noom.__le__
Noom.__le__ (oth)
Return self<=value.
Examples:
p <= q, p >= q(True, True)
p <= r, p >= r(True, False)