Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
fractions.Fraction Class Reference
Inheritance diagram for fractions.Fraction:
Inheritance graph
[legend]
Collaboration diagram for fractions.Fraction:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __new__
def from_float
def from_decimal
def limit_denominator
def numerator
def denominator
def __repr__
def __str__
def __floordiv__
def __rfloordiv__
def __mod__
def __rmod__
def __pow__
def __rpow__
def __pos__
def __neg__
def __abs__
def __trunc__
def __floor__
def __ceil__
def __round__
def __hash__
def __eq__
def __lt__
def __gt__
def __le__
def __ge__
def __bool__
def __reduce__
def __copy__
def __deepcopy__
def __float__
def __divmod__
def __rdivmod__
def __complex__
def real
def imag
def conjugate
def __add__
def __radd__
def __sub__
def __rsub__
def __mul__
def __rmul__
def __truediv__
def __rtruediv__
def __ne__
def __new__
def register
def __instancecheck__
def __subclasscheck__

Private Member Functions

def _operator_fallbacks
def _add
def _sub
def _mul
def _div
def _richcmp

Private Attributes

 _numerator
 _denominator

Static Private Attributes

tuple __slots__ = ('_numerator', '_denominator')

Detailed Description

This class implements rational numbers.

In the two-argument form of the constructor, Fraction(8, 6) will
produce a rational number equivalent to 4/3. Both arguments must
be Rational. The numerator defaults to 0 and the denominator
defaults to 1 so that Fraction(3) == 3 and Fraction() == 0.

Fractions can also be constructed from:

  - numeric strings similar to those accepted by the
    float constructor (for example, '-2.3' or '1e10')

  - strings of the form '123/456'

  - float and Decimal instances

  - other Rational instances (including integers)

Definition at line 49 of file fractions.py.


Member Function Documentation

abs(a)

Reimplemented from numbers.Complex.

Definition at line 485 of file fractions.py.

00485 
00486     def __abs__(a):
00487         """abs(a)"""
00488         return Fraction(abs(a._numerator), a._denominator)

def numbers.Complex.__add__ (   self,
  other 
) [inherited]
self + other

Definition at line 70 of file numbers.py.

00070 
00071     def __add__(self, other):
00072         """self + other"""
00073         raise NotImplementedError

a != 0

Reimplemented from numbers.Complex.

Definition at line 609 of file fractions.py.

00609 
00610     def __bool__(a):
00611         """a != 0"""
00612         return a._numerator != 0

Will be math.ceil(a) in 3.0.

Reimplemented from numbers.Real.

Definition at line 500 of file fractions.py.

