| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | # Copyright 2007 Google, Inc. All Rights Reserved. | 
					
						
							|  |  |  | # Licensed to PSF under a Contributor Agreement. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  | """Abstract Base Classes (ABCs) for numbers, according to PEP 3141.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TODO: Fill out more detailed documentation on the operators."""
 | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 59952-59984 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r59952 | thomas.heller | 2008-01-14 02:35:28 -0800 (Mon, 14 Jan 2008) | 1 line
  Issue 1821: configure libffi for amd64 on FreeeBSD.
........
  r59953 | andrew.kuchling | 2008-01-14 06:48:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Update description of float_info
........
  r59959 | raymond.hettinger | 2008-01-14 14:58:05 -0800 (Mon, 14 Jan 2008) | 1 line
  Fix 1698398:  Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
........
  r59961 | andrew.kuchling | 2008-01-14 17:29:16 -0800 (Mon, 14 Jan 2008) | 1 line
  Typo fixes
........
  r59962 | andrew.kuchling | 2008-01-14 17:29:44 -0800 (Mon, 14 Jan 2008) | 1 line
  Markup fix
........
  r59963 | andrew.kuchling | 2008-01-14 17:47:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Add many items
........
  r59964 | andrew.kuchling | 2008-01-14 17:55:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Repair unfinished sentence
........
  r59967 | raymond.hettinger | 2008-01-14 19:02:37 -0800 (Mon, 14 Jan 2008) | 5 lines
  Issue 1820:  structseq objects did not work with the % formatting operator or isinstance(t, tuple).
  Orignal patch (without tests) by Leif Walsh.
........
  r59968 | raymond.hettinger | 2008-01-14 19:07:42 -0800 (Mon, 14 Jan 2008) | 1 line
  Tighten the definition of a named tuple.
........
  r59969 | skip.montanaro | 2008-01-14 19:40:20 -0800 (Mon, 14 Jan 2008) | 3 lines
  Better (?) text describing the lack of guarantees provided by qsize(),
  empty() and full().
........
  r59970 | raymond.hettinger | 2008-01-14 21:39:59 -0800 (Mon, 14 Jan 2008) | 1 line
  Temporarily revert 59967 until GC can be added.
........
  r59971 | raymond.hettinger | 2008-01-14 21:46:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Small grammar nit
........
  r59972 | georg.brandl | 2008-01-14 22:55:56 -0800 (Mon, 14 Jan 2008) | 2 lines
  Typo.
........
  r59973 | georg.brandl | 2008-01-14 22:58:15 -0800 (Mon, 14 Jan 2008) | 2 lines
  Remove duplicate entry.
........
  r59974 | jeffrey.yasskin | 2008-01-14 23:46:24 -0800 (Mon, 14 Jan 2008) | 12 lines
  Add rational.Rational as an implementation of numbers.Rational with infinite
  precision. This has been discussed at http://bugs.python.org/issue1682. It's
  useful primarily for teaching, but it also demonstrates how to implement a
  member of the numeric tower, including fallbacks for mixed-mode arithmetic.
  I expect to write a couple more patches in this area:
   * Rational.from_decimal()
   * Rational.trim/approximate() (maybe with different names)
   * Maybe remove the parentheses from Rational.__str__()
   * Maybe rename one of the Rational classes
   * Maybe make Rational('3/2') work.
........
  r59978 | andrew.kuchling | 2008-01-15 06:38:05 -0800 (Tue, 15 Jan 2008) | 8 lines
  Restore description of sys.dont_write_bytecode.
  The duplication is intentional -- this paragraph is in a section
  describing additions to the sys module, and there's a later section
  that mentions the switch.  I think most people scan the what's-new and
  don't read it in detail, so a bit of duplication is OK.
........
  r59984 | guido.van.rossum | 2008-01-15 09:59:29 -0800 (Tue, 15 Jan 2008) | 3 lines
  Issue #1786 (by myself): pdb should use its own stdin/stdout around an
  exec call and when creating a recursive instance.
