Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Properties | Private Member Functions | Private Attributes | Static Private Attributes
test.test_binop.Rat Class Reference
Inheritance diagram for test.test_binop.Rat:
Inheritance graph
[legend]
Collaboration diagram for test.test_binop.Rat:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __repr__
def __str__
def __float__
def __int__
def __add__
def __sub__
def __rsub__
def __mul__
def __truediv__
def __rtruediv__
def __floordiv__
def __rfloordiv__
def __divmod__
def __rdivmod__
def __mod__
def __rmod__
def __eq__
def __ne__

Public Attributes

_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Properties

 num = property(_get_num, None)
 den = property(_get_den, None)

Private Member Functions

def _get_num
def _get_den

Private Attributes

 __num
 __den

Static Private Attributes

list __slots__ = ['_Rat__num', '_Rat__den']
 __radd__ = __add__
 __rmul__ = __mul__

Detailed Description

Rational number implemented as a normalized pair of ints.

Definition at line 28 of file test_binop.py.


Constructor & Destructor Documentation

def test.test_binop.Rat.__init__ (   self,
  num = 0,
  den = 1 
)
Constructor: Rat([num[, den]]).

The arguments must be ints, and default to (0, 1).

Definition at line 34 of file test_binop.py.

00034 
00035     def __init__(self, num=0, den=1):
00036         """Constructor: Rat([num[, den]]).
00037 
00038         The arguments must be ints, and default to (0, 1)."""
00039         if not isint(num):
00040             raise TypeError("Rat numerator must be int (%r)" % num)
00041         if not isint(den):
00042             raise TypeError("Rat denominator must be int (%r)" % den)
00043         # But the zero is always on
00044         if den == 0:
00045             raise ZeroDivisionError("zero denominator")
00046         g = gcd(den, num)
00047         self.__num = int(num//g)
00048         self.__den = int(den//g)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def test.test_binop.Rat.__add__ (   self,
  other 
)
Add two Rats, or a Rat and a number.

Definition at line 81 of file test_binop.py.

00081 
00082     def __add__(self, other):
00083         """Add two Rats, or a Rat and a number."""
00084         if isint(other):
00085             other = Rat(other)
00086         if isRat(other):
00087             return Rat(self.__num*other.__den + other.__num*self.__den,
00088                        self.__den*other.__den)
00089         if isnum(other):
00090             return float(self) + other
00091         return NotImplemented

Here is the call graph for this function:

def test.test_binop.Rat.__divmod__ (   self,
  other 
)
Divide two Rats, returning quotient and remainder.

Definition at line 162 of file test_binop.py.

00162 
00163     def __divmod__(self, other):
00164         """Divide two Rats, returning quotient and remainder."""
00165         if isint(other):
00166             other = Rat(other)
00167         elif not isRat(other):
00168             return NotImplemented
00169         x = self//other
00170         return (x, self - other * x)

Here is the call graph for this function:

def test.test_binop.Rat.__eq__ (   self,
  other 
)
Compare two Rats for equality.

Definition at line 187 of file test_binop.py.

00187 
00188     def __eq__(self, other):
00189         """Compare two Rats for equality."""
00190         if isint(other):
00191             return self.__den == 1 and self.__num == other
00192         if isRat(other):
00193             return self.__num == other.__num and self.__den == other.__den
00194         if isnum(other):
00195             return float(self) == other
00196         return NotImplemented

Here is the call graph for this function:

Convert a Rat to a float.

Definition at line 67 of file test_binop.py.

00067 
00068     def __float__(self):
00069         """Convert a Rat to a float."""
00070         return self.__num*1.0/self.__den

def test.test_binop.Rat.__floordiv__ (   self,
  other 
)
Divide two Rats, returning the floored result.

Definition at line 148 of file test_binop.py.

00148 
00149     def __floordiv__(self, other):
00150         """Divide two Rats, returning the floored result."""
00151         if isint(other):
00152             other = Rat(other)
00153         elif not isRat(other):
00154             return NotImplemented
00155         x = self/other
00156         return x.__num // x.__den

Here is the call graph for this function:

Convert a Rat to an int; self.den must be 1.

Definition at line 71 of file test_binop.py.

00071 
00072     def __int__(self):
00073         """Convert a Rat to an int; self.den must be 1."""
00074         if self.__den == 1:
00075             try:
00076                 return int(self.__num)
00077             except OverflowError:
00078                 raise OverflowError("%s too large to convert to int" %
00079                                       repr(self))
00080         raise ValueError("can't convert %s to int" % repr(self))

def test.test_binop.Rat.__mod__ (   self,
  other 
)
Take one Rat modulo another.

Definition at line 179 of file test_binop.py.

00179 
00180     def __mod__(self, other):
00181         """Take one Rat modulo another."""
00182         return divmod(self, other)[1]

Here is the call graph for this function:

def test.test_binop.Rat.__mul__ (   self,
  other 
)
Multiply two Rats, or a Rat and a number.

Definition at line 116 of file test_binop.py.

00116 
00117     def __mul__(self, other):
00118         """Multiply two Rats, or a Rat and a number."""
00119         if isRat(other):
00120             return Rat(self.__num*other.__num, self.__den*other.__den)
00121         if isint(other):
00122             return Rat(self.__num*other, self.__den)
00123         if isnum(other):
00124             return float(self)*other
00125         return NotImplemented

Here is the call graph for this function:

def test.test_binop.Rat.__ne__ (   self,
  other 
)
Compare two Rats for inequality.

Definition at line 197 of file test_binop.py.

00197 
00198     def __ne__(self, other):
00199         """Compare two Rats for inequality."""
00200         return not self == other

def test.test_binop.Rat.__rdivmod__ (   self,
  other 
)
Divide two Rats, returning quotient and remainder (reversed args).

Definition at line 171 of file test_binop.py.

00171 
00172     def __rdivmod__(self, other):
00173         """Divide two Rats, returning quotient and remainder (reversed args)."""
00174         if isint(other):
00175             other = Rat(other)
00176         elif not isRat(other):
00177             return NotImplemented
00178         return divmod(other, self)

Here is the call graph for this function:

Convert a Rat to an string resembling a Rat constructor call.

Definition at line 59 of file test_binop.py.

00059 
00060     def __repr__(self):
00061         """Convert a Rat to an string resembling a Rat constructor call."""
00062         return "Rat(%d, %d)" % (self.__num, self.__den)

def test.test_binop.Rat.__rfloordiv__ (   self,
  other 
)
Divide two Rats, returning the floored result (reversed args).

Definition at line 157 of file test_binop.py.

00157 
00158     def __rfloordiv__(self, other):
00159         """Divide two Rats, returning the floored result (reversed args)."""
00160         x = other/self
00161         return x.__num // x.__den

def test.test_binop.Rat.__rmod__ (   self,
  other 
)
Take one Rat modulo another (reversed args).

Definition at line 183 of file test_binop.py.

00183 
00184     def __rmod__(self, other):
00185         """Take one Rat modulo another (reversed args)."""
00186         return divmod(other, self)[1]

Here is the call graph for this function:

def test.test_binop.Rat.__rsub__ (   self,
  other 
)
Subtract two Rats, or a Rat and a number (reversed args).

Definition at line 105 of file test_binop.py.

00105 
00106     def __rsub__(self, other):
00107         """Subtract two Rats, or a Rat and a number (reversed args)."""
00108         if isint(other):
00109             other = Rat(other)
00110         if isRat(other):
00111             return Rat(other.__num*self.__den - self.__num*other.__den,
00112                        self.__den*other.__den)
00113         if isnum(other):
00114             return other - float(self)
00115         return NotImplemented

Here is the call graph for this function:

def test.test_binop.Rat.__rtruediv__ (   self,
  other 
)
Divide two Rats, or a Rat and a number (reversed args).

Definition at line 138 of file test_binop.py.

00138 
00139     def __rtruediv__(self, other):
00140         """Divide two Rats, or a Rat and a number (reversed args)."""
00141         if isRat(other):
00142             return Rat(other.__num*self.__den, other.__den*self.__num)
00143         if isint(other):
00144             return Rat(other*self.__den, self.__num)
00145         if isnum(other):
00146             return other / float(self)
00147         return NotImplemented

Here is the call graph for this function:

Convert a Rat to a string resembling a decimal numeric value.

Definition at line 63 of file test_binop.py.

00063 
00064     def __str__(self):
00065         """Convert a Rat to a string resembling a decimal numeric value."""
00066         return str(float(self))

def test.test_binop.Rat.__sub__ (   self,
  other 
)
Subtract two Rats, or a Rat and a number.

Definition at line 94 of file test_binop.py.

00094 
00095     def __sub__(self, other):
00096         """Subtract two Rats, or a Rat and a number."""
00097         if isint(other):
00098             other = Rat(other)
00099         if isRat(other):
00100             return Rat(self.__num*other.__den - other.__num*self.__den,
00101                        self.__den*other.__den)
00102         if isnum(other):
00103             return float(self) - other
00104         return NotImplemented

Here is the call graph for this function:

def test.test_binop.Rat.__truediv__ (   self,
  other 
)
Divide two Rats, or a Rat and a number.

Definition at line 128 of file test_binop.py.

00128 
00129     def __truediv__(self, other):
00130         """Divide two Rats, or a Rat and a number."""
00131         if isRat(other):
00132             return Rat(self.__num*other.__den, self.__den*other.__num)
00133         if isint(other):
00134             return Rat(self.__num, self.__den*other)
00135         if isnum(other):
00136             return float(self) / other
00137         return NotImplemented

Here is the call graph for this function:

def test.test_binop.Rat._get_den (   self) [private]
Accessor function for read-only 'den' attribute of Rat.

Definition at line 54 of file test_binop.py.

00054 
00055     def _get_den(self):
00056         """Accessor function for read-only 'den' attribute of Rat."""
        return self.__den
def test.test_binop.Rat._get_num (   self) [private]
Accessor function for read-only 'num' attribute of Rat.

Definition at line 49 of file test_binop.py.

00049 
00050     def _get_num(self):
00051         """Accessor function for read-only 'num' attribute of Rat."""
        return self.__num

Member Data Documentation

Definition at line 47 of file test_binop.py.

Definition at line 46 of file test_binop.py.

Definition at line 92 of file test_binop.py.

Definition at line 126 of file test_binop.py.

list test.test_binop.Rat.__slots__ = ['_Rat__num', '_Rat__den'] [static, private]

Definition at line 32 of file test_binop.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.


Property Documentation

test.test_binop.Rat.den = property(_get_den, None) [static]

Definition at line 57 of file test_binop.py.

test.test_binop.Rat.num = property(_get_num, None) [static]

Definition at line 52 of file test_binop.py.


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