Ñò §ÚêLc@ s“dZddklZddkZddkZddkZddkZddgZeiZd„Z ei dei ƒZ defd„ƒYZ dS( s+Rational, infinite-precision, real numbers.iÿÿÿÿ(tdivisionNtFractiontgcdcC s$x|o|||}}qW|S(s¶Calculate the Greatest Common Divisor of a and b. Unless b==0, the result will have the same sign as b (so that when b is divided by it, the result comes out positive). ((tatb((s!/usr/lib64/python2.6/fractions.pyRss \A\s* # optional whitespace at the start, then (?P[-+]?) # an optional sign, then (?=\d|\.\d) # lookahead for digit or .digit (?P\d*) # numerator (possibly empty) (?: # followed by an optional /(?P\d+) # / and denominator | # or \.(?P\d*) # decimal point and fractional part )? \s*\Z # and optional whitespace to finish cB sßeZdZd(Zddd„Zed„ƒZed„ƒZdd „Ze d „ƒZ e d „ƒZ d „Z d „Z d„Zd„Zeeeiƒ\ZZd„Zeeeiƒ\ZZd„Zeeeiƒ\ZZd„Zeeeiƒ\ZZeeei ƒ\Z!Z"d„Z#d„Z$d„Z%d„Z&d„Z'd„Z(d„Z)d„Z*d„Z+d„Z,d„Z-d„Z.d„Z/d „Z0d!„Z1d"„Z2d#„Z3d$„Z4d%„Z5d&„Z6d'„Z7RS()sŽThis class implements rational numbers. Fraction(8, 6) will produce a rational number equivalent to 4/3. Both arguments must be Integral. 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 strings of the form '[-+]?[0-9]+((/|.)[0-9]+)?', optionally surrounded by spaces. t _numeratort _denominatoriic C s¶tt|ƒi|ƒ}t|ƒttfjo|djo t|tƒoÌ|}ti |ƒ}|d jot d|ƒ‚n|i dƒ}|i dƒ}|o$t||ƒ}dt |ƒ}n)t|ƒ}t|i dƒpdƒ}|i dƒdjo | }qFqJt|tƒo|}|i}|i}qJn|d jotd |ƒ‚nti|ƒ}ti|ƒ}t||ƒ}|||_|||_|S( sˆConstructs a Fraction. Takes a string like '3/2' or '1.5', another Fraction, or a numerator/denominator pair. is Invalid literal for Fraction: %rtnumtdecimali tdenomtsignt-isFraction(%s, 0)N(tsuperRt__new__ttypetinttlongt isinstancet basestringt_RATIONAL_FORMATtmatchtNonet ValueErrortgrouptlentRationalt numeratort denominatortZeroDivisionErrortoperatortindexRRR( tclsRRtselftinputtmRtother_rationaltg((s!/usr/lib64/python2.6/fractions.pyR :s8&      cC s¤t|tiƒo ||ƒSt|tƒp)td|i|t|ƒifƒ‚nti|ƒpti |ƒotd||ifƒ‚n||i ƒŒS(s‚Converts a finite float to a rational number, exactly. Beware that Fraction.from_float(0.3) != Fraction(3, 10). s.%s.from_float() only takes floats, not %r (%s)sCannot convert %r to %s.( RtnumberstIntegraltfloatt TypeErrort__name__Rtmathtisnantisinftas_integer_ratio(Rtf((s!/usr/lib64/python2.6/fractions.pyt from_floatjs # cC sddkl}t|tiƒo|t|ƒƒ}n:t||ƒp)td|i|t|ƒifƒ‚n|i ƒptd||ifƒ‚n|i ƒ\}}}tdi t t |ƒƒƒ}|o | }n|djo||d|ƒS||d| ƒSdS( sAConverts a finite Decimal instance to a rational number, exactly.iÿÿÿÿ(tDecimals2%s.from_decimal() only takes Decimals, not %r (%s)sCannot convert %s to %s.tii N(RR0RR%R&RR(R)Rt is_finitetas_tupletjointmaptstr(RtdecR0R tdigitstexp((s!/usr/lib64/python2.6/fractions.pyt from_decimalzs #   i@Bc C s;|djotdƒ‚n|i|jo t|ƒSd\}}}}|i|i}}xqtoi||}|||} | |joPn|||||| f\}}}}||||}}q`W|||} t|| ||| |ƒ} t||ƒ} t| |ƒt| |ƒjo| S| SdS(sWClosest 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) is$max_denominator should be at least 1iN(iiii(RRRRtTruetabs( R tmax_denominatortp0tq0tp1tq1tntdRtq2tktbound1tbound2((s!/usr/lib64/python2.6/fractions.pytlimit_denominators(    &!cC s|iS(N(R(R((s!/usr/lib64/python2.6/fractions.pyRÇscC s|iS(N(R(R((s!/usr/lib64/python2.6/fractions.pyRËscC sd|i|ifS(s repr(self)sFraction(%s, %s)(RR(R ((s!/usr/lib64/python2.6/fractions.pyt__repr__ÏscC s6|idjot|iƒSd|i|ifSdS(s str(self)is%s/%sN(RR6R(R ((s!/usr/lib64/python2.6/fractions.pyt__str__Ósc sn‡‡fd†}dˆid|_ˆi|_‡‡fd†}dˆid|_ˆi|_||fS(sÓ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, long, 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, 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. c swt|tttfƒoˆ||ƒSt|tƒoˆt|ƒ|ƒSt|tƒoˆt|ƒ|ƒStSdS(N(RRRRR'tcomplextNotImplemented(RR(tfallback_operatortmonomorphic_operator(s!/usr/lib64/python2.6/fractions.pytforward*st__c s€t|tƒoˆ||ƒSt|tiƒoˆt|ƒt|ƒƒSt|tiƒoˆt|ƒt|ƒƒStSdS(N(RRR%tRealR'tComplexRKRL(RR(RMRN(s!/usr/lib64/python2.6/fractions.pytreverse6st__r(R)t__doc__(RNRMRORS((RMRNs!/usr/lib64/python2.6/fractions.pyt_operator_fallbacksÚsP    cC s/t|i|i|i|i|i|iƒS(sa + b(RRR(RR((s!/usr/lib64/python2.6/fractions.pyt_addEscC s/t|i|i|i|i|i|iƒS(sa - b(RRR(RR((s!/usr/lib64/python2.6/fractions.pyt_subMscC s!t|i|i|i|iƒS(sa * b(RRR(RR((s!/usr/lib64/python2.6/fractions.pyt_mulUscC s!t|i|i|i|iƒS(sa / b(RRR(RR((s!/usr/lib64/python2.6/fractions.pyt_div[scC s:||}t|tƒo|i|iSti|ƒSdS(sa // bN(RRRRR*tfloor(RRtdiv((s!/usr/lib64/python2.6/fractions.pyt __floordiv__cs cC s:||}t|tƒo|i|iSti|ƒSdS(sa // bN(RRRRR*R[(RRR\((s!/usr/lib64/python2.6/fractions.pyt __rfloordiv__os cC s||}|||S(sa % b((RRR\((s!/usr/lib64/python2.6/fractions.pyt__mod__{s cC s||}|||S(sa % b((RRR\((s!/usr/lib64/python2.6/fractions.pyt__rmod__€s cC st|tƒo{|idjoS|i}|djot|i||i|ƒSt|i| |i| ƒSq™t|ƒt|ƒSnt|ƒ|SdS(s¾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. iiN(RRRRRRRR'(RRtpower((s!/usr/lib64/python2.6/fractions.pyt__pow__…s   cC s~|idjo|idjo ||iSt|tƒot|i|iƒ|S|idjo ||iS|t|ƒS(sa ** bii(RRRRRRRR'(RR((s!/usr/lib64/python2.6/fractions.pyt__rpow__s   cC st|i|iƒS(s++a: Coerces a subclass instance to Fraction(RRR(R((s!/usr/lib64/python2.6/fractions.pyt__pos__«scC st|i |iƒS(s-a(RRR(R((s!/usr/lib64/python2.6/fractions.pyt__neg__¯scC stt|iƒ|iƒS(sabs(a)(RR<RR(R((s!/usr/lib64/python2.6/fractions.pyt__abs__³scC s3|idjo|i |i S|i|iSdS(strunc(a)iN(RR(R((s!/usr/lib64/python2.6/fractions.pyt __trunc__·scC s\|idjot|iƒS|t|ƒjott|ƒƒSt|i|ifƒSdS(sŒhash(self) Tricky because values that are exactly representable as a float must have the same hash as that float. iN(RthashRR'(R ((s!/usr/lib64/python2.6/fractions.pyt__hash__¾s cC sœt|tƒo$|i|ijo|i|ijSt|tiƒo|idjo |i }nt|t ƒo||i |ƒjSt |ƒ|jSdS(sa == biN( RRRRRRR%RRtimagtrealR'R/(RR((s!/usr/lib64/python2.6/fractions.pyt__eq__Ñs# cC s¦t|tiƒo|idjo |i}nt|tƒo|i|ƒ}ny||}Wntj otSXt|t ƒo||i dƒS||dƒS(sHelper function for comparison operators. Subtracts b from a, exactly if possible, and compares the result with 0 using op, in such a way that the comparison won't recurse. If the difference raises a TypeError, returns NotImplemented instead. i( RR%RRRjRkR'R/R(RLRR(RRtoptdiff((s!/usr/lib64/python2.6/fractions.pyt_subtractAndCompareToZeroàs # cC s|i|tiƒS(sa < b(RoRtlt(RR((s!/usr/lib64/python2.6/fractions.pyt__lt__úscC s|i|tiƒS(sa > b(RoRtgt(RR((s!/usr/lib64/python2.6/fractions.pyt__gt__þscC s|i|tiƒS(sa <= b(RoRtle(RR((s!/usr/lib64/python2.6/fractions.pyt__le__scC s|i|tiƒS(sa >= b(RoRtge(RR((s!/usr/lib64/python2.6/fractions.pyt__ge__scC s |idjS(sa != 0i(R(R((s!/usr/lib64/python2.6/fractions.pyt __nonzero__ scC s|it|ƒffS(N(t __class__R6(R ((s!/usr/lib64/python2.6/fractions.pyt __reduce__scC s.t|ƒtjo|S|i|i|iƒS(N(RRRyRR(R ((s!/usr/lib64/python2.6/fractions.pyt__copy__scC s.t|ƒtjo|S|i|i|iƒS(N(RRRyRR(R tmemo((s!/usr/lib64/python2.6/fractions.pyt __deepcopy__s(s _numerators _denominator(8R)t __module__RUt __slots__R t classmethodR/R:RHtpropertyRRRIRJRVRWRtaddt__add__t__radd__RXtsubt__sub__t__rsub__RYtmult__mul__t__rmul__RZttruedivt __truediv__t __rtruediv__R\t__div__t__rdiv__R]R^R_R`RbRcRdReRfRgRiRlRoRqRsRuRwRxRzR{R}(((s!/usr/lib64/python2.6/fractions.pyR*sR 0 7   k                      (RUt __future__RR*R%Rtret__all__RRtcompiletVERBOSERR(((s!/usr/lib64/python2.6/fractions.pyts