........
											
										 
											2008-01-15 21:44:53 +00:00
										 |  |  | from __future__ import division | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | from abc import ABCMeta, abstractmethod, abstractproperty | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __all__ = ["Number", "Exact", "Inexact", | 
					
						
							|  |  |  |            "Complex", "Real", "Rational", "Integral", | 
					
						
							|  |  |  |            ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Number(metaclass=ABCMeta): | 
					
						
							|  |  |  |     """All numbers inherit from this class.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If you just want to check if an argument x is a number, without | 
					
						
							|  |  |  |     caring what kind, use isinstance(x, Number). | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Exact(Number): | 
					
						
							|  |  |  |     """Operations on instances of this type are exact.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     As long as the result of a homogenous operation is of the same | 
					
						
							|  |  |  |     type, you can assume that it was computed exactly, and there are | 
					
						
							|  |  |  |     no round-off errors. Laws like commutativity and associativity | 
					
						
							|  |  |  |     hold. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Exact.register(int) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Inexact(Number): | 
					
						
							|  |  |  |     """Operations on instances of this type are inexact.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Given X, an instance of Inexact, it is possible that (X + -X) + 3 | 
					
						
							|  |  |  |     == 3, but X + (-X + 3) == 0. The exact form this error takes will | 
					
						
							|  |  |  |     vary by type, but it's generally unsafe to compare this type for | 
					
						
							|  |  |  |     equality. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Inexact.register(complex) | 
					
						
							|  |  |  | Inexact.register(float) | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  | # Inexact.register(decimal.Decimal) | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Complex(Number): | 
					
						
							|  |  |  |     """Complex defines the operations that work on the builtin complex type.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     In short, those are: a conversion to complex, .real, .imag, +, -, | 
					
						
							|  |  |  |     *, /, abs(), .conjugate, ==, and !=. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If it is given heterogenous arguments, and doesn't have special | 
					
						
							|  |  |  |     knowledge about them, it should fall back to the builtin complex | 
					
						
							|  |  |  |     type as described below. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __complex__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """Return a builtin complex instance. Called for complex(self).""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 59952-59984 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r59952 | thomas.heller | 2008-01-14 02:35:28 -0800 (Mon, 14 Jan 2008) | 1 line
  Issue 1821: configure libffi for amd64 on FreeeBSD.
........
  r59953 | andrew.kuchling | 2008-01-14 06:48:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Update description of float_info
........
  r59959 | raymond.hettinger | 2008-01-14 14:58:05 -0800 (Mon, 14 Jan 2008) | 1 line
  Fix 1698398:  Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
........
  r59961 | andrew.kuchling | 2008-01-14 17:29:16 -0800 (Mon, 14 Jan 2008) | 1 line
  Typo fixes
........
  r59962 | andrew.kuchling | 2008-01-14 17:29:44 -0800 (Mon, 14 Jan 2008) | 1 line
  Markup fix
........
  r59963 | andrew.kuchling | 2008-01-14 17:47:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Add many items
........
  r59964 | andrew.kuchling | 2008-01-14 17:55:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Repair unfinished sentence
........
  r59967 | raymond.hettinger | 2008-01-14 19:02:37 -0800 (Mon, 14 Jan 2008) | 5 lines
  Issue 1820:  structseq objects did not work with the % formatting operator or isinstance(t, tuple).
  Orignal patch (without tests) by Leif Walsh.
........
  r59968 | raymond.hettinger | 2008-01-14 19:07:42 -0800 (Mon, 14 Jan 2008) | 1 line
  Tighten the definition of a named tuple.
........
  r59969 | skip.montanaro | 2008-01-14 19:40:20 -0800 (Mon, 14 Jan 2008) | 3 lines
  Better (?) text describing the lack of guarantees provided by qsize(),
  empty() and full().
........
  r59970 | raymond.hettinger | 2008-01-14 21:39:59 -0800 (Mon, 14 Jan 2008) | 1 line
  Temporarily revert 59967 until GC can be added.
........
  r59971 | raymond.hettinger | 2008-01-14 21:46:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Small grammar nit
........
  r59972 | georg.brandl | 2008-01-14 22:55:56 -0800 (Mon, 14 Jan 2008) | 2 lines
  Typo.
........
  r59973 | georg.brandl | 2008-01-14 22:58:15 -0800 (Mon, 14 Jan 2008) | 2 lines
  Remove duplicate entry.
........
  r59974 | jeffrey.yasskin | 2008-01-14 23:46:24 -0800 (Mon, 14 Jan 2008) | 12 lines
  Add rational.Rational as an implementation of numbers.Rational with infinite
  precision. This has been discussed at http://bugs.python.org/issue1682. It's
  useful primarily for teaching, but it also demonstrates how to implement a
  member of the numeric tower, including fallbacks for mixed-mode arithmetic.
  I expect to write a couple more patches in this area:
   * Rational.from_decimal()
   * Rational.trim/approximate() (maybe with different names)
   * Maybe remove the parentheses from Rational.__str__()
   * Maybe rename one of the Rational classes
   * Maybe make Rational('3/2') work.
........
  r59978 | andrew.kuchling | 2008-01-15 06:38:05 -0800 (Tue, 15 Jan 2008) | 8 lines
  Restore description of sys.dont_write_bytecode.
  The duplication is intentional -- this paragraph is in a section
  describing additions to the sys module, and there's a later section
  that mentions the switch.  I think most people scan the what's-new and
  don't read it in detail, so a bit of duplication is OK.
........
  r59984 | guido.van.rossum | 2008-01-15 09:59:29 -0800 (Tue, 15 Jan 2008) | 3 lines
  Issue #1786 (by myself): pdb should use its own stdin/stdout around an
  exec call and when creating a recursive instance.
........
											
										 
											2008-01-15 21:44:53 +00:00
										 |  |  |     # Will be __bool__ in 3.0. | 
					
						
							|  |  |  |     def __nonzero__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """True if self != 0. Called for bool(self).""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return self != 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractproperty | 
					
						
							|  |  |  |     def real(self): | 
					
						
							|  |  |  |         """Retrieve the real component of this number.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This should subclass Real. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractproperty | 
					
						
							|  |  |  |     def imag(self): | 
					
						
							|  |  |  |         """Retrieve the real component of this number.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This should subclass Real. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __add__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self + other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __radd__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other + self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __neg__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """-self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 59952-59984 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r59952 | thomas.heller | 2008-01-14 02:35:28 -0800 (Mon, 14 Jan 2008) | 1 line
  Issue 1821: configure libffi for amd64 on FreeeBSD.
........
  r59953 | andrew.kuchling | 2008-01-14 06:48:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Update description of float_info
........
  r59959 | raymond.hettinger | 2008-01-14 14:58:05 -0800 (Mon, 14 Jan 2008) | 1 line
  Fix 1698398:  Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
........
  r59961 | andrew.kuchling | 2008-01-14 17:29:16 -0800 (Mon, 14 Jan 2008) | 1 line
  Typo fixes
........
  r59962 | andrew.kuchling | 2008-01-14 17:29:44 -0800 (Mon, 14 Jan 2008) | 1 line
  Markup fix
........
  r59963 | andrew.kuchling | 2008-01-14 17:47:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Add many items
........
  r59964 | andrew.kuchling | 2008-01-14 17:55:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Repair unfinished sentence
........
  r59967 | raymond.hettinger | 2008-01-14 19:02:37 -0800 (Mon, 14 Jan 2008) | 5 lines
  Issue 1820:  structseq objects did not work with the % formatting operator or isinstance(t, tuple).
  Orignal patch (without tests) by Leif Walsh.
........
  r59968 | raymond.hettinger | 2008-01-14 19:07:42 -0800 (Mon, 14 Jan 2008) | 1 line
  Tighten the definition of a named tuple.
........
  r59969 | skip.montanaro | 2008-01-14 19:40:20 -0800 (Mon, 14 Jan 2008) | 3 lines
  Better (?) text describing the lack of guarantees provided by qsize(),
  empty() and full().
........
  r59970 | raymond.hettinger | 2008-01-14 21:39:59 -0800 (Mon, 14 Jan 2008) | 1 line
  Temporarily revert 59967 until GC can be added.
........
  r59971 | raymond.hettinger | 2008-01-14 21:46:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Small grammar nit
........
  r59972 | georg.brandl | 2008-01-14 22:55:56 -0800 (Mon, 14 Jan 2008) | 2 lines
  Typo.
........
  r59973 | georg.brandl | 2008-01-14 22:58:15 -0800 (Mon, 14 Jan 2008) | 2 lines
  Remove duplicate entry.
........
  r59974 | jeffrey.yasskin | 2008-01-14 23:46:24 -0800 (Mon, 14 Jan 2008) | 12 lines
  Add rational.Rational as an implementation of numbers.Rational with infinite
  precision. This has been discussed at http://bugs.python.org/issue1682. It's
  useful primarily for teaching, but it also demonstrates how to implement a
  member of the numeric tower, including fallbacks for mixed-mode arithmetic.
  I expect to write a couple more patches in this area:
   * Rational.from_decimal()
   * Rational.trim/approximate() (maybe with different names)
   * Maybe remove the parentheses from Rational.__str__()
   * Maybe rename one of the Rational classes
   * Maybe make Rational('3/2') work.
........
  r59978 | andrew.kuchling | 2008-01-15 06:38:05 -0800 (Tue, 15 Jan 2008) | 8 lines
  Restore description of sys.dont_write_bytecode.
  The duplication is intentional -- this paragraph is in a section
  describing additions to the sys module, and there's a later section
  that mentions the switch.  I think most people scan the what's-new and
  don't read it in detail, so a bit of duplication is OK.
........
  r59984 | guido.van.rossum | 2008-01-15 09:59:29 -0800 (Tue, 15 Jan 2008) | 3 lines
  Issue #1786 (by myself): pdb should use its own stdin/stdout around an
  exec call and when creating a recursive instance.
........
											
										 
											2008-01-15 21:44:53 +00:00
										 |  |  |     @abstractmethod | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |     def __pos__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """+self""" | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __sub__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self - other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return self + -other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __rsub__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other - self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return -self + other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __mul__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self * other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rmul__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other * self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __div__(self, other): | 
					
						
							| 
									
										
											  
											
												Merged revisions 59952-59984 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r59952 | thomas.heller | 2008-01-14 02:35:28 -0800 (Mon, 14 Jan 2008) | 1 line
  Issue 1821: configure libffi for amd64 on FreeeBSD.
........
  r59953 | andrew.kuchling | 2008-01-14 06:48:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Update description of float_info
........
  r59959 | raymond.hettinger | 2008-01-14 14:58:05 -0800 (Mon, 14 Jan 2008) | 1 line
  Fix 1698398:  Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
........
  r59961 | andrew.kuchling | 2008-01-14 17:29:16 -0800 (Mon, 14 Jan 2008) | 1 line
  Typo fixes
........
  r59962 | andrew.kuchling | 2008-01-14 17:29:44 -0800 (Mon, 14 Jan 2008) | 1 line
  Markup fix
........
  r59963 | andrew.kuchling | 2008-01-14 17:47:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Add many items
........
  r59964 | andrew.kuchling | 2008-01-14 17:55:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Repair unfinished sentence
........
  r59967 | raymond.hettinger | 2008-01-14 19:02:37 -0800 (Mon, 14 Jan 2008) | 5 lines
  Issue 1820:  structseq objects did not work with the % formatting operator or isinstance(t, tuple).
  Orignal patch (without tests) by Leif Walsh.
........
  r59968 | raymond.hettinger | 2008-01-14 19:07:42 -0800 (Mon, 14 Jan 2008) | 1 line
  Tighten the definition of a named tuple.
........
  r59969 | skip.montanaro | 2008-01-14 19:40:20 -0800 (Mon, 14 Jan 2008) | 3 lines
  Better (?) text describing the lack of guarantees provided by qsize(),
  empty() and full().
........
  r59970 | raymond.hettinger | 2008-01-14 21:39:59 -0800 (Mon, 14 Jan 2008) | 1 line
  Temporarily revert 59967 until GC can be added.
........
  r59971 | raymond.hettinger | 2008-01-14 21:46:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Small grammar nit
........
  r59972 | georg.brandl | 2008-01-14 22:55:56 -0800 (Mon, 14 Jan 2008) | 2 lines
  Typo.
........
  r59973 | georg.brandl | 2008-01-14 22:58:15 -0800 (Mon, 14 Jan 2008) | 2 lines
  Remove duplicate entry.
........
  r59974 | jeffrey.yasskin | 2008-01-14 23:46:24 -0800 (Mon, 14 Jan 2008) | 12 lines
  Add rational.Rational as an implementation of numbers.Rational with infinite
  precision. This has been discussed at http://bugs.python.org/issue1682. It's
  useful primarily for teaching, but it also demonstrates how to implement a
  member of the numeric tower, including fallbacks for mixed-mode arithmetic.
  I expect to write a couple more patches in this area:
   * Rational.from_decimal()
   * Rational.trim/approximate() (maybe with different names)
   * Maybe remove the parentheses from Rational.__str__()
   * Maybe rename one of the Rational classes
   * Maybe make Rational('3/2') work.
........
  r59978 | andrew.kuchling | 2008-01-15 06:38:05 -0800 (Tue, 15 Jan 2008) | 8 lines
  Restore description of sys.dont_write_bytecode.
  The duplication is intentional -- this paragraph is in a section
  describing additions to the sys module, and there's a later section
  that mentions the switch.  I think most people scan the what's-new and
  don't read it in detail, so a bit of duplication is OK.
........
  r59984 | guido.van.rossum | 2008-01-15 09:59:29 -0800 (Tue, 15 Jan 2008) | 3 lines
  Issue #1786 (by myself): pdb should use its own stdin/stdout around an
  exec call and when creating a recursive instance.
........
											
										 
											2008-01-15 21:44:53 +00:00
										 |  |  |         """self / other without __future__ division
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         May promote to float. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rdiv__(self, other): | 
					
						
							| 
									
										
											  
											
												Merged revisions 59952-59984 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r59952 | thomas.heller | 2008-01-14 02:35:28 -0800 (Mon, 14 Jan 2008) | 1 line
  Issue 1821: configure libffi for amd64 on FreeeBSD.
........
  r59953 | andrew.kuchling | 2008-01-14 06:48:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Update description of float_info
........
  r59959 | raymond.hettinger | 2008-01-14 14:58:05 -0800 (Mon, 14 Jan 2008) | 1 line
  Fix 1698398:  Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
........
  r59961 | andrew.kuchling | 2008-01-14 17:29:16 -0800 (Mon, 14 Jan 2008) | 1 line
  Typo fixes
........
  r59962 | andrew.kuchling | 2008-01-14 17:29:44 -0800 (Mon, 14 Jan 2008) | 1 line
  Markup fix
........
  r59963 | andrew.kuchling | 2008-01-14 17:47:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Add many items
........
  r59964 | andrew.kuchling | 2008-01-14 17:55:32 -0800 (Mon, 14 Jan 2008) | 1 line
  Repair unfinished sentence
........
  r59967 | raymond.hettinger | 2008-01-14 19:02:37 -0800 (Mon, 14 Jan 2008) | 5 lines
  Issue 1820:  structseq objects did not work with the % formatting operator or isinstance(t, tuple).
  Orignal patch (without tests) by Leif Walsh.
........
  r59968 | raymond.hettinger | 2008-01-14 19:07:42 -0800 (Mon, 14 Jan 2008) | 1 line
  Tighten the definition of a named tuple.
........
  r59969 | skip.montanaro | 2008-01-14 19:40:20 -0800 (Mon, 14 Jan 2008) | 3 lines
  Better (?) text describing the lack of guarantees provided by qsize(),
  empty() and full().
........
  r59970 | raymond.hettinger | 2008-01-14 21:39:59 -0800 (Mon, 14 Jan 2008) | 1 line
  Temporarily revert 59967 until GC can be added.
........
  r59971 | raymond.hettinger | 2008-01-14 21:46:43 -0800 (Mon, 14 Jan 2008) | 1 line
  Small grammar nit
........
  r59972 | georg.brandl | 2008-01-14 22:55:56 -0800 (Mon, 14 Jan 2008) | 2 lines
  Typo.
........
  r59973 | georg.brandl | 2008-01-14 22:58:15 -0800 (Mon, 14 Jan 2008) | 2 lines
  Remove duplicate entry.
........
  r59974 | jeffrey.yasskin | 2008-01-14 23:46:24 -0800 (Mon, 14 Jan 2008) | 12 lines
  Add rational.Rational as an implementation of numbers.Rational with infinite
  precision. This has been discussed at http://bugs.python.org/issue1682. It's
  useful primarily for teaching, but it also demonstrates how to implement a
  member of the numeric tower, including fallbacks for mixed-mode arithmetic.
  I expect to write a couple more patches in this area:
   * Rational.from_decimal()
   * Rational.trim/approximate() (maybe with different names)
   * Maybe remove the parentheses from Rational.__str__()
   * Maybe rename one of the Rational classes
   * Maybe make Rational('3/2') work.
........
  r59978 | andrew.kuchling | 2008-01-15 06:38:05 -0800 (Tue, 15 Jan 2008) | 8 lines
  Restore description of sys.dont_write_bytecode.
  The duplication is intentional -- this paragraph is in a section
  describing additions to the sys module, and there's a later section
  that mentions the switch.  I think most people scan the what's-new and
  don't read it in detail, so a bit of duplication is OK.
........
  r59984 | guido.van.rossum | 2008-01-15 09:59:29 -0800 (Tue, 15 Jan 2008) | 3 lines
  Issue #1786 (by myself): pdb should use its own stdin/stdout around an
  exec call and when creating a recursive instance.
........
											
										 
											2008-01-15 21:44:53 +00:00
										 |  |  |         """other / self without __future__ division""" | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __truediv__(self, other): | 
					
						
							|  |  |  |         """self / other with __future__ division.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Should promote to float when necessary. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rtruediv__(self, other): | 
					
						
							|  |  |  |         """other / self with __future__ division""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __pow__(self, exponent): | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  |         """self**exponent; should promote to float or complex when necessary.""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rpow__(self, base): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """base ** self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __abs__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """Returns the Real distance from 0. Called for abs(self).""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def conjugate(self): | 
					
						
							|  |  |  |         """(x+y*i).conjugate() returns (x-y*i).""" | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __eq__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self == other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  |     # __ne__ is inherited from object and negates whatever __eq__ does. | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Complex.register(complex) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Real(Complex): | 
					
						
							|  |  |  |     """To Complex, Real adds the operations that work on real numbers.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     In short, those are: a conversion to float, trunc(), divmod, | 
					
						
							|  |  |  |     %, <, <=, >, and >=. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Real also provides defaults for the derived operations. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __float__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """Any Real can be converted to a native float object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Called for float(self)."""
 | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __trunc__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """trunc(self): Truncates self to an Integral.
 | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Returns an Integral i such that: | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  |           * i>0 iff self>0; | 
					
						
							|  |  |  |           * abs(i) <= abs(self); | 
					
						
							|  |  |  |           * for any Integral j satisfying the first two conditions, | 
					
						
							|  |  |  |             abs(i) >= abs(j) [i.e. i has "maximal" abs among those]. | 
					
						
							|  |  |  |         i.e. "truncate towards 0". | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __floor__(self): | 
					
						
							|  |  |  |         """Finds the greatest Integral <= self.""" | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __ceil__(self): | 
					
						
							|  |  |  |         """Finds the least Integral >= self.""" | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __round__(self, ndigits:"Integral"=None): | 
					
						
							|  |  |  |         """Rounds self to ndigits decimal places, defaulting to 0.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         If ndigits is omitted or None, returns an Integral, otherwise | 
					
						
							|  |  |  |         returns a Real. Rounds half toward even. | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __divmod__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """divmod(self, other): The pair (self // other, self % other).
 | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Sometimes this can be computed faster than the pair of | 
					
						
							|  |  |  |         operations. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return (self // other, self % other) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __rdivmod__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """divmod(other, self): The pair (self // other, self % other).
 | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Sometimes this can be computed faster than the pair of | 
					
						
							|  |  |  |         operations. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return (other // self, other % self) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __floordiv__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self // other: The floor() of self/other.""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rfloordiv__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other // self: The floor() of other/self.""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __mod__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self % other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rmod__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other % self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __lt__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self < other
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         < on Reals defines a total ordering, except perhaps for NaN."""
 | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |     @abstractmethod | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |     def __le__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self <= other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Concrete implementations of Complex abstract methods. | 
					
						
							|  |  |  |     def __complex__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """complex(self) == complex(float(self), 0)""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return complex(float(self)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def real(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """Real numbers are their real component.""" | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  |         return +self | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def imag(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """Real numbers have no imaginary component.""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def conjugate(self): | 
					
						
							|  |  |  |         """Conjugate is a no-op for Reals.""" | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  |         return +self | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Real.register(float) | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  | # Real.register(decimal.Decimal) | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Rational(Real, Exact): | 
					
						
							|  |  |  |     """.numerator and .denominator should be in lowest terms.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractproperty | 
					
						
							|  |  |  |     def numerator(self): | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractproperty | 
					
						
							|  |  |  |     def denominator(self): | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Concrete implementation of Real's conversion to float. | 
					
						
							|  |  |  |     def __float__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """float(self) = self.numerator / self.denominator""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return self.numerator / self.denominator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Integral(Rational): | 
					
						
							|  |  |  |     """Integral adds a conversion to int and the bit-string operations.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __int__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """int(self)""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __index__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """index(self)""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return int(self) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |     def __pow__(self, exponent, modulus=None): | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         """self ** exponent % modulus, but maybe faster.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         Accept the modulus argument if you want to support the | 
					
						
							|  |  |  |         3-argument version of pow(). Raise a TypeError if exponent < 0 | 
					
						
							|  |  |  |         or any argument isn't Integral. Otherwise, just implement the | 
					
						
							|  |  |  |         2-argument version described in Complex. | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __lshift__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self << other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rlshift__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other << self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rshift__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self >> other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rrshift__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other >> self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __and__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self & other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rand__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other & self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __xor__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self ^ other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __rxor__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other ^ self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __or__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """self | other""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __ror__(self, other): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """other | self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def __invert__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """~self""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Concrete implementations of Rational and Real abstract methods. | 
					
						
							|  |  |  |     def __float__(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """float(self) == float(int(self))""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return float(int(self)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def numerator(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """Integers are their own numerators.""" | 
					
						
							| 
									
										
										
										
											2007-12-06 17:45:33 +00:00
										 |  |  |         return +self | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def denominator(self): | 
					
						
							| 
									
										
										
										
											2007-09-07 15:15:49 +00:00
										 |  |  |         """Integers have a denominator of 1.""" | 
					
						
							| 
									
										
										
										
											2007-08-30 17:45:54 +00:00
										 |  |  |         return 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Integral.register(int) |