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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60752 | mark.dickinson | 2008-02-12 22:31:59 +0100 (Tue, 12 Feb 2008) | 5 lines
Implementation of Fraction.limit_denominator.
Remove Fraction.to_continued_fraction and
Fraction.from_continued_fraction
........
r60754 | mark.dickinson | 2008-02-12 22:40:53 +0100 (Tue, 12 Feb 2008) | 3 lines
Revert change in r60712: turn alternate constructors back into
classmethods instead of staticmethods.
........
r60755 | mark.dickinson | 2008-02-12 22:46:54 +0100 (Tue, 12 Feb 2008) | 4 lines
Replace R=fractions.Fraction with F=fractions.Fraction in
test_fractions.py. This should have been part of the name
change from Rational to Fraction.
........
r60758 | georg.brandl | 2008-02-13 08:20:22 +0100 (Wed, 13 Feb 2008) | 3 lines
#2063: correct order of utime and stime in os.times()
result on Windows.
........
r60762 | jeffrey.yasskin | 2008-02-13 18:58:04 +0100 (Wed, 13 Feb 2008) | 7 lines
Working on issue #1762: Brought
./python.exe -m timeit -s 'from fractions import Fraction; f = Fraction(3, 2)' 'isinstance(3, Fraction); isinstance(f, Fraction)'
from 12.3 usec/loop to 3.44 usec/loop and
./python.exe -m timeit -s 'from fractions import Fraction' 'Fraction(3, 2)'
from 48.8 usec to 23.6 usec by avoiding genexps and sets in __instancecheck__
and inlining the common case from __subclasscheck__.
........
r60765 | brett.cannon | 2008-02-13 20:15:44 +0100 (Wed, 13 Feb 2008) | 5 lines
Fix --enable-universalsdk and its comment line so that zsh's flag completion
works.
Thanks to Jeroen Ruigrok van der Werven for the fix.
........
r60771 | kurt.kaiser | 2008-02-14 01:08:55 +0100 (Thu, 14 Feb 2008) | 2 lines
Bring NEWS.txt up to date from check-in msgs.
........
r60772 | raymond.hettinger | 2008-02-14 02:08:02 +0100 (Thu, 14 Feb 2008) | 3 lines
Update notes on Decimal.
........
r60773 | raymond.hettinger | 2008-02-14 03:41:22 +0100 (Thu, 14 Feb 2008) | 1 line
Fix decimal repr which should have used single quotes like other reprs.
........
r60785 | jeffrey.yasskin | 2008-02-14 07:12:24 +0100 (Thu, 14 Feb 2008) | 11 lines
Performance optimizations on Fraction's constructor.
./python.exe -m timeit -s 'from fractions import Fraction' 'Fraction(3)`
31.7 usec/loop -> 9.2 usec/loop
./python.exe -m timeit -s 'from fractions import Fraction' 'Fraction(3, 2)'`
27.7 usec/loop -> 9.32 usec/loop
./python.exe -m timeit -s 'from fractions import Fraction; f = Fraction(3, 2)' 'Fraction(f)'
31.9 usec/loop -> 14.3 usec/loop
........
r60786 | jeffrey.yasskin | 2008-02-14 08:49:25 +0100 (Thu, 14 Feb 2008) | 5 lines
Change simple instances (in Fraction) of self.numerator and self.denominator to
self._numerator and self._denominator. This speeds abs() up from 12.2us to
10.8us and trunc() from 2.07us to 1.11us. This doesn't change _add and friends
because they're more complicated.
........
2008-02-14 08:27:37 +00:00
|
|
|
## Notes on Decimal
|
|
|
|
## ----------------
|
|
|
|
## Decimal has all of the methods specified by the Real abc, but it should
|
|
|
|
## not be registered as a Real because decimals do not interoperate with
|
|
|
|
## binary floats.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60735-60751 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60735 | raymond.hettinger | 2008-02-11 23:53:01 +0100 (Mon, 11 Feb 2008) | 1 line
Add notes on how decimal fits into the model.
........
r60737 | raymond.hettinger | 2008-02-12 00:34:56 +0100 (Tue, 12 Feb 2008) | 1 line
Fix markup
........
r60738 | raymond.hettinger | 2008-02-12 00:38:00 +0100 (Tue, 12 Feb 2008) | 1 line
Backport ABC docs
........
r60739 | raymond.hettinger | 2008-02-12 01:15:32 +0100 (Tue, 12 Feb 2008) | 1 line
Restore fractions.rst to the document tree.
........
r60740 | raymond.hettinger | 2008-02-12 01:48:20 +0100 (Tue, 12 Feb 2008) | 1 line
Fix typo in comments
........
r60741 | raymond.hettinger | 2008-02-12 02:18:03 +0100 (Tue, 12 Feb 2008) | 1 line
Bring decimal a bit closer to the spec for Reals.
........
r60743 | martin.v.loewis | 2008-02-12 14:47:26 +0100 (Tue, 12 Feb 2008) | 2 lines
Patch #1736: Fix file name handling of _msi.FCICreate.
........
r60745 | kurt.kaiser | 2008-02-12 16:45:50 +0100 (Tue, 12 Feb 2008) | 2 lines
what??! Correct r60225.
........
r60747 | martin.v.loewis | 2008-02-12 19:47:34 +0100 (Tue, 12 Feb 2008) | 4 lines
Patch #1966: Break infinite loop in httplib when the servers
implements the chunked encoding incorrectly.
Will backport to 2.5.
........
r60749 | raymond.hettinger | 2008-02-12 20:05:36 +0100 (Tue, 12 Feb 2008) | 1 line
dict.copy() rises from the ashes. Revert r60687.
........
2008-02-12 22:59:25 +00:00
|
|
|
##
|
|
|
|
## Decimal has some of the characteristics of Integrals. It provides
|
|
|
|
## logical operations but not as operators. The logical operations only apply
|
|
|
|
## to a subset of decimals (those that are non-negative, have a zero exponent,
|
|
|
|
## and have digits that are only 0 or 1). It does provide __long__() and
|
|
|
|
## a three argument form of __pow__ that includes exactness guarantees.
|
|
|
|
## It does not provide an __index__() method.
|
|
|
|
##
|
|
|
|
## Depending on context, decimal operations may be exact or inexact.
|
|
|
|
##
|
|
|
|
## When decimal is run in a context with small precision and automatic rounding,
|
|
|
|
## it is Inexact. See the "Floating point notes" section of the decimal docs
|
|
|
|
## for an example of losing the associative and distributive properties of
|
|
|
|
## addition.
|
|
|
|
##
|
|
|
|
## When decimal is used for high precision integer arithmetic, it is Exact.
|
|
|
|
## When the decimal used as fixed-point, it is Exact.
|
|
|
|
## When it is run with sufficient precision, it is Exact.
|
|
|
|
## When the decimal.Inexact trap is set, decimal operations are Exact.
|
|
|
|
## For an example, see the float_to_decimal() recipe in the "Decimal FAQ"
|
|
|
|
## section of the docs -- it shows an how traps are used in conjunction
|
|
|
|
## with variable precision to reliably achieve exact results.
|
|
|
|
|
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
|
|
|
|
2008-01-17 07:36:30 +00:00
|
|
|
def __bool__(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
|
|
|
|
|
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
|
|
|
|
def __truediv__(self, other):
|
2008-01-17 07:36:30 +00:00
|
|
|
"""self / other: Should promote to float when necessary."""
|
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
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __rtruediv__(self, other):
|
2008-01-17 07:36:30 +00:00
|
|
|
"""other / self"""
|
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
|
|
|
|
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines
Remove month parameter from Calendar.yeardatescalendar(),
Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods
don't have such a parameter. Fixes issue #2017.
Rewrap content to 80 chars.
........
r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines
Fixes issue 1959. Converted tests to unittest.
Thanks Giampaolo Rodola.
........
r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines
Fixed refcounts and error handling.
Should not be merged to py3k branch.
........
r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines
Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN
raise InvalidOperation (and return False if InvalidOperation is trapped).
........
r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines
Remove incorrect usage of :const: in documentation.
........
r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines
Revert accidental changes to test_queue in r60605.
........
r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line
Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
........
r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines
Fix broken link in decimal documentation.
........
r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines
IEEE 754 should be IEEE 854; give precise reference for
comparisons involving NaNs.
........
r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line
Return ints instead of longs for tuple.count() and tuple.index().
........
r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line
Merge 60627.
........
r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line
Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs.
........
r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines
Cast a struct to a void pointer so as to do a type-safe pointer comparison
(mistmatch found by clang).
........
r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines
Remove unnecessary curly braces around an int literal.
........
r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line
Update URL
........
r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines
Fixes issue 2026. Tests converted to unittest. Thanks
Giampaolo Rodola.
........
r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
........
r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines
Fixes Issue 1401. When redirected, a possible POST get converted
to GET, so it loses its payload. So, it also must lose the
headers related to the payload (if it has no content any more,
it shouldn't indicate content length and type).
........
r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines
Clarify that the output of TextCalendar.formatmonth() and
TextCalendar.formatyear() for custom instances won't be influenced by calls
to the module global setfirstweekday() function. Fixes #2018.
........
r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines
Fix documentation for Calendar.iterweekdays(): firstweekday is a property.
Fixes second part of #2018.
........
r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines
Fix typo in docstring for Calendar.itermonthdays().
........
r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line
The float conversion recipe is simpler in Py2.6
........
r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line
Fix typo
........
r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines
Make sure a switch statement does not have repetitive case statements.
Error found through LLVM post-2.1 svn.
........
r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line
Deallocate content of the dict free list on interpreter shutdown
........
r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line
Use prefix decrement
........
r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines
issue 2045: Infinite recursion when printing a subclass of defaultdict,
if default_factory is set to a bound method.
Will backport.
........
r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines
Oops! 2.6's Rational.__ne__ didn't work.
........
r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines
Update big5hkscs codec to conform to the HKSCS:2004 revision.
........
r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines
Remove unnecessary modulo division.
The preceding test guarantees that 0 <= i < len.
........
r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line
Speed-up __iter__() mixin method.
........
r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line
Fill-in missing Set comparisons
........
r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line
Add advice on choosing between DictMixin and MutableMapping
........
2008-02-09 02:18:51 +00:00
|
|
|
def __ne__(self, other):
|
|
|
|
"""self != other"""
|
|
|
|
# The default __ne__ doesn't negate __eq__ until 3.0.
|
|
|
|
return not (self == other)
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
2008-01-31 14:31:45 +00:00
|
|
|
"""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.
|
|
|
|
|
|
|
|
"""
|
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)
|