+++ /dev/null
-# Copyright 2007 Google, Inc. All Rights Reserved.\r
-# Licensed to PSF under a Contributor Agreement.\r
-\r
-"""Abstract Base Classes (ABCs) for numbers, according to PEP 3141.\r
-\r
-TODO: Fill out more detailed documentation on the operators."""\r
-\r
-from __future__ import division\r
-from abc import ABCMeta, abstractmethod, abstractproperty\r
-\r
-__all__ = ["Number", "Complex", "Real", "Rational", "Integral"]\r
-\r
-class Number(object):\r
- """All numbers inherit from this class.\r
-\r
- If you just want to check if an argument x is a number, without\r
- caring what kind, use isinstance(x, Number).\r
- """\r
- __metaclass__ = ABCMeta\r
- __slots__ = ()\r
-\r
- # Concrete numeric types must provide their own hash implementation\r
- __hash__ = None\r
-\r
-\r
-## Notes on Decimal\r
-## ----------------\r
-## Decimal has all of the methods specified by the Real abc, but it should\r
-## not be registered as a Real because decimals do not interoperate with\r
-## binary floats (i.e. Decimal('3.14') + 2.71828 is undefined). But,\r
-## abstract reals are expected to interoperate (i.e. R1 + R2 should be\r
-## expected to work if R1 and R2 are both Reals).\r
-\r
-class Complex(Number):\r
- """Complex defines the operations that work on the builtin complex type.\r
-\r
- In short, those are: a conversion to complex, .real, .imag, +, -,\r
- *, /, abs(), .conjugate, ==, and !=.\r
-\r
- If it is given heterogenous arguments, and doesn't have special\r
- knowledge about them, it should fall back to the builtin complex\r
- type as described below.\r
- """\r
-\r
- __slots__ = ()\r
-\r
- @abstractmethod\r
- def __complex__(self):\r
- """Return a builtin complex instance. Called for complex(self)."""\r
-\r
- # Will be __bool__ in 3.0.\r
- def __nonzero__(self):\r
- """True if self != 0. Called for bool(self)."""\r
- return self != 0\r
-\r
- @abstractproperty\r
- def real(self):\r
- """Retrieve the real component of this number.\r
-\r
- This should subclass Real.\r
- """\r
- raise NotImplementedError\r
-\r
- @abstractproperty\r
- def imag(self):\r
- """Retrieve the imaginary component of this number.\r
-\r
- This should subclass Real.\r
- """\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __add__(self, other):\r
- """self + other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __radd__(self, other):\r
- """other + self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __neg__(self):\r
- """-self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __pos__(self):\r
- """+self"""\r
- raise NotImplementedError\r
-\r
- def __sub__(self, other):\r
- """self - other"""\r
- return self + -other\r
-\r
- def __rsub__(self, other):\r
- """other - self"""\r
- return -self + other\r
-\r
- @abstractmethod\r
- def __mul__(self, other):\r
- """self * other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rmul__(self, other):\r
- """other * self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __div__(self, other):\r
- """self / other without __future__ division\r
-\r
- May promote to float.\r
- """\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rdiv__(self, other):\r
- """other / self without __future__ division"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __truediv__(self, other):\r
- """self / other with __future__ division.\r
-\r
- Should promote to float when necessary.\r
- """\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rtruediv__(self, other):\r
- """other / self with __future__ division"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __pow__(self, exponent):\r
- """self**exponent; should promote to float or complex when necessary."""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rpow__(self, base):\r
- """base ** self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __abs__(self):\r
- """Returns the Real distance from 0. Called for abs(self)."""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def conjugate(self):\r
- """(x+y*i).conjugate() returns (x-y*i)."""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __eq__(self, other):\r
- """self == other"""\r
- raise NotImplementedError\r
-\r
- def __ne__(self, other):\r
- """self != other"""\r
- # The default __ne__ doesn't negate __eq__ until 3.0.\r
- return not (self == other)\r
-\r
-Complex.register(complex)\r
-\r
-\r
-class Real(Complex):\r
- """To Complex, Real adds the operations that work on real numbers.\r
-\r
- In short, those are: a conversion to float, trunc(), divmod,\r
- %, <, <=, >, and >=.\r
-\r
- Real also provides defaults for the derived operations.\r
- """\r
-\r
- __slots__ = ()\r
-\r
- @abstractmethod\r
- def __float__(self):\r
- """Any Real can be converted to a native float object.\r
-\r
- Called for float(self)."""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __trunc__(self):\r
- """trunc(self): Truncates self to an Integral.\r
-\r
- Returns an Integral i such that:\r
- * i>0 iff self>0;\r
- * abs(i) <= abs(self);\r
- * for any Integral j satisfying the first two conditions,\r
- abs(i) >= abs(j) [i.e. i has "maximal" abs among those].\r
- i.e. "truncate towards 0".\r
- """\r
- raise NotImplementedError\r
-\r
- def __divmod__(self, other):\r
- """divmod(self, other): The pair (self // other, self % other).\r
-\r
- Sometimes this can be computed faster than the pair of\r
- operations.\r
- """\r
- return (self // other, self % other)\r
-\r
- def __rdivmod__(self, other):\r
- """divmod(other, self): The pair (self // other, self % other).\r
-\r
- Sometimes this can be computed faster than the pair of\r
- operations.\r
- """\r
- return (other // self, other % self)\r
-\r
- @abstractmethod\r
- def __floordiv__(self, other):\r
- """self // other: The floor() of self/other."""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rfloordiv__(self, other):\r
- """other // self: The floor() of other/self."""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __mod__(self, other):\r
- """self % other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rmod__(self, other):\r
- """other % self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __lt__(self, other):\r
- """self < other\r
-\r
- < on Reals defines a total ordering, except perhaps for NaN."""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __le__(self, other):\r
- """self <= other"""\r
- raise NotImplementedError\r
-\r
- # Concrete implementations of Complex abstract methods.\r
- def __complex__(self):\r
- """complex(self) == complex(float(self), 0)"""\r
- return complex(float(self))\r
-\r
- @property\r
- def real(self):\r
- """Real numbers are their real component."""\r
- return +self\r
-\r
- @property\r
- def imag(self):\r
- """Real numbers have no imaginary component."""\r
- return 0\r
-\r
- def conjugate(self):\r
- """Conjugate is a no-op for Reals."""\r
- return +self\r
-\r
-Real.register(float)\r
-\r
-\r
-class Rational(Real):\r
- """.numerator and .denominator should be in lowest terms."""\r
-\r
- __slots__ = ()\r
-\r
- @abstractproperty\r
- def numerator(self):\r
- raise NotImplementedError\r
-\r
- @abstractproperty\r
- def denominator(self):\r
- raise NotImplementedError\r
-\r
- # Concrete implementation of Real's conversion to float.\r
- def __float__(self):\r
- """float(self) = self.numerator / self.denominator\r
-\r
- It's important that this conversion use the integer's "true"\r
- division rather than casting one side to float before dividing\r
- so that ratios of huge integers convert without overflowing.\r
-\r
- """\r
- return self.numerator / self.denominator\r
-\r
-\r
-class Integral(Rational):\r
- """Integral adds a conversion to long and the bit-string operations."""\r
-\r
- __slots__ = ()\r
-\r
- @abstractmethod\r
- def __long__(self):\r
- """long(self)"""\r
- raise NotImplementedError\r
-\r
- def __index__(self):\r
- """index(self)"""\r
- return long(self)\r
-\r
- @abstractmethod\r
- def __pow__(self, exponent, modulus=None):\r
- """self ** exponent % modulus, but maybe faster.\r
-\r
- Accept the modulus argument if you want to support the\r
- 3-argument version of pow(). Raise a TypeError if exponent < 0\r
- or any argument isn't Integral. Otherwise, just implement the\r
- 2-argument version described in Complex.\r
- """\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __lshift__(self, other):\r
- """self << other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rlshift__(self, other):\r
- """other << self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rshift__(self, other):\r
- """self >> other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rrshift__(self, other):\r
- """other >> self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __and__(self, other):\r
- """self & other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rand__(self, other):\r
- """other & self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __xor__(self, other):\r
- """self ^ other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __rxor__(self, other):\r
- """other ^ self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __or__(self, other):\r
- """self | other"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __ror__(self, other):\r
- """other | self"""\r
- raise NotImplementedError\r
-\r
- @abstractmethod\r
- def __invert__(self):\r
- """~self"""\r
- raise NotImplementedError\r
-\r
- # Concrete implementations of Rational and Real abstract methods.\r
- def __float__(self):\r
- """float(self) == float(long(self))"""\r
- return float(long(self))\r
-\r
- @property\r
- def numerator(self):\r
- """Integers are their own numerators."""\r
- return +self\r
-\r
- @property\r
- def denominator(self):\r
- """Integers have a denominator of 1."""\r
- return 1\r
-\r
-Integral.register(int)\r
-Integral.register(long)\r