00500 
00501     def __ceil__(a):
00502         """Will be math.ceil(a) in 3.0."""
00503         # The negations cleverly convince floordiv to return the ceiling.
00504         return -(-a.numerator // a.denominator)

def numbers.Real.__complex__ (   self) [inherited]
complex(self) == complex(float(self), 0)

Reimplemented from numbers.Complex.

Definition at line 249 of file numbers.py.

00249 
00250     def __complex__(self):
00251         """complex(self) == complex(float(self), 0)"""
00252         return complex(float(self))

Definition at line 618 of file fractions.py.

00618 
00619     def __copy__(self):
00620         if type(self) == Fraction:
00621             return self     # I'm immutable; therefore I am my own clone
00622         return self.__class__(self._numerator, self._denominator)

def fractions.Fraction.__deepcopy__ (   self,
  memo 
)

Definition at line 623 of file fractions.py.

00623 
00624     def __deepcopy__(self, memo):
00625         if type(self) == Fraction:
00626             return self     # My components are also immutable
00627         return self.__class__(self._numerator, self._denominator)
def numbers.Real.__divmod__ (   self,
  other 
) [inherited]
divmod(self, other): The pair (self // other, self % other).

Sometimes this can be computed faster than the pair of
operations.

Definition at line 200 of file numbers.py.

00200 
00201     def __divmod__(self, other):
00202         """divmod(self, other): The pair (self // other, self % other).
00203 
00204         Sometimes this can be computed faster than the pair of
00205         operations.
00206         """
00207         return (self // other, self % other)

def fractions.Fraction.__eq__ (   a,
  b 
)
a == b

Reimplemented from numbers.Complex.

Definition at line 552 of file fractions.py.

00552 
00553     def __eq__(a, b):
00554         """a == b"""
00555         if isinstance(b, numbers.Rational):
00556             return (a._numerator == b.numerator and
00557                     a._denominator == b.denominator)
00558         if isinstance(b, numbers.Complex) and b.imag == 0:
00559             b = b.real
00560         if isinstance(b, float):
00561             if math.isnan(b) or math.isinf(b):
00562                 # comparisons with an infinity or nan should behave in
00563                 # the same way for any finite a, so treat a as zero.
00564                 return 0.0 == b
00565             else:
00566                 return a == a.from_float(b)
00567         else:
00568             # Since a doesn't know how to compare with b, let's give b
00569             # a chance to compare itself with a.
00570             return NotImplemented

def numbers.Rational.__float__ (   self) [inherited]
float(self) = self.numerator / self.denominator

It's important that this conversion use the integer's "true"
division rather than casting one side to float before dividing
so that ratios of huge integers convert without overflowing.

Reimplemented from numbers.Real.

Reimplemented in numbers.Integral.

Definition at line 284 of file numbers.py.

00284 
00285     def __float__(self):
00286         """float(self) = self.numerator / self.denominator
00287 
00288         It's important that this conversion use the integer's "true"
00289         division rather than casting one side to float before dividing
00290         so that ratios of huge integers convert without overflowing.
00291 
00292         """
00293         return self.numerator / self.denominator
00294 

Here is the call graph for this function:

Will be math.floor(a) in 3.0.

Reimplemented from numbers.Real.

Definition at line 496 of file fractions.py.

00496 
00497     def __floor__(a):
00498         """Will be math.floor(a) in 3.0."""
00499         return a.numerator // a.denominator

def fractions.Fraction.__floordiv__ (   a,
  b 
)
a // b

Reimplemented from numbers.Real.

Definition at line 421 of file fractions.py.

00421 
00422     def __floordiv__(a, b):
00423         """a // b"""
00424         return math.floor(a / b)

def fractions.Fraction.__ge__ (   a,
  b 
)
a >= b

Definition at line 605 of file fractions.py.

00605 
00606     def __ge__(a, b):
00607         """a >= b"""
00608         return a._richcmp(b, operator.ge)

def fractions.Fraction.__gt__ (   a,
  b 
)
a > b

Definition at line 597 of file fractions.py.

00597 
00598     def __gt__(a, b):
00599         """a > b"""
00600         return a._richcmp(b, operator.gt)

hash(self)

Definition at line 530 of file fractions.py.

00530 
00531     def __hash__(self):
00532         """hash(self)"""
00533 
00534         # XXX since this method is expensive, consider caching the result
00535 
00536         # In order to make sure that the hash of a Fraction agrees
00537         # with the hash of a numerically equal integer, float or
00538         # Decimal instance, we follow the rules for numeric hashes
00539         # outlined in the documentation.  (See library docs, 'Built-in
00540         # Types').
00541 
00542         # dinv is the inverse of self._denominator modulo the prime
00543         # _PyHASH_MODULUS, or 0 if self._denominator is divisible by
00544         # _PyHASH_MODULUS.
00545         dinv = pow(self._denominator, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
00546         if not dinv:
00547             hash_ = _PyHASH_INF
00548         else:
00549             hash_ = abs(self._numerator) * dinv % _PyHASH_MODULUS
00550         result = hash_ if self >= 0 else -hash_
00551         return -2 if result == -1 else result

Here is the call graph for this function:

def abc.ABCMeta.__instancecheck__ (   cls,
  instance 
) [inherited]
Override for isinstance(instance, cls).

Definition at line 158 of file abc.py.

00158 
00159     def __instancecheck__(cls, instance):
00160         """Override for isinstance(instance, cls)."""
00161         # Inline the cache checking
00162         subclass = instance.__class__
00163         if subclass in cls._abc_cache:
00164             return True
00165         subtype = type(instance)
00166         if subtype is subclass:
00167             if (cls._abc_negative_cache_version ==
00168                 ABCMeta._abc_invalidation_counter and
00169                 subclass in cls._abc_negative_cache):
00170                 return False
00171             # Fall back to the subclass check.
00172             return cls.__subclasscheck__(subclass)
00173         return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

Here is the call graph for this function:

def fractions.Fraction.__le__ (   a,
  b 
)
a <= b

Reimplemented from numbers.Real.

Definition at line 601 of file fractions.py.

00601 
00602     def __le__(a, b):
00603         """a <= b"""
00604         return a._richcmp(b, operator.le)

def fractions.Fraction.__lt__ (   a,
  b 
)
a < b

Reimplemented from numbers.Real.

Definition at line 593 of file fractions.py.

00593 
00594     def __lt__(a, b):
00595         """a < b"""
00596         return a._richcmp(b, operator.lt)

def fractions.Fraction.__mod__ (   a,
  b 
)
a % b

Reimplemented from numbers.Real.

Definition at line 429 of file fractions.py.

00429 
00430     def __mod__(a, b):
00431         """a % b"""
00432         div = a // b
00433         return a - b * div

def numbers.Complex.__mul__ (   self,
  other 
) [inherited]
self * other

Definition at line 98 of file numbers.py.

00098 
00099     def __mul__(self, other):
00100         """self * other"""
00101         raise NotImplementedError

def numbers.Complex.__ne__ (   self,
  other 
) [inherited]
self != other

Definition at line 142 of file numbers.py.

00142 
00143     def __ne__(self, other):
00144         """self != other"""
00145         # The default __ne__ doesn't negate __eq__ until 3.0.
00146         return not (self == other)
00147 
00148 Complex.register(complex)
00149 

-a

Reimplemented from numbers.Complex.

Definition at line 481 of file fractions.py.

00481 
00482     def __neg__(a):
00483         """-a"""
00484         return Fraction(-a._numerator, a._denominator)

def fractions.Fraction.__new__ (   cls,
  numerator = 0,
  denominator = None 
)
Constructs a Rational.

Takes a string like '3/2' or '1.5', another Rational instance, a
numerator/denominator pair, or a float.

Examples
--------

>>> Fraction(10, -8)
Fraction(-5, 4)
>>> Fraction(Fraction(1, 7), 5)
Fraction(1, 35)
>>> Fraction(Fraction(1, 7), Fraction(2, 3))
Fraction(3, 14)
>>> Fraction('314')
Fraction(314, 1)
>>> Fraction('-35/4')
Fraction(-35, 4)
>>> Fraction('3.1415') # conversion from numeric string
Fraction(6283, 2000)
>>> Fraction('-47e-2') # string may include a decimal exponent
Fraction(-47, 100)
>>> Fraction(1.47)  # direct construction from float (exact conversion)
Fraction(6620291452234629, 4503599627370496)
>>> Fraction(2.25)
Fraction(9, 4)
>>> Fraction(Decimal('1.47'))
Fraction(147, 100)

Definition at line 73 of file fractions.py.

00073 
00074     def __new__(cls, numerator=0, denominator=None):
00075         """Constructs a Rational.
00076 
00077         Takes a string like '3/2' or '1.5', another Rational instance, a
00078         numerator/denominator pair, or a float.
00079 
00080         Examples
00081         --------
00082 
00083         >>> Fraction(10, -8)
00084         Fraction(-5, 4)
00085         >>> Fraction(Fraction(1, 7), 5)
00086         Fraction(1, 35)
00087         >>> Fraction(Fraction(1, 7), Fraction(2, 3))
00088         Fraction(3, 14)
00089         >>> Fraction('314')
00090         Fraction(314, 1)
00091         >>> Fraction('-35/4')
00092         Fraction(-35, 4)
00093         >>> Fraction('3.1415') # conversion from numeric string
00094         Fraction(6283, 2000)
00095         >>> Fraction('-47e-2') # string may include a decimal exponent
00096         Fraction(-47, 100)
00097         >>> Fraction(1.47)  # direct construction from float (exact conversion)
00098         Fraction(6620291452234629, 4503599627370496)
00099         >>> Fraction(2.25)
00100         Fraction(9, 4)
00101         >>> Fraction(Decimal('1.47'))
00102         Fraction(147, 100)
00103 
00104         """
00105         self = super(Fraction, cls).__new__(cls)
00106 
00107         if denominator is None:
00108             if isinstance(numerator, numbers.Rational):
00109                 self._numerator = numerator.numerator
00110                 self._denominator = numerator.denominator
00111                 return self
00112 
00113             elif isinstance(numerator, float):
00114                 # Exact conversion from float
00115                 value = Fraction.from_float(numerator)
00116                 self._numerator = value._numerator
00117                 self._denominator = value._denominator
00118                 return self
00119 
00120             elif isinstance(numerator, Decimal):
00121                 value = Fraction.from_decimal(numerator)
00122                 self._numerator = value._numerator
00123                 self._denominator = value._denominator
00124                 return self
00125 
00126             elif isinstance(numerator, str):
00127                 # Handle construction from strings.
00128                 m = _RATIONAL_FORMAT.match(numerator)
00129                 if m is None:
00130                     raise ValueError('Invalid literal for Fraction: %r' %
00131                                      numerator)
00132                 numerator = int(m.group('num') or '0')
00133                 denom = m.group('denom')
00134                 if denom:
00135                     denominator = int(denom)
00136                 else:
00137                     denominator = 1
00138                     decimal = m.group('decimal')
00139                     if decimal:
00140                         scale = 10**len(decimal)
00141                         numerator = numerator * scale + int(decimal)
00142                         denominator *= scale
00143                     exp = m.group('exp')
00144                     if exp:
00145                         exp = int(exp)
00146                         if exp >= 0:
00147                             numerator *= 10**exp
00148                         else:
00149                             denominator *= 10**-exp
00150                 if m.group('sign') == '-':
00151                     numerator = -numerator
00152 
00153             else:
00154                 raise TypeError("argument should be a string "
00155                                 "or a Rational instance")
00156 
00157         elif (isinstance(numerator, numbers.Rational) and
00158             isinstance(denominator, numbers.Rational)):
00159             numerator, denominator = (
00160                 numerator.numerator * denominator.denominator,
00161                 denominator.numerator * numerator.denominator
00162                 )
00163         else:
00164             raise TypeError("both arguments should be "
00165                             "Rational instances")
00166 
00167         if denominator == 0:
00168             raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
00169         g = gcd(numerator, denominator)
00170         self._numerator = numerator // g
00171         self._denominator = denominator // g
00172         return self

def abc.ABCMeta.__new__ (   mcls,
  name,
  bases,
  namespace 
) [inherited]

Definition at line 116 of file abc.py.

00116 
00117     def __new__(mcls, name, bases, namespace):
00118         cls = super().__new__(mcls, name, bases, namespace)
00119         # Compute set of abstract method names
00120         abstracts = {name
00121                      for name, value in namespace.items()
00122                      if getattr(value, "__isabstractmethod__", False)}
00123         for base in bases:
00124             for name in getattr(base, "__abstractmethods__", set()):
00125                 value = getattr(cls, name, None)
00126                 if getattr(value, "__isabstractmethod__", False):
00127                     abstracts.add(name)
00128         cls.__abstractmethods__ = frozenset(abstracts)
00129         # Set up inheritance registry
00130         cls._abc_registry = WeakSet()
00131         cls._abc_cache = WeakSet()
00132         cls._abc_negative_cache = WeakSet()
00133         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00134         return cls

Here is the call graph for this function:

+a: Coerces a subclass instance to Fraction

Reimplemented from numbers.Complex.

Definition at line 477 of file fractions.py.

00477 
00478     def __pos__(a):
00479         """+a: Coerces a subclass instance to Fraction"""
00480         return Fraction(a._numerator, a._denominator)

def fractions.Fraction.__pow__ (   a,
  b 
)
a ** b

If b is not an integer, the result will be a float or complex
since roots are generally irrational. If b is an integer, the
result will be rational.

Reimplemented from numbers.Complex.

Definition at line 439 of file fractions.py.

00439 
00440     def __pow__(a, b):
00441         """a ** b
00442 
00443         If b is not an integer, the result will be a float or complex
00444         since roots are generally irrational. If b is an integer, the
00445         result will be rational.
00446 
00447         """
00448         if isinstance(b, numbers.Rational):
00449             if b.denominator == 1:
00450                 power = b.numerator
00451                 if power >= 0:
00452                     return Fraction(a._numerator ** power,
00453                                     a._denominator ** power)
00454                 else:
00455                     return Fraction(a._denominator ** -power,
00456                                     a._numerator ** -power)
00457             else:
00458                 # A fractional power will generally produce an
00459                 # irrational number.
00460                 return float(a) ** float(b)
00461         else:
00462             return float(a) ** b

def numbers.Complex.__radd__ (   self,
  other 
) [inherited]
other + self

Definition at line 75 of file numbers.py.

00075 
00076     def __radd__(self, other):
00077         """other + self"""
00078         raise NotImplementedError

def numbers.Real.__rdivmod__ (   self,
  other 
) [inherited]
divmod(other, self): The pair (self // other, self % other).

Sometimes this can be computed faster than the pair of
operations.

Definition at line 208 of file numbers.py.

00208 
00209     def __rdivmod__(self, other):
00210         """divmod(other, self): The pair (self // other, self % other).
00211 
00212         Sometimes this can be computed faster than the pair of
00213         operations.
00214         """
00215         return (other // self, other % self)

Definition at line 615 of file fractions.py.

00615 
00616     def __reduce__(self):
00617         return (self.__class__, (str(self),))

repr(self)

Definition at line 274 of file fractions.py.

00274 
00275     def __repr__(self):
00276         """repr(self)"""
00277         return ('Fraction(%s, %s)' % (self._numerator, self._denominator))

def fractions.Fraction.__rfloordiv__ (   b,
  a 
)
a // b

Reimplemented from numbers.Real.

Definition at line 425 of file fractions.py.

00425 
00426     def __rfloordiv__(b, a):
00427         """a // b"""
00428         return math.floor(a / b)

def fractions.Fraction.__rmod__ (   b,
  a 
)
a % b

Reimplemented from numbers.Real.

Definition at line 434 of file fractions.py.

00434 
00435     def __rmod__(b, a):
00436         """a % b"""
00437         div = a // b
00438         return a - b * div

def numbers.Complex.__rmul__ (   self,
  other 
) [inherited]
other * self

Definition at line 103 of file numbers.py.

00103 
00104     def __rmul__(self, other):
00105         """other * self"""
00106         raise NotImplementedError

def fractions.Fraction.__round__ (   self,
  ndigits = None 
)
Will be round(self, ndigits) in 3.0.

Rounds half toward even.

Reimplemented from numbers.Real.

Definition at line 505 of file fractions.py.

00505 
00506     def __round__(self, ndigits=None):
00507         """Will be round(self, ndigits) in 3.0.
00508 
00509         Rounds half toward even.
00510         """
00511         if ndigits is None:
00512             floor, remainder = divmod(self.numerator, self.denominator)
00513             if remainder * 2 < self.denominator:
00514                 return floor
00515             elif remainder * 2 > self.denominator:
00516                 return floor + 1
00517             # Deal with the half case:
00518             elif floor % 2 == 0:
00519                 return floor
00520             else:
00521                 return floor + 1
00522         shift = 10**abs(ndigits)
00523         # See _operator_fallbacks.forward to check that the results of
00524         # these operations will always be Fraction and therefore have
00525         # round().
00526         if ndigits > 0:
00527             return Fraction(round(self * shift), shift)
00528         else:
00529             return Fraction(round(self / shift) * shift)

Here is the call graph for this function:

def fractions.Fraction.__rpow__ (   b,
  a 
)
a ** b

Reimplemented from numbers.Complex.

Definition at line 463 of file fractions.py.

00463 
00464     def __rpow__(b, a):
00465         """a ** b"""
00466         if b._denominator == 1 and b._numerator >= 0:
00467             # If a is an int, keep it that way if possible.
00468             return a ** b._numerator
00469 
00470         if isinstance(a, numbers.Rational):
00471             return Fraction(a.numerator, a.denominator) ** b
00472 
00473         if b._denominator == 1:
00474             return a ** b._numerator
00475 
00476         return a ** float(b)

def numbers.Complex.__rsub__ (   self,
  other 
) [inherited]
other - self

Definition at line 93 of file numbers.py.

00093 
00094     def __rsub__(self, other):
00095         """other - self"""
00096         return -self + other

def numbers.Complex.__rtruediv__ (   self,
  other 
) [inherited]
other / self

Definition at line 113 of file numbers.py.

00113 
00114     def __rtruediv__(self, other):
00115         """other / self"""
00116         raise NotImplementedError

str(self)

Definition at line 278 of file fractions.py.

00278 
00279     def __str__(self):
00280         """str(self)"""
00281         if self._denominator == 1:
00282             return str(self._numerator)
00283         else:
00284             return '%s/%s' % (self._numerator, self._denominator)

def numbers.Complex.__sub__ (   self,
  other 
) [inherited]
self - other

Definition at line 89 of file numbers.py.

00089 
00090     def __sub__(self, other):
00091         """self - other"""
00092         return self + -other

def abc.ABCMeta.__subclasscheck__ (   cls,
  subclass 
) [inherited]
Override for issubclass(subclass, cls).

Definition at line 174 of file abc.py.

00174 
00175     def __subclasscheck__(cls, subclass):
00176         """Override for issubclass(subclass, cls)."""
00177         # Check cache
00178         if subclass in cls._abc_cache:
00179             return True
00180         # Check negative cache; may have to invalidate
00181         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
00182             # Invalidate the negative cache
00183             cls._abc_negative_cache = WeakSet()
00184             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00185         elif subclass in cls._abc_negative_cache:
00186             return False
00187         # Check the subclass hook
00188         ok = cls.__subclasshook__(subclass)
00189         if ok is not NotImplemented:
00190             assert isinstance(ok, bool)
00191             if ok:
00192                 cls._abc_cache.add(subclass)
00193             else:
00194                 cls._abc_negative_cache.add(subclass)
00195             return ok
00196         # Check if it's a direct subclass
00197         if cls in getattr(subclass, '__mro__', ()):
00198             cls._abc_cache.add(subclass)
00199             return True
00200         # Check if it's a subclass of a registered class (recursive)
00201         for rcls in cls._abc_registry:
00202             if issubclass(subclass, rcls):
00203                 cls._abc_cache.add(subclass)
00204                 return True
00205         # Check if it's a subclass of a subclass (recursive)
00206         for scls in cls.__subclasses__():
00207             if issubclass(subclass, scls):
00208                 cls._abc_cache.add(subclass)
00209                 return True
00210         # No dice; update negative cache
00211         cls._abc_negative_cache.add(subclass)
00212         return False

Here is the call graph for this function:

def numbers.Complex.__truediv__ (   self,
  other 
) [inherited]
self / other: Should promote to float when necessary.

Definition at line 108 of file numbers.py.

00108 
00109     def __truediv__(self, other):
00110         """self / other: Should promote to float when necessary."""
00111         raise NotImplementedError

trunc(a)

Reimplemented from numbers.Real.

Definition at line 489 of file fractions.py.

00489 
00490     def __trunc__(a):
00491         """trunc(a)"""
00492         if a._numerator < 0:
00493             return -(-a._numerator // a._denominator)
00494         else:
00495             return a._numerator // a._denominator

def fractions.Fraction._add (   a,
  b 
) [private]
a + b

Definition at line 392 of file fractions.py.

00392 
00393     def _add(a, b):
00394         """a + b"""
00395         return Fraction(a.numerator * b.denominator +
00396                         b.numerator * a.denominator,
00397                         a.denominator * b.denominator)

def fractions.Fraction._div (   a,
  b 
) [private]
a / b

Definition at line 414 of file fractions.py.

00414 
00415     def _div(a, b):
00416         """a / b"""
00417         return Fraction(a.numerator * b.denominator,
00418                         a.denominator * b.numerator)

def fractions.Fraction._mul (   a,
  b 
) [private]
a * b

Definition at line 408 of file fractions.py.

00408 
00409     def _mul(a, b):
00410         """a * b"""
00411         return Fraction(a.numerator * b.numerator, a.denominator * b.denominator)

def fractions.Fraction._operator_fallbacks (   monomorphic_operator,
  fallback_operator 
) [private]
Generates forward and reverse operators given a purely-rational
operator and a function from the operator module.

Use this like:
__op__, __rop__ = _operator_fallbacks(just_rational_op, operator.op)

In general, we want to implement the arithmetic operations so
that mixed-mode operations either call an implementation whose
author knew about the types of both arguments, or convert both
to the nearest built in type and do the operation there. In
Fraction, that means that we define __add__ and __radd__ as:

    def __add__(self, other):
# Both types have numerators/denominator attributes,
# so do the operation directly
if isinstance(other, (int, Fraction)):
    return Fraction(self.numerator * other.denominator +
                    other.numerator * self.denominator,
                    self.denominator * other.denominator)
# float and complex don't have those operations, but we
# know about those types, so special case them.
elif isinstance(other, float):
    return float(self) + other
elif isinstance(other, complex):
    return complex(self) + other
# Let the other type take over.
return NotImplemented

    def __radd__(self, other):
# radd handles more types than add because there's
# nothing left to fall back to.
if isinstance(other, numbers.Rational):
    return Fraction(self.numerator * other.denominator +
                    other.numerator * self.denominator,
                    self.denominator * other.denominator)
elif isinstance(other, Real):
    return float(other) + float(self)
elif isinstance(other, Complex):
    return complex(other) + complex(self)
return NotImplemented


There are 5 different cases for a mixed-type addition on
Fraction. I'll refer to all of the above code that doesn't
refer to Fraction, float, or complex as "boilerplate". 'r'
will be an instance of Fraction, which is a subtype of
Rational (r : Fraction <: Rational), and b : B <:
Complex. The first three involve 'r + b':

    1. If B <: Fraction, int, float, or complex, we handle
       that specially, and all is well.
    2. If Fraction falls back to the boilerplate code, and it
       were to return a value from __add__, we'd miss the
       possibility that B defines a more intelligent __radd__,
       so the boilerplate should return NotImplemented from
       __add__. In particular, we don't handle Rational
       here, even though we could get an exact answer, in case
       the other type wants to do something special.
    3. If B <: Fraction, Python tries B.__radd__ before
       Fraction.__add__. This is ok, because it was
       implemented with knowledge of Fraction, so it can
       handle those instances before delegating to Real or
       Complex.

The next two situations describe 'b + r'. We assume that b
didn't know about Fraction in its implementation, and that it
uses similar boilerplate code:

    4. If B <: Rational, then __radd_ converts both to the
       builtin rational type (hey look, that's us) and
       proceeds.
    5. Otherwise, __radd__ tries to find the nearest common
       base ABC, and fall back to its builtin type. Since this
       class doesn't subclass a concrete type, there's no
       implementation to fall back to, so we need to try as
       hard as possible to return an actual value, or the user
       will get a TypeError.

Definition at line 285 of file fractions.py.

00285 
00286     def _operator_fallbacks(monomorphic_operator, fallback_operator):
00287         """Generates forward and reverse operators given a purely-rational
00288         operator and a function from the operator module.
00289 
00290         Use this like:
00291         __op__, __rop__ = _operator_fallbacks(just_rational_op, operator.op)
00292 
00293         In general, we want to implement the arithmetic operations so
00294         that mixed-mode operations either call an implementation whose
00295         author knew about the types of both arguments, or convert both
00296         to the nearest built in type and do the operation there. In
00297         Fraction, that means that we define __add__ and __radd__ as:
00298 
00299             def __add__(self, other):
00300                 # Both types have numerators/denominator attributes,
00301                 # so do the operation directly
00302                 if isinstance(other, (int, Fraction)):
00303                     return Fraction(self.numerator * other.denominator +
00304                                     other.numerator * self.denominator,
00305                                     self.denominator * other.denominator)
00306                 # float and complex don't have those operations, but we
00307                 # know about those types, so special case them.
00308                 elif isinstance(other, float):
00309                     return float(self) + other
00310                 elif isinstance(other, complex):
00311                     return complex(self) + other
00312                 # Let the other type take over.
00313                 return NotImplemented
00314 
00315             def __radd__(self, other):
00316                 # radd handles more types than add because there's
00317                 # nothing left to fall back to.
00318                 if isinstance(other, numbers.Rational):
00319                     return Fraction(self.numerator * other.denominator +
00320                                     other.numerator * self.denominator,
00321                                     self.denominator * other.denominator)
00322                 elif isinstance(other, Real):
00323                     return float(other) + float(self)
00324                 elif isinstance(other, Complex):
00325                     return complex(other) + complex(self)
00326                 return NotImplemented
00327 
00328 
00329         There are 5 different cases for a mixed-type addition on
00330         Fraction. I'll refer to all of the above code that doesn't
00331         refer to Fraction, float, or complex as "boilerplate". 'r'
00332         will be an instance of Fraction, which is a subtype of
00333         Rational (r : Fraction <: Rational), and b : B <:
00334         Complex. The first three involve 'r + b':
00335 
00336             1. If B <: Fraction, int, float, or complex, we handle
00337                that specially, and all is well.
00338             2. If Fraction falls back to the boilerplate code, and it
00339                were to return a value from __add__, we'd miss the
00340                possibility that B defines a more intelligent __radd__,
00341                so the boilerplate should return NotImplemented from
00342                __add__. In particular, we don't handle Rational
00343                here, even though we could get an exact answer, in case
00344                the other type wants to do something special.
00345             3. If B <: Fraction, Python tries B.__radd__ before
00346                Fraction.__add__. This is ok, because it was
00347                implemented with knowledge of Fraction, so it can
00348                handle those instances before delegating to Real or
00349                Complex.
00350 
00351         The next two situations describe 'b + r'. We assume that b
00352         didn't know about Fraction in its implementation, and that it
00353         uses similar boilerplate code:
00354 
00355             4. If B <: Rational, then __radd_ converts both to the
00356                builtin rational type (hey look, that's us) and
00357                proceeds.
00358             5. Otherwise, __radd__ tries to find the nearest common
00359                base ABC, and fall back to its builtin type. Since this
00360                class doesn't subclass a concrete type, there's no
00361                implementation to fall back to, so we need to try as
00362                hard as possible to return an actual value, or the user
00363                will get a TypeError.
00364 
00365         """
00366         def forward(a, b):
00367             if isinstance(b, (int, Fraction)):
00368                 return monomorphic_operator(a, b)
00369             elif isinstance(b, float):
00370                 return fallback_operator(float(a), b)
00371             elif isinstance(b, complex):
00372                 return fallback_operator(complex(a), b)
00373             else:
00374                 return NotImplemented
00375         forward.__name__ = '__' + fallback_operator.__name__ + '__'
00376         forward.__doc__ = monomorphic_operator.__doc__
00377 
00378         def reverse(b, a):
00379             if isinstance(a, numbers.Rational):
00380                 # Includes ints.
00381                 return monomorphic_operator(a, b)
00382             elif isinstance(a, numbers.Real):
00383                 return fallback_operator(float(a), float(b))
00384             elif isinstance(a, numbers.Complex):
00385                 return fallback_operator(complex(a), complex(b))
00386             else:
00387                 return NotImplemented
00388         reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
00389         reverse.__doc__ = monomorphic_operator.__doc__
00390 
00391         return forward, reverse

def fractions.Fraction._richcmp (   self,
  other,
  op 
) [private]
Helper for comparison operators, for internal use only.

Implement comparison between a Rational instance `self`, and
either another Rational instance or a float `other`.  If
`other` is not a Rational instance or a float, return
NotImplemented. `op` should be one of the six standard
comparison operators.

Definition at line 571 of file fractions.py.

00571 
00572     def _richcmp(self, other, op):
00573         """Helper for comparison operators, for internal use only.
00574 
00575         Implement comparison between a Rational instance `self`, and
00576         either another Rational instance or a float `other`.  If
00577         `other` is not a Rational instance or a float, return
00578         NotImplemented. `op` should be one of the six standard
00579         comparison operators.
00580 
00581         """
00582         # convert other to a Rational instance where reasonable.
00583         if isinstance(other, numbers.Rational):
00584             return op(self._numerator * other.denominator,
00585                       self._denominator * other.numerator)
00586         if isinstance(other, float):
00587             if math.isnan(other) or math.isinf(other):
00588                 return op(0.0, other)
00589             else:
00590                 return op(self, self.from_float(other))
00591         else:
00592             return NotImplemented

Here is the call graph for this function:

def fractions.Fraction._sub (   a,
  b 
) [private]
a - b

Definition at line 400 of file fractions.py.

00400 
00401     def _sub(a, b):
00402         """a - b"""
00403         return Fraction(a.numerator * b.denominator -
00404                         b.numerator * a.denominator,
00405                         a.denominator * b.denominator)

def numbers.Real.conjugate (   self) [inherited]
Conjugate is a no-op for Reals.

Reimplemented from numbers.Complex.

Definition at line 263 of file numbers.py.

00263 
00264     def conjugate(self):
00265         """Conjugate is a no-op for Reals."""
00266         return +self
00267 
00268 Real.register(float)
00269 

Reimplemented from numbers.Rational.

Definition at line 271 of file fractions.py.

00271 
00272     def denominator(a):
00273         return a._denominator

Here is the caller graph for this function:

def fractions.Fraction.from_decimal (   cls,
  dec 
)
Converts a finite Decimal instance to a rational number, exactly.

Definition at line 190 of file fractions.py.

00190 
00191     def from_decimal(cls, dec):
00192         """Converts a finite Decimal instance to a rational number, exactly."""
00193         from decimal import Decimal
00194         if isinstance(dec, numbers.Integral):
00195             dec = Decimal(int(dec))
00196         elif not isinstance(dec, Decimal):
00197             raise TypeError(
00198                 "%s.from_decimal() only takes Decimals, not %r (%s)" %
00199                 (cls.__name__, dec, type(dec).__name__))
00200         if not dec.is_finite():
00201             # Catches infinities and nans.
00202             raise TypeError("Cannot convert %s to %s." % (dec, cls.__name__))
00203         sign, digits, exp = dec.as_tuple()
00204         digits = int(''.join(map(str, digits)))
00205         if sign:
00206             digits = -digits
00207         if exp >= 0:
00208             return cls(digits * 10 ** exp)
00209         else:
00210             return cls(digits, 10 ** -exp)

def fractions.Fraction.from_float (   cls,
  f 
)
Converts a finite float to a rational number, exactly.

Beware that Fraction.from_float(0.3) != Fraction(3, 10).

Definition at line 174 of file fractions.py.

00174 
00175     def from_float(cls, f):
00176         """Converts a finite float to a rational number, exactly.
00177 
00178         Beware that Fraction.from_float(0.3) != Fraction(3, 10).
00179 
00180         """
00181         if isinstance(f, numbers.Integral):
00182             return cls(f)
00183         elif not isinstance(f, float):
00184             raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
00185                             (cls.__name__, f, type(f).__name__))
00186         if math.isnan(f) or math.isinf(f):
00187             raise TypeError("Cannot convert %r to %s." % (f, cls.__name__))
00188         return cls(*f.as_integer_ratio())

Here is the caller graph for this function:

def numbers.Real.imag (   self) [inherited]
Real numbers have no imaginary component.

Reimplemented from numbers.Complex.

Definition at line 259 of file numbers.py.

00259 
00260     def imag(self):
00261         """Real numbers have no imaginary component."""
00262         return 0

def fractions.Fraction.limit_denominator (   self,
  max_denominator = 1000000 
)
Closest Fraction to self with denominator at most max_denominator.

>>> Fraction('3.141592653589793').limit_denominator(10)
Fraction(22, 7)
>>> Fraction('3.141592653589793').limit_denominator(100)
Fraction(311, 99)
>>> Fraction(4321, 8765).limit_denominator(10000)
Fraction(4321, 8765)

Definition at line 211 of file fractions.py.

00211 
00212     def limit_denominator(self, max_denominator=1000000):
00213         """Closest Fraction to self with denominator at most max_denominator.
00214 
00215         >>> Fraction('3.141592653589793').limit_denominator(10)
00216         Fraction(22, 7)
00217         >>> Fraction('3.141592653589793').limit_denominator(100)
00218         Fraction(311, 99)
00219         >>> Fraction(4321, 8765).limit_denominator(10000)
00220         Fraction(4321, 8765)
00221 
00222         """
00223         # Algorithm notes: For any real number x, define a *best upper
00224         # approximation* to x to be a rational number p/q such that:
00225         #
00226         #   (1) p/q >= x, and
00227         #   (2) if p/q > r/s >= x then s > q, for any rational r/s.
00228         #
00229         # Define *best lower approximation* similarly.  Then it can be
00230         # proved that a rational number is a best upper or lower
00231         # approximation to x if, and only if, it is a convergent or
00232         # semiconvergent of the (unique shortest) continued fraction
00233         # associated to x.
00234         #
00235         # To find a best rational approximation with denominator <= M,
00236         # we find the best upper and lower approximations with
00237         # denominator <= M and take whichever of these is closer to x.
00238         # In the event of a tie, the bound with smaller denominator is
00239         # chosen.  If both denominators are equal (which can happen
00240         # only when max_denominator == 1 and self is midway between
00241         # two integers) the lower bound---i.e., the floor of self, is
00242         # taken.
00243 
00244         if max_denominator < 1:
00245             raise ValueError("max_denominator should be at least 1")
00246         if self._denominator <= max_denominator:
00247             return Fraction(self)
00248 
00249         p0, q0, p1, q1 = 0, 1, 1, 0
00250         n, d = self._numerator, self._denominator
00251         while True:
00252             a = n//d
00253             q2 = q0+a*q1
00254             if q2 > max_denominator:
00255                 break
00256             p0, q0, p1, q1 = p1, q1, p0+a*p1, q2
00257             n, d = d, n-a*d
00258 
00259         k = (max_denominator-q0)//q1
00260         bound1 = Fraction(p0+k*p1, q0+k*q1)
00261         bound2 = Fraction(p1, q1)
00262         if abs(bound2 - self) <= abs(bound1-self):
00263             return bound2
00264         else:
00265             return bound1

Reimplemented from numbers.Rational.

Definition at line 267 of file fractions.py.

00267 
00268     def numerator(a):
00269         return a._numerator

Here is the caller graph for this function:

def numbers.Real.real (   self) [inherited]
Real numbers are their real component.

Reimplemented from numbers.Complex.

Definition at line 254 of file numbers.py.

00254 
00255     def real(self):
00256         """Real numbers are their real component."""
00257         return +self

def abc.ABCMeta.register (   cls,
  subclass 
) [inherited]
Register a virtual subclass of an ABC.

Definition at line 135 of file abc.py.

00135 
00136     def register(cls, subclass):
00137         """Register a virtual subclass of an ABC."""
00138         if not isinstance(subclass, type):
00139             raise TypeError("Can only register classes")
00140         if issubclass(subclass, cls):
00141             return  # Already a subclass
00142         # Subtle: test for cycles *after* testing for "already a subclass";
00143         # this means we allow X.register(X) and interpret it as a no-op.
00144         if issubclass(cls, subclass):
00145             # This would create a cycle, which is bad for the algorithm below
00146             raise RuntimeError("Refusing to create an inheritance cycle")
00147         cls._abc_registry.add(subclass)
00148         ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache

Here is the caller graph for this function:


Member Data Documentation

tuple fractions.Fraction.__slots__ = ('_numerator', '_denominator') [static, private]

Reimplemented from numbers.Rational.

Definition at line 70 of file fractions.py.

Definition at line 109 of file fractions.py.

Definition at line 108 of file fractions.py.


The documentation for this class was generated from the following file: