Fix a bunch of doctests with the -d option of refactor.py.
We still have 27 failing tests (down from 39).
This commit is contained in:
parent
4502c804b9
commit
7131f84400
|
@ -80,9 +80,9 @@ attributes by using the .output() function
|
||||||
>>> C = Cookie.SmartCookie()
|
>>> C = Cookie.SmartCookie()
|
||||||
>>> C["rocky"] = "road"
|
>>> C["rocky"] = "road"
|
||||||
>>> C["rocky"]["path"] = "/cookie"
|
>>> C["rocky"]["path"] = "/cookie"
|
||||||
>>> print(C.output(header="Cookie:"))
|
>>> print((C.output(header="Cookie:")))
|
||||||
Cookie: rocky=road; Path=/cookie
|
Cookie: rocky=road; Path=/cookie
|
||||||
>>> print(C.output(attrs=[], header="Cookie:"))
|
>>> print((C.output(attrs=[], header="Cookie:")))
|
||||||
Cookie: rocky=road
|
Cookie: rocky=road
|
||||||
|
|
||||||
The load() method of a Cookie extracts cookies from a string. In a
|
The load() method of a Cookie extracts cookies from a string. In a
|
||||||
|
@ -100,7 +100,7 @@ such trickeries do not confuse it.
|
||||||
|
|
||||||
>>> C = Cookie.SmartCookie()
|
>>> C = Cookie.SmartCookie()
|
||||||
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
|
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
|
||||||
>>> print(C)
|
>>> print((C))
|
||||||
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
|
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
|
||||||
|
|
||||||
Each element of the Cookie also supports all of the RFC 2109
|
Each element of the Cookie also supports all of the RFC 2109
|
||||||
|
@ -110,7 +110,7 @@ attribute.
|
||||||
>>> C = Cookie.SmartCookie()
|
>>> C = Cookie.SmartCookie()
|
||||||
>>> C["oreo"] = "doublestuff"
|
>>> C["oreo"] = "doublestuff"
|
||||||
>>> C["oreo"]["path"] = "/"
|
>>> C["oreo"]["path"] = "/"
|
||||||
>>> print(C)
|
>>> print((C))
|
||||||
Set-Cookie: oreo=doublestuff; Path=/
|
Set-Cookie: oreo=doublestuff; Path=/
|
||||||
|
|
||||||
Each dictionary element has a 'value' attribute, which gives you
|
Each dictionary element has a 'value' attribute, which gives you
|
||||||
|
@ -198,7 +198,7 @@ it is still possible to use Cookie.Cookie() to create a Cookie. In
|
||||||
fact, this simply returns a SmartCookie.
|
fact, this simply returns a SmartCookie.
|
||||||
|
|
||||||
>>> C = Cookie.Cookie()
|
>>> C = Cookie.Cookie()
|
||||||
>>> print(C.__class__.__name__)
|
>>> print((C.__class__.__name__))
|
||||||
SmartCookie
|
SmartCookie
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ Here is an array of string pointers:
|
||||||
|
|
||||||
>>> from ctypes import *
|
>>> from ctypes import *
|
||||||
>>> array = (c_char_p * 5)()
|
>>> array = (c_char_p * 5)()
|
||||||
>>> print array._objects
|
>>> print(array._objects)
|
||||||
None
|
None
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
@ -34,14 +34,14 @@ in a 'base' object.
|
||||||
... _fields_ = [("x", c_int), ("y", c_int), ("array", c_char_p * 5)]
|
... _fields_ = [("x", c_int), ("y", c_int), ("array", c_char_p * 5)]
|
||||||
...
|
...
|
||||||
>>> x = X()
|
>>> x = X()
|
||||||
>>> print x._objects
|
>>> print(x._objects)
|
||||||
None
|
None
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
The'array' attribute of the 'x' object shares part of the memory buffer
|
The'array' attribute of the 'x' object shares part of the memory buffer
|
||||||
of 'x' ('_b_base_' is either None, or the root object owning the memory block):
|
of 'x' ('_b_base_' is either None, or the root object owning the memory block):
|
||||||
|
|
||||||
>>> print x.array._b_base_ # doctest: +ELLIPSIS
|
>>> print(x.array._b_base_) # doctest: +ELLIPSIS
|
||||||
<ctypes.test.test_objects.X object at 0x...>
|
<ctypes.test.test_objects.X object at 0x...>
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
|
|
@ -56,31 +56,31 @@ Decimal("2.60")
|
||||||
>>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
|
>>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
|
||||||
Decimal("-2.20")
|
Decimal("-2.20")
|
||||||
>>> dig = Decimal(1)
|
>>> dig = Decimal(1)
|
||||||
>>> print dig / Decimal(3)
|
>>> print(dig / Decimal(3))
|
||||||
0.333333333
|
0.333333333
|
||||||
>>> getcontext().prec = 18
|
>>> getcontext().prec = 18
|
||||||
>>> print dig / Decimal(3)
|
>>> print(dig / Decimal(3))
|
||||||
0.333333333333333333
|
0.333333333333333333
|
||||||
>>> print dig.sqrt()
|
>>> print(dig.sqrt())
|
||||||
1
|
1
|
||||||
>>> print Decimal(3).sqrt()
|
>>> print(Decimal(3).sqrt())
|
||||||
1.73205080756887729
|
1.73205080756887729
|
||||||
>>> print Decimal(3) ** 123
|
>>> print(Decimal(3) ** 123)
|
||||||
4.85192780976896427E+58
|
4.85192780976896427E+58
|
||||||
>>> inf = Decimal(1) / Decimal(0)
|
>>> inf = Decimal(1) / Decimal(0)
|
||||||
>>> print inf
|
>>> print(inf)
|
||||||
Infinity
|
Infinity
|
||||||
>>> neginf = Decimal(-1) / Decimal(0)
|
>>> neginf = Decimal(-1) / Decimal(0)
|
||||||
>>> print neginf
|
>>> print(neginf)
|
||||||
-Infinity
|
-Infinity
|
||||||
>>> print neginf + inf
|
>>> print(neginf + inf)
|
||||||
NaN
|
NaN
|
||||||
>>> print neginf * inf
|
>>> print(neginf * inf)
|
||||||
-Infinity
|
-Infinity
|
||||||
>>> print dig / 0
|
>>> print(dig / 0)
|
||||||
Infinity
|
Infinity
|
||||||
>>> getcontext().traps[DivisionByZero] = 1
|
>>> getcontext().traps[DivisionByZero] = 1
|
||||||
>>> print dig / 0
|
>>> print(dig / 0)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
...
|
...
|
||||||
|
@ -88,29 +88,29 @@ Traceback (most recent call last):
|
||||||
decimal.DivisionByZero: x / 0
|
decimal.DivisionByZero: x / 0
|
||||||
>>> c = Context()
|
>>> c = Context()
|
||||||
>>> c.traps[InvalidOperation] = 0
|
>>> c.traps[InvalidOperation] = 0
|
||||||
>>> print c.flags[InvalidOperation]
|
>>> print(c.flags[InvalidOperation])
|
||||||
0
|
0
|
||||||
>>> c.divide(Decimal(0), Decimal(0))
|
>>> c.divide(Decimal(0), Decimal(0))
|
||||||
Decimal("NaN")
|
Decimal("NaN")
|
||||||
>>> c.traps[InvalidOperation] = 1
|
>>> c.traps[InvalidOperation] = 1
|
||||||
>>> print c.flags[InvalidOperation]
|
>>> print(c.flags[InvalidOperation])
|
||||||
1
|
1
|
||||||
>>> c.flags[InvalidOperation] = 0
|
>>> c.flags[InvalidOperation] = 0
|
||||||
>>> print c.flags[InvalidOperation]
|
>>> print(c.flags[InvalidOperation])
|
||||||
0
|
0
|
||||||
>>> print c.divide(Decimal(0), Decimal(0))
|
>>> print(c.divide(Decimal(0), Decimal(0)))
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
...
|
...
|
||||||
...
|
...
|
||||||
decimal.InvalidOperation: 0 / 0
|
decimal.InvalidOperation: 0 / 0
|
||||||
>>> print c.flags[InvalidOperation]
|
>>> print(c.flags[InvalidOperation])
|
||||||
1
|
1
|
||||||
>>> c.flags[InvalidOperation] = 0
|
>>> c.flags[InvalidOperation] = 0
|
||||||
>>> c.traps[InvalidOperation] = 0
|
>>> c.traps[InvalidOperation] = 0
|
||||||
>>> print c.divide(Decimal(0), Decimal(0))
|
>>> print(c.divide(Decimal(0), Decimal(0)))
|
||||||
NaN
|
NaN
|
||||||
>>> print c.flags[InvalidOperation]
|
>>> print(c.flags[InvalidOperation])
|
||||||
1
|
1
|
||||||
>>>
|
>>>
|
||||||
"""
|
"""
|
||||||
|
@ -483,19 +483,19 @@ def localcontext(ctx=None):
|
||||||
# as the doctest module doesn't understand __future__ statements
|
# as the doctest module doesn't understand __future__ statements
|
||||||
"""
|
"""
|
||||||
>>> from __future__ import with_statement
|
>>> from __future__ import with_statement
|
||||||
>>> print getcontext().prec
|
>>> print(getcontext().prec)
|
||||||
28
|
28
|
||||||
>>> with localcontext():
|
>>> with localcontext():
|
||||||
... ctx = getcontext()
|
... ctx = getcontext()
|
||||||
... ctx.prec() += 2
|
... ctx.prec() += 2
|
||||||
... print ctx.prec
|
... print(ctx.prec)
|
||||||
...
|
...
|
||||||
30
|
30
|
||||||
>>> with localcontext(ExtendedContext):
|
>>> with localcontext(ExtendedContext):
|
||||||
... print getcontext().prec
|
... print(getcontext().prec)
|
||||||
...
|
...
|
||||||
9
|
9
|
||||||
>>> print getcontext().prec
|
>>> print(getcontext().prec)
|
||||||
28
|
28
|
||||||
"""
|
"""
|
||||||
if ctx is None: ctx = getcontext()
|
if ctx is None: ctx = getcontext()
|
||||||
|
|
|
@ -76,7 +76,7 @@ class SequenceMatcher:
|
||||||
sequences. As a rule of thumb, a .ratio() value over 0.6 means the
|
sequences. As a rule of thumb, a .ratio() value over 0.6 means the
|
||||||
sequences are close matches:
|
sequences are close matches:
|
||||||
|
|
||||||
>>> print(round(s.ratio(), 3))
|
>>> print((round(s.ratio(), 3)))
|
||||||
0.866
|
0.866
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ class SequenceMatcher:
|
||||||
.get_matching_blocks() is handy:
|
.get_matching_blocks() is handy:
|
||||||
|
|
||||||
>>> for block in s.get_matching_blocks():
|
>>> for block in s.get_matching_blocks():
|
||||||
... print("a[%d] and b[%d] match for %d elements" % block)
|
... print(("a[%d] and b[%d] match for %d elements" % block))
|
||||||
a[0] and b[0] match for 8 elements
|
a[0] and b[0] match for 8 elements
|
||||||
a[8] and b[17] match for 21 elements
|
a[8] and b[17] match for 21 elements
|
||||||
a[29] and b[38] match for 0 elements
|
a[29] and b[38] match for 0 elements
|
||||||
|
@ -97,7 +97,7 @@ class SequenceMatcher:
|
||||||
use .get_opcodes():
|
use .get_opcodes():
|
||||||
|
|
||||||
>>> for opcode in s.get_opcodes():
|
>>> for opcode in s.get_opcodes():
|
||||||
... print("%6s a[%d:%d] b[%d:%d]" % opcode)
|
... print(("%6s a[%d:%d] b[%d:%d]" % opcode))
|
||||||
equal a[0:8] b[0:8]
|
equal a[0:8] b[0:8]
|
||||||
insert a[8:8] b[8:17]
|
insert a[8:8] b[8:17]
|
||||||
equal a[8:29] b[17:38]
|
equal a[8:29] b[17:38]
|
||||||
|
@ -545,8 +545,8 @@ class SequenceMatcher:
|
||||||
>>> b = "abycdf"
|
>>> b = "abycdf"
|
||||||
>>> s = SequenceMatcher(None, a, b)
|
>>> s = SequenceMatcher(None, a, b)
|
||||||
>>> for tag, i1, i2, j1, j2 in s.get_opcodes():
|
>>> for tag, i1, i2, j1, j2 in s.get_opcodes():
|
||||||
... print(("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
|
... print((("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
|
||||||
... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])))
|
... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))))
|
||||||
delete a[0:1] (q) b[0:0] ()
|
delete a[0:1] (q) b[0:0] ()
|
||||||
equal a[1:3] (ab) b[0:2] (ab)
|
equal a[1:3] (ab) b[0:2] (ab)
|
||||||
replace a[3:4] (x) b[2:3] (y)
|
replace a[3:4] (x) b[2:3] (y)
|
||||||
|
@ -1059,8 +1059,8 @@ class Differ:
|
||||||
>>> d = Differ()
|
>>> d = Differ()
|
||||||
>>> results = d._qformat('\tabcDefghiJkl\n', '\t\tabcdefGhijkl\n',
|
>>> results = d._qformat('\tabcDefghiJkl\n', '\t\tabcdefGhijkl\n',
|
||||||
... ' ^ ^ ^ ', '+ ^ ^ ^ ')
|
... ' ^ ^ ^ ', '+ ^ ^ ^ ')
|
||||||
>>> for line in results: print(repr(line))
|
>>> for line in results: print((repr(line)))
|
||||||
...
|
...
|
||||||
'- \tabcDefghiJkl\n'
|
'- \tabcDefghiJkl\n'
|
||||||
'? \t ^ ^ ^\n'
|
'? \t ^ ^ ^\n'
|
||||||
'+ \t\tabcdefGhijkl\n'
|
'+ \t\tabcdefGhijkl\n'
|
||||||
|
@ -1165,7 +1165,7 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
|
||||||
... 'zero one tree four'.split(), 'Original', 'Current',
|
... 'zero one tree four'.split(), 'Original', 'Current',
|
||||||
... 'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:20:52 2003',
|
... 'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:20:52 2003',
|
||||||
... lineterm=''):
|
... lineterm=''):
|
||||||
... print(line)
|
... print((line))
|
||||||
--- Original Sat Jan 26 23:30:50 1991
|
--- Original Sat Jan 26 23:30:50 1991
|
||||||
+++ Current Fri Jun 06 10:20:52 2003
|
+++ Current Fri Jun 06 10:20:52 2003
|
||||||
@@ -1,4 +1,4 @@
|
@@ -1,4 +1,4 @@
|
||||||
|
|
|
@ -40,7 +40,7 @@ class VersionPredicate:
|
||||||
The str() of a `VersionPredicate` provides a normalized
|
The str() of a `VersionPredicate` provides a normalized
|
||||||
human-readable version of the expression::
|
human-readable version of the expression::
|
||||||
|
|
||||||
>>> print v
|
>>> print(v)
|
||||||
pyepat.abc (> 1.0, < 3333.3a1, != 1555.1b3)
|
pyepat.abc (> 1.0, < 3333.3a1, != 1555.1b3)
|
||||||
|
|
||||||
The `satisfied_by()` method can be used to determine with a given
|
The `satisfied_by()` method can be used to determine with a given
|
||||||
|
|
|
@ -1012,7 +1012,7 @@ class DocTestRunner:
|
||||||
>>> runner = DocTestRunner(verbose=False)
|
>>> runner = DocTestRunner(verbose=False)
|
||||||
>>> tests.sort(key = lambda test: test.name)
|
>>> tests.sort(key = lambda test: test.name)
|
||||||
>>> for test in tests:
|
>>> for test in tests:
|
||||||
... print test.name, '->', runner.run(test)
|
... print(test.name, '->', runner.run(test))
|
||||||
_TestClass -> (0, 2)
|
_TestClass -> (0, 2)
|
||||||
_TestClass.__init__ -> (0, 2)
|
_TestClass.__init__ -> (0, 2)
|
||||||
_TestClass.get -> (0, 2)
|
_TestClass.get -> (0, 2)
|
||||||
|
@ -2419,7 +2419,7 @@ def script_from_examples(s):
|
||||||
... Ho hum
|
... Ho hum
|
||||||
... '''
|
... '''
|
||||||
|
|
||||||
>>> print script_from_examples(text)
|
>>> print(script_from_examples(text))
|
||||||
# Here are examples of simple math.
|
# Here are examples of simple math.
|
||||||
#
|
#
|
||||||
# Python has super accurate integer addition
|
# Python has super accurate integer addition
|
||||||
|
@ -2554,7 +2554,7 @@ class _TestClass:
|
||||||
"""val -> _TestClass object with associated value val.
|
"""val -> _TestClass object with associated value val.
|
||||||
|
|
||||||
>>> t = _TestClass(123)
|
>>> t = _TestClass(123)
|
||||||
>>> print t.get()
|
>>> print(t.get())
|
||||||
123
|
123
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -2574,7 +2574,7 @@ class _TestClass:
|
||||||
"""get() -> return TestClass's associated value.
|
"""get() -> return TestClass's associated value.
|
||||||
|
|
||||||
>>> x = _TestClass(-42)
|
>>> x = _TestClass(-42)
|
||||||
>>> print x.get()
|
>>> print(x.get())
|
||||||
-42
|
-42
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -2606,7 +2606,7 @@ __test__ = {"_TestClass": _TestClass,
|
||||||
|
|
||||||
"blank lines": r"""
|
"blank lines": r"""
|
||||||
Blank lines can be marked with <BLANKLINE>:
|
Blank lines can be marked with <BLANKLINE>:
|
||||||
>>> print 'foo\n\nbar\n'
|
>>> print('foo\n\nbar\n')
|
||||||
foo
|
foo
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
bar
|
bar
|
||||||
|
@ -2616,14 +2616,14 @@ __test__ = {"_TestClass": _TestClass,
|
||||||
"ellipsis": r"""
|
"ellipsis": r"""
|
||||||
If the ellipsis flag is used, then '...' can be used to
|
If the ellipsis flag is used, then '...' can be used to
|
||||||
elide substrings in the desired output:
|
elide substrings in the desired output:
|
||||||
>>> print range(1000) #doctest: +ELLIPSIS
|
>>> print(range(1000)) #doctest: +ELLIPSIS
|
||||||
[0, 1, 2, ..., 999]
|
[0, 1, 2, ..., 999]
|
||||||
""",
|
""",
|
||||||
|
|
||||||
"whitespace normalization": r"""
|
"whitespace normalization": r"""
|
||||||
If the whitespace normalization flag is used, then
|
If the whitespace normalization flag is used, then
|
||||||
differences in whitespace are ignored.
|
differences in whitespace are ignored.
|
||||||
>>> print range(30) #doctest: +NORMALIZE_WHITESPACE
|
>>> print(range(30)) #doctest: +NORMALIZE_WHITESPACE
|
||||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
||||||
27, 28, 29]
|
27, 28, 29]
|
||||||
|
|
|
@ -5,7 +5,7 @@ Example:
|
||||||
>>> from nntplib import NNTP
|
>>> from nntplib import NNTP
|
||||||
>>> s = NNTP('news')
|
>>> s = NNTP('news')
|
||||||
>>> resp, count, first, last, name = s.group('comp.lang.python')
|
>>> resp, count, first, last, name = s.group('comp.lang.python')
|
||||||
>>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
|
>>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
|
||||||
Group comp.lang.python has 51 articles, range 5770 to 5821
|
Group comp.lang.python has 51 articles, range 5770 to 5821
|
||||||
>>> resp, subs = s.xhdr('subject', first + '-' + last)
|
>>> resp, subs = s.xhdr('subject', first + '-' + last)
|
||||||
>>> resp = s.quit()
|
>>> resp = s.quit()
|
||||||
|
|
|
@ -15,7 +15,7 @@ Example:
|
||||||
|
|
||||||
>>> import smtplib
|
>>> import smtplib
|
||||||
>>> s=smtplib.SMTP("localhost")
|
>>> s=smtplib.SMTP("localhost")
|
||||||
>>> print s.help()
|
>>> print(s.help())
|
||||||
This is Sendmail version 8.8.4
|
This is Sendmail version 8.8.4
|
||||||
Topics:
|
Topics:
|
||||||
HELO EHLO MAIL RCPT DATA
|
HELO EHLO MAIL RCPT DATA
|
||||||
|
|
|
@ -8,7 +8,7 @@ Example:
|
||||||
>>> from telnetlib import Telnet
|
>>> from telnetlib import Telnet
|
||||||
>>> tn = Telnet('www.python.org', 79) # connect to finger port
|
>>> tn = Telnet('www.python.org', 79) # connect to finger port
|
||||||
>>> tn.write('guido\r\n')
|
>>> tn.write('guido\r\n')
|
||||||
>>> print tn.read_all()
|
>>> print(tn.read_all())
|
||||||
Login Name TTY Idle When Where
|
Login Name TTY Idle When Where
|
||||||
guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston..
|
guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston..
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ class TwoNames:
|
||||||
|
|
||||||
def f(self):
|
def f(self):
|
||||||
'''
|
'''
|
||||||
>>> print TwoNames().f()
|
>>> print(TwoNames().f())
|
||||||
f
|
f
|
||||||
'''
|
'''
|
||||||
return 'f'
|
return 'f'
|
||||||
|
|
|
@ -40,9 +40,9 @@ def test_silly_setup():
|
||||||
def w_blank():
|
def w_blank():
|
||||||
"""
|
"""
|
||||||
>>> if 1:
|
>>> if 1:
|
||||||
... print 'a'
|
... print('a')
|
||||||
... print
|
... print()
|
||||||
... print 'b'
|
... print('b')
|
||||||
a
|
a
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
b
|
b
|
||||||
|
|
|
@ -50,9 +50,9 @@ flags: 67
|
||||||
consts: ('None',)
|
consts: ('None',)
|
||||||
|
|
||||||
>>> def attrs(obj):
|
>>> def attrs(obj):
|
||||||
... print obj.attr1
|
... print(obj.attr1)
|
||||||
... print obj.attr2
|
... print(obj.attr2)
|
||||||
... print obj.attr3
|
... print(obj.attr3)
|
||||||
|
|
||||||
>>> dump(attrs.func_code)
|
>>> dump(attrs.func_code)
|
||||||
name: attrs
|
name: attrs
|
||||||
|
|
|
@ -504,7 +504,7 @@ Example from the Library Reference: Doc/lib/libcollections.tex
|
||||||
>>> from collections import deque
|
>>> from collections import deque
|
||||||
>>> d = deque('ghi') # make a new deque with three items
|
>>> d = deque('ghi') # make a new deque with three items
|
||||||
>>> for elem in d: # iterate over the deque's elements
|
>>> for elem in d: # iterate over the deque's elements
|
||||||
... print elem.upper()
|
... print(elem.upper())
|
||||||
G
|
G
|
||||||
H
|
H
|
||||||
I
|
I
|
||||||
|
@ -574,8 +574,8 @@ deque(['a', 'b', 'd', 'e', 'f'])
|
||||||
...
|
...
|
||||||
|
|
||||||
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
||||||
... print value
|
... print(value)
|
||||||
...
|
...
|
||||||
a
|
a
|
||||||
d
|
d
|
||||||
e
|
e
|
||||||
|
@ -593,7 +593,7 @@ h
|
||||||
... d.append(pair)
|
... d.append(pair)
|
||||||
... return list(d)
|
... return list(d)
|
||||||
...
|
...
|
||||||
>>> print maketree('abcdefgh')
|
>>> print(maketree('abcdefgh'))
|
||||||
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
|
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -36,28 +36,28 @@ test_1 = """
|
||||||
|
|
||||||
Here's the new type at work:
|
Here's the new type at work:
|
||||||
|
|
||||||
>>> print defaultdict # show our type
|
>>> print(defaultdict) # show our type
|
||||||
<class 'test.test_descrtut.defaultdict'>
|
<class 'test.test_descrtut.defaultdict'>
|
||||||
>>> print type(defaultdict) # its metatype
|
>>> print(type(defaultdict)) # its metatype
|
||||||
<type 'type'>
|
<type 'type'>
|
||||||
>>> a = defaultdict(default=0.0) # create an instance
|
>>> a = defaultdict(default=0.0) # create an instance
|
||||||
>>> print a # show the instance
|
>>> print(a) # show the instance
|
||||||
{}
|
{}
|
||||||
>>> print type(a) # show its type
|
>>> print(type(a)) # show its type
|
||||||
<class 'test.test_descrtut.defaultdict'>
|
<class 'test.test_descrtut.defaultdict'>
|
||||||
>>> print a.__class__ # show its class
|
>>> print(a.__class__) # show its class
|
||||||
<class 'test.test_descrtut.defaultdict'>
|
<class 'test.test_descrtut.defaultdict'>
|
||||||
>>> print type(a) is a.__class__ # its type is its class
|
>>> print(type(a) is a.__class__) # its type is its class
|
||||||
True
|
True
|
||||||
>>> a[1] = 3.25 # modify the instance
|
>>> a[1] = 3.25 # modify the instance
|
||||||
>>> print a # show the new value
|
>>> print(a) # show the new value
|
||||||
{1: 3.25}
|
{1: 3.25}
|
||||||
>>> print a[1] # show the new item
|
>>> print(a[1]) # show the new item
|
||||||
3.25
|
3.25
|
||||||
>>> print a[0] # a non-existant item
|
>>> print(a[0]) # a non-existant item
|
||||||
0.0
|
0.0
|
||||||
>>> a.merge({1:100, 2:200}) # use a dict method
|
>>> a.merge({1:100, 2:200}) # use a dict method
|
||||||
>>> print sortdict(a) # show the result
|
>>> print(sortdict(a)) # show the result
|
||||||
{1: 3.25, 2: 200}
|
{1: 3.25, 2: 200}
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
@ -65,13 +65,13 @@ We can also use the new type in contexts where classic only allows "real"
|
||||||
dictionaries, such as the locals/globals dictionaries for the exec
|
dictionaries, such as the locals/globals dictionaries for the exec
|
||||||
statement or the built-in function eval():
|
statement or the built-in function eval():
|
||||||
|
|
||||||
>>> print sorted(a.keys())
|
>>> print(sorted(a.keys()))
|
||||||
[1, 2]
|
[1, 2]
|
||||||
>>> exec("x = 3; print x", a)
|
>>> exec("x = 3; print x", a)
|
||||||
3
|
3
|
||||||
>>> print sorted(a.keys(), key=lambda x: (str(type(x)), x))
|
>>> print(sorted(a.keys(), key=lambda x: (str(type(x)), x)))
|
||||||
[1, 2, '__builtins__', 'x']
|
[1, 2, '__builtins__', 'x']
|
||||||
>>> print a['x']
|
>>> print(a['x'])
|
||||||
3
|
3
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
@ -79,21 +79,21 @@ Now I'll show that defaultdict instances have dynamic instance variables,
|
||||||
just like classic classes:
|
just like classic classes:
|
||||||
|
|
||||||
>>> a.default = -1
|
>>> a.default = -1
|
||||||
>>> print a["noway"]
|
>>> print(a["noway"])
|
||||||
-1
|
-1
|
||||||
>>> a.default = -1000
|
>>> a.default = -1000
|
||||||
>>> print a["noway"]
|
>>> print(a["noway"])
|
||||||
-1000
|
-1000
|
||||||
>>> 'default' in dir(a)
|
>>> 'default' in dir(a)
|
||||||
True
|
True
|
||||||
>>> a.x1 = 100
|
>>> a.x1 = 100
|
||||||
>>> a.x2 = 200
|
>>> a.x2 = 200
|
||||||
>>> print a.x1
|
>>> print(a.x1)
|
||||||
100
|
100
|
||||||
>>> d = dir(a)
|
>>> d = dir(a)
|
||||||
>>> 'default' in d and 'x1' in d and 'x2' in d
|
>>> 'default' in d and 'x1' in d and 'x2' in d
|
||||||
True
|
True
|
||||||
>>> print sortdict(a.__dict__)
|
>>> print(sortdict(a.__dict__))
|
||||||
{'default': -1000, 'x1': 100, 'x2': 200}
|
{'default': -1000, 'x1': 100, 'x2': 200}
|
||||||
>>>
|
>>>
|
||||||
"""
|
"""
|
||||||
|
@ -242,10 +242,10 @@ methods. Static methods are easy to describe: they behave pretty much like
|
||||||
static methods in C++ or Java. Here's an example:
|
static methods in C++ or Java. Here's an example:
|
||||||
|
|
||||||
>>> class C:
|
>>> class C:
|
||||||
...
|
...
|
||||||
... @staticmethod
|
... @staticmethod
|
||||||
... def foo(x, y):
|
... def foo(x, y):
|
||||||
... print "staticmethod", x, y
|
... print("staticmethod", x, y)
|
||||||
|
|
||||||
>>> C.foo(1, 2)
|
>>> C.foo(1, 2)
|
||||||
staticmethod 1 2
|
staticmethod 1 2
|
||||||
|
@ -259,7 +259,7 @@ implicit first argument that is the *class* for which they are invoked.
|
||||||
>>> class C:
|
>>> class C:
|
||||||
... @classmethod
|
... @classmethod
|
||||||
... def foo(cls, y):
|
... def foo(cls, y):
|
||||||
... print "classmethod", cls, y
|
... print("classmethod", cls, y)
|
||||||
|
|
||||||
>>> C.foo(1)
|
>>> C.foo(1)
|
||||||
classmethod <class 'test.test_descrtut.C'> 1
|
classmethod <class 'test.test_descrtut.C'> 1
|
||||||
|
@ -285,7 +285,7 @@ But notice this:
|
||||||
>>> class E(C):
|
>>> class E(C):
|
||||||
... @classmethod
|
... @classmethod
|
||||||
... def foo(cls, y): # override C.foo
|
... def foo(cls, y): # override C.foo
|
||||||
... print "E.foo() called"
|
... print("E.foo() called")
|
||||||
... C.foo(y)
|
... C.foo(y)
|
||||||
|
|
||||||
>>> E.foo(1)
|
>>> E.foo(1)
|
||||||
|
@ -343,10 +343,10 @@ Here's a small demonstration:
|
||||||
|
|
||||||
>>> a = C()
|
>>> a = C()
|
||||||
>>> a.x = 10
|
>>> a.x = 10
|
||||||
>>> print a.x
|
>>> print(a.x)
|
||||||
10
|
10
|
||||||
>>> a.x = -10
|
>>> a.x = -10
|
||||||
>>> print a.x
|
>>> print(a.x)
|
||||||
0
|
0
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
@ -369,10 +369,10 @@ Hmm -- property is builtin now, so let's try it that way too.
|
||||||
|
|
||||||
>>> a = C()
|
>>> a = C()
|
||||||
>>> a.x = 10
|
>>> a.x = 10
|
||||||
>>> print a.x
|
>>> print(a.x)
|
||||||
10
|
10
|
||||||
>>> a.x = -10
|
>>> a.x = -10
|
||||||
>>> print a.x
|
>>> print(a.x)
|
||||||
0
|
0
|
||||||
>>>
|
>>>
|
||||||
"""
|
"""
|
||||||
|
@ -385,12 +385,12 @@ This example is implicit in the writeup.
|
||||||
|
|
||||||
>>> class A: # implicit new-style class
|
>>> class A: # implicit new-style class
|
||||||
... def save(self):
|
... def save(self):
|
||||||
... print "called A.save()"
|
... print("called A.save()")
|
||||||
>>> class B(A):
|
>>> class B(A):
|
||||||
... pass
|
... pass
|
||||||
>>> class C(A):
|
>>> class C(A):
|
||||||
... def save(self):
|
... def save(self):
|
||||||
... print "called C.save()"
|
... print("called C.save()")
|
||||||
>>> class D(B, C):
|
>>> class D(B, C):
|
||||||
... pass
|
... pass
|
||||||
|
|
||||||
|
@ -399,12 +399,12 @@ called C.save()
|
||||||
|
|
||||||
>>> class A(object): # explicit new-style class
|
>>> class A(object): # explicit new-style class
|
||||||
... def save(self):
|
... def save(self):
|
||||||
... print "called A.save()"
|
... print("called A.save()")
|
||||||
>>> class B(A):
|
>>> class B(A):
|
||||||
... pass
|
... pass
|
||||||
>>> class C(A):
|
>>> class C(A):
|
||||||
... def save(self):
|
... def save(self):
|
||||||
... print "called C.save()"
|
... print("called C.save()")
|
||||||
>>> class D(B, C):
|
>>> class D(B, C):
|
||||||
... pass
|
... pass
|
||||||
|
|
||||||
|
@ -433,7 +433,7 @@ test_7 = """
|
||||||
|
|
||||||
Cooperative methods and "super"
|
Cooperative methods and "super"
|
||||||
|
|
||||||
>>> print D().m() # "DCBA"
|
>>> print(D().m()) # "DCBA"
|
||||||
DCBA
|
DCBA
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -443,7 +443,7 @@ Backwards incompatibilities
|
||||||
|
|
||||||
>>> class A:
|
>>> class A:
|
||||||
... def foo(self):
|
... def foo(self):
|
||||||
... print "called A.foo()"
|
... print("called A.foo()")
|
||||||
|
|
||||||
>>> class B(A):
|
>>> class B(A):
|
||||||
... pass
|
... pass
|
||||||
|
|
|
@ -14,7 +14,7 @@ def sample_func(v):
|
||||||
"""
|
"""
|
||||||
Blah blah
|
Blah blah
|
||||||
|
|
||||||
>>> print sample_func(22)
|
>>> print(sample_func(22))
|
||||||
44
|
44
|
||||||
|
|
||||||
Yee ha!
|
Yee ha!
|
||||||
|
@ -23,7 +23,7 @@ def sample_func(v):
|
||||||
|
|
||||||
class SampleClass:
|
class SampleClass:
|
||||||
"""
|
"""
|
||||||
>>> print 1
|
>>> print(1)
|
||||||
1
|
1
|
||||||
|
|
||||||
>>> # comments get ignored. so are empty PS1 and PS2 prompts:
|
>>> # comments get ignored. so are empty PS1 and PS2 prompts:
|
||||||
|
@ -34,33 +34,33 @@ class SampleClass:
|
||||||
>>> sc = SampleClass(3)
|
>>> sc = SampleClass(3)
|
||||||
>>> for i in range(10):
|
>>> for i in range(10):
|
||||||
... sc = sc.double()
|
... sc = sc.double()
|
||||||
... print sc.get(),
|
... print(sc.get(), end=' ')
|
||||||
6 12 24 48 96 192 384 768 1536 3072
|
6 12 24 48 96 192 384 768 1536 3072
|
||||||
"""
|
"""
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
"""
|
"""
|
||||||
>>> print SampleClass(12).get()
|
>>> print(SampleClass(12).get())
|
||||||
12
|
12
|
||||||
"""
|
"""
|
||||||
self.val = val
|
self.val = val
|
||||||
|
|
||||||
def double(self):
|
def double(self):
|
||||||
"""
|
"""
|
||||||
>>> print SampleClass(12).double().get()
|
>>> print(SampleClass(12).double().get())
|
||||||
24
|
24
|
||||||
"""
|
"""
|
||||||
return SampleClass(self.val + self.val)
|
return SampleClass(self.val + self.val)
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
"""
|
"""
|
||||||
>>> print SampleClass(-5).get()
|
>>> print(SampleClass(-5).get())
|
||||||
-5
|
-5
|
||||||
"""
|
"""
|
||||||
return self.val
|
return self.val
|
||||||
|
|
||||||
def a_staticmethod(v):
|
def a_staticmethod(v):
|
||||||
"""
|
"""
|
||||||
>>> print SampleClass.a_staticmethod(10)
|
>>> print(SampleClass.a_staticmethod(10))
|
||||||
11
|
11
|
||||||
"""
|
"""
|
||||||
return v+1
|
return v+1
|
||||||
|
@ -68,16 +68,16 @@ class SampleClass:
|
||||||
|
|
||||||
def a_classmethod(cls, v):
|
def a_classmethod(cls, v):
|
||||||
"""
|
"""
|
||||||
>>> print SampleClass.a_classmethod(10)
|
>>> print(SampleClass.a_classmethod(10))
|
||||||
12
|
12
|
||||||
>>> print SampleClass(0).a_classmethod(10)
|
>>> print(SampleClass(0).a_classmethod(10))
|
||||||
12
|
12
|
||||||
"""
|
"""
|
||||||
return v+2
|
return v+2
|
||||||
a_classmethod = classmethod(a_classmethod)
|
a_classmethod = classmethod(a_classmethod)
|
||||||
|
|
||||||
a_property = property(get, doc="""
|
a_property = property(get, doc="""
|
||||||
>>> print SampleClass(22).a_property
|
>>> print(SampleClass(22).a_property)
|
||||||
22
|
22
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
@ -85,12 +85,12 @@ class SampleClass:
|
||||||
"""
|
"""
|
||||||
>>> x = SampleClass.NestedClass(5)
|
>>> x = SampleClass.NestedClass(5)
|
||||||
>>> y = x.square()
|
>>> y = x.square()
|
||||||
>>> print y.get()
|
>>> print(y.get())
|
||||||
25
|
25
|
||||||
"""
|
"""
|
||||||
def __init__(self, val=0):
|
def __init__(self, val=0):
|
||||||
"""
|
"""
|
||||||
>>> print SampleClass.NestedClass().get()
|
>>> print(SampleClass.NestedClass().get())
|
||||||
0
|
0
|
||||||
"""
|
"""
|
||||||
self.val = val
|
self.val = val
|
||||||
|
@ -101,28 +101,28 @@ class SampleClass:
|
||||||
|
|
||||||
class SampleNewStyleClass(object):
|
class SampleNewStyleClass(object):
|
||||||
r"""
|
r"""
|
||||||
>>> print '1\n2\n3'
|
>>> print('1\n2\n3')
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
3
|
3
|
||||||
"""
|
"""
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
"""
|
"""
|
||||||
>>> print SampleNewStyleClass(12).get()
|
>>> print(SampleNewStyleClass(12).get())
|
||||||
12
|
12
|
||||||
"""
|
"""
|
||||||
self.val = val
|
self.val = val
|
||||||
|
|
||||||
def double(self):
|
def double(self):
|
||||||
"""
|
"""
|
||||||
>>> print SampleNewStyleClass(12).double().get()
|
>>> print(SampleNewStyleClass(12).double().get())
|
||||||
24
|
24
|
||||||
"""
|
"""
|
||||||
return SampleNewStyleClass(self.val + self.val)
|
return SampleNewStyleClass(self.val + self.val)
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
"""
|
"""
|
||||||
>>> print SampleNewStyleClass(-5).get()
|
>>> print(SampleNewStyleClass(-5).get())
|
||||||
-5
|
-5
|
||||||
"""
|
"""
|
||||||
return self.val
|
return self.val
|
||||||
|
@ -278,7 +278,7 @@ constructor:
|
||||||
>>> parser = doctest.DocTestParser()
|
>>> parser = doctest.DocTestParser()
|
||||||
>>> test = parser.get_doctest(docstring, globs, 'some_test',
|
>>> test = parser.get_doctest(docstring, globs, 'some_test',
|
||||||
... 'some_file', 20)
|
... 'some_file', 20)
|
||||||
>>> print test
|
>>> print(test)
|
||||||
<DocTest some_test from some_file:20 (2 examples)>
|
<DocTest some_test from some_file:20 (2 examples)>
|
||||||
>>> len(test.examples)
|
>>> len(test.examples)
|
||||||
2
|
2
|
||||||
|
@ -368,7 +368,7 @@ We'll simulate a __file__ attr that ends in pyc:
|
||||||
|
|
||||||
>>> tests = finder.find(sample_func)
|
>>> tests = finder.find(sample_func)
|
||||||
|
|
||||||
>>> print tests # doctest: +ELLIPSIS
|
>>> print(tests) # doctest: +ELLIPSIS
|
||||||
[<DocTest sample_func from ...:13 (1 example)>]
|
[<DocTest sample_func from ...:13 (1 example)>]
|
||||||
|
|
||||||
The exact name depends on how test_doctest was invoked, so allow for
|
The exact name depends on how test_doctest was invoked, so allow for
|
||||||
|
@ -420,7 +420,7 @@ methods, classmethods, staticmethods, properties, and nested classes.
|
||||||
>>> finder = doctest.DocTestFinder()
|
>>> finder = doctest.DocTestFinder()
|
||||||
>>> tests = finder.find(SampleClass)
|
>>> tests = finder.find(SampleClass)
|
||||||
>>> for t in tests:
|
>>> for t in tests:
|
||||||
... print '%2s %s' % (len(t.examples), t.name)
|
... print('%2s %s' % (len(t.examples), t.name))
|
||||||
3 SampleClass
|
3 SampleClass
|
||||||
3 SampleClass.NestedClass
|
3 SampleClass.NestedClass
|
||||||
1 SampleClass.NestedClass.__init__
|
1 SampleClass.NestedClass.__init__
|
||||||
|
@ -435,7 +435,7 @@ New-style classes are also supported:
|
||||||
|
|
||||||
>>> tests = finder.find(SampleNewStyleClass)
|
>>> tests = finder.find(SampleNewStyleClass)
|
||||||
>>> for t in tests:
|
>>> for t in tests:
|
||||||
... print '%2s %s' % (len(t.examples), t.name)
|
... print('%2s %s' % (len(t.examples), t.name))
|
||||||
1 SampleNewStyleClass
|
1 SampleNewStyleClass
|
||||||
1 SampleNewStyleClass.__init__
|
1 SampleNewStyleClass.__init__
|
||||||
1 SampleNewStyleClass.double
|
1 SampleNewStyleClass.double
|
||||||
|
@ -474,7 +474,7 @@ functions, classes, and the `__test__` dictionary, if it exists:
|
||||||
>>> import test.test_doctest
|
>>> import test.test_doctest
|
||||||
>>> tests = finder.find(m, module=test.test_doctest)
|
>>> tests = finder.find(m, module=test.test_doctest)
|
||||||
>>> for t in tests:
|
>>> for t in tests:
|
||||||
... print '%2s %s' % (len(t.examples), t.name)
|
... print('%2s %s' % (len(t.examples), t.name))
|
||||||
1 some_module
|
1 some_module
|
||||||
3 some_module.SampleClass
|
3 some_module.SampleClass
|
||||||
3 some_module.SampleClass.NestedClass
|
3 some_module.SampleClass.NestedClass
|
||||||
|
@ -496,9 +496,9 @@ will only be generated for it once:
|
||||||
|
|
||||||
>>> from test import doctest_aliases
|
>>> from test import doctest_aliases
|
||||||
>>> tests = excl_empty_finder.find(doctest_aliases)
|
>>> tests = excl_empty_finder.find(doctest_aliases)
|
||||||
>>> print len(tests)
|
>>> print(len(tests))
|
||||||
2
|
2
|
||||||
>>> print tests[0].name
|
>>> print(tests[0].name)
|
||||||
test.doctest_aliases.TwoNames
|
test.doctest_aliases.TwoNames
|
||||||
|
|
||||||
TwoNames.f and TwoNames.g are bound to the same object.
|
TwoNames.f and TwoNames.g are bound to the same object.
|
||||||
|
@ -514,7 +514,7 @@ By default, an object with no doctests doesn't create any tests:
|
||||||
|
|
||||||
>>> tests = doctest.DocTestFinder().find(SampleClass)
|
>>> tests = doctest.DocTestFinder().find(SampleClass)
|
||||||
>>> for t in tests:
|
>>> for t in tests:
|
||||||
... print '%2s %s' % (len(t.examples), t.name)
|
... print('%2s %s' % (len(t.examples), t.name))
|
||||||
3 SampleClass
|
3 SampleClass
|
||||||
3 SampleClass.NestedClass
|
3 SampleClass.NestedClass
|
||||||
1 SampleClass.NestedClass.__init__
|
1 SampleClass.NestedClass.__init__
|
||||||
|
@ -532,7 +532,7 @@ displays.
|
||||||
|
|
||||||
>>> tests = doctest.DocTestFinder(exclude_empty=False).find(SampleClass)
|
>>> tests = doctest.DocTestFinder(exclude_empty=False).find(SampleClass)
|
||||||
>>> for t in tests:
|
>>> for t in tests:
|
||||||
... print '%2s %s' % (len(t.examples), t.name)
|
... print('%2s %s' % (len(t.examples), t.name))
|
||||||
3 SampleClass
|
3 SampleClass
|
||||||
3 SampleClass.NestedClass
|
3 SampleClass.NestedClass
|
||||||
1 SampleClass.NestedClass.__init__
|
1 SampleClass.NestedClass.__init__
|
||||||
|
@ -552,7 +552,7 @@ using the `recurse` flag:
|
||||||
|
|
||||||
>>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass)
|
>>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass)
|
||||||
>>> for t in tests:
|
>>> for t in tests:
|
||||||
... print '%2s %s' % (len(t.examples), t.name)
|
... print('%2s %s' % (len(t.examples), t.name))
|
||||||
3 SampleClass
|
3 SampleClass
|
||||||
|
|
||||||
Line numbers
|
Line numbers
|
||||||
|
@ -603,9 +603,9 @@ text:
|
||||||
>>> parser = doctest.DocTestParser()
|
>>> parser = doctest.DocTestParser()
|
||||||
>>> for piece in parser.parse(s):
|
>>> for piece in parser.parse(s):
|
||||||
... if isinstance(piece, doctest.Example):
|
... if isinstance(piece, doctest.Example):
|
||||||
... print 'Example:', (piece.source, piece.want, piece.lineno)
|
... print('Example:', (piece.source, piece.want, piece.lineno))
|
||||||
... else:
|
... else:
|
||||||
... print ' Text:', repr(piece)
|
... print(' Text:', repr(piece))
|
||||||
Text: '\n'
|
Text: '\n'
|
||||||
Example: ('x, y = 2, 3 # no output expected\n', '', 1)
|
Example: ('x, y = 2, 3 # no output expected\n', '', 1)
|
||||||
Text: ''
|
Text: ''
|
||||||
|
@ -617,7 +617,7 @@ text:
|
||||||
The `get_examples` method returns just the examples:
|
The `get_examples` method returns just the examples:
|
||||||
|
|
||||||
>>> for piece in parser.get_examples(s):
|
>>> for piece in parser.get_examples(s):
|
||||||
... print (piece.source, piece.want, piece.lineno)
|
... print((piece.source, piece.want, piece.lineno))
|
||||||
('x, y = 2, 3 # no output expected\n', '', 1)
|
('x, y = 2, 3 # no output expected\n', '', 1)
|
||||||
('if 1:\n print x\n print y\n', '2\n3\n', 2)
|
('if 1:\n print x\n print y\n', '2\n3\n', 2)
|
||||||
('x+y\n', '5\n', 9)
|
('x+y\n', '5\n', 9)
|
||||||
|
@ -629,7 +629,7 @@ given arguments:
|
||||||
>>> (test.name, test.filename, test.lineno)
|
>>> (test.name, test.filename, test.lineno)
|
||||||
('name', 'filename', 5)
|
('name', 'filename', 5)
|
||||||
>>> for piece in test.examples:
|
>>> for piece in test.examples:
|
||||||
... print (piece.source, piece.want, piece.lineno)
|
... print((piece.source, piece.want, piece.lineno))
|
||||||
('x, y = 2, 3 # no output expected\n', '', 1)
|
('x, y = 2, 3 # no output expected\n', '', 1)
|
||||||
('if 1:\n print x\n print y\n', '2\n3\n', 2)
|
('if 1:\n print x\n print y\n', '2\n3\n', 2)
|
||||||
('x+y\n', '5\n', 9)
|
('x+y\n', '5\n', 9)
|
||||||
|
@ -996,7 +996,7 @@ treated as equal:
|
||||||
(0, 1)
|
(0, 1)
|
||||||
|
|
||||||
An example from the docs:
|
An example from the docs:
|
||||||
>>> print range(20) #doctest: +NORMALIZE_WHITESPACE
|
>>> print(range(20)) #doctest: +NORMALIZE_WHITESPACE
|
||||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
||||||
|
|
||||||
|
@ -1029,21 +1029,21 @@ output to match any substring in the actual output:
|
||||||
... also matches nothing:
|
... also matches nothing:
|
||||||
|
|
||||||
>>> for i in range(100):
|
>>> for i in range(100):
|
||||||
... print i**2, #doctest: +ELLIPSIS
|
... print(i**2, end=' ') #doctest: +ELLIPSIS
|
||||||
0 1...4...9 16 ... 36 49 64 ... 9801
|
0 1...4...9 16 ... 36 49 64 ... 9801
|
||||||
|
|
||||||
... can be surprising; e.g., this test passes:
|
... can be surprising; e.g., this test passes:
|
||||||
|
|
||||||
>>> for i in range(21): #doctest: +ELLIPSIS
|
>>> for i in range(21): #doctest: +ELLIPSIS
|
||||||
... print i,
|
... print(i, end=' ')
|
||||||
0 1 2 ...1...2...0
|
0 1 2 ...1...2...0
|
||||||
|
|
||||||
Examples from the docs:
|
Examples from the docs:
|
||||||
|
|
||||||
>>> print range(20) # doctest:+ELLIPSIS
|
>>> print(range(20)) # doctest:+ELLIPSIS
|
||||||
[0, 1, ..., 18, 19]
|
[0, 1, ..., 18, 19]
|
||||||
|
|
||||||
>>> print range(20) # doctest: +ELLIPSIS
|
>>> print(range(20)) # doctest: +ELLIPSIS
|
||||||
... # doctest: +NORMALIZE_WHITESPACE
|
... # doctest: +NORMALIZE_WHITESPACE
|
||||||
[0, 1, ..., 18, 19]
|
[0, 1, ..., 18, 19]
|
||||||
|
|
||||||
|
@ -1063,7 +1063,7 @@ which would be unavailable.) The SKIP flag can also be used for
|
||||||
UncheckedBlowUpError: Nobody checks me.
|
UncheckedBlowUpError: Nobody checks me.
|
||||||
|
|
||||||
>>> import random
|
>>> import random
|
||||||
>>> print random.random() # doctest: +SKIP
|
>>> print(random.random()) # doctest: +SKIP
|
||||||
0.721216923889
|
0.721216923889
|
||||||
|
|
||||||
The REPORT_UDIFF flag causes failures that involve multi-line expected
|
The REPORT_UDIFF flag causes failures that involve multi-line expected
|
||||||
|
@ -1516,7 +1516,7 @@ words and expected output are converted to comments:
|
||||||
|
|
||||||
>>> import test.test_doctest
|
>>> import test.test_doctest
|
||||||
>>> name = 'test.test_doctest.sample_func'
|
>>> name = 'test.test_doctest.sample_func'
|
||||||
>>> print doctest.testsource(test.test_doctest, name)
|
>>> print(doctest.testsource(test.test_doctest, name))
|
||||||
# Blah blah
|
# Blah blah
|
||||||
#
|
#
|
||||||
print sample_func(22)
|
print sample_func(22)
|
||||||
|
@ -1527,7 +1527,7 @@ words and expected output are converted to comments:
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
|
|
||||||
>>> name = 'test.test_doctest.SampleNewStyleClass'
|
>>> name = 'test.test_doctest.SampleNewStyleClass'
|
||||||
>>> print doctest.testsource(test.test_doctest, name)
|
>>> print(doctest.testsource(test.test_doctest, name))
|
||||||
print '1\n2\n3'
|
print '1\n2\n3'
|
||||||
# Expected:
|
# Expected:
|
||||||
## 1
|
## 1
|
||||||
|
@ -1536,7 +1536,7 @@ words and expected output are converted to comments:
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
|
|
||||||
>>> name = 'test.test_doctest.SampleClass.a_classmethod'
|
>>> name = 'test.test_doctest.SampleClass.a_classmethod'
|
||||||
>>> print doctest.testsource(test.test_doctest, name)
|
>>> print(doctest.testsource(test.test_doctest, name))
|
||||||
print SampleClass.a_classmethod(10)
|
print SampleClass.a_classmethod(10)
|
||||||
# Expected:
|
# Expected:
|
||||||
## 12
|
## 12
|
||||||
|
@ -2037,7 +2037,7 @@ def test_trailing_space_in_test():
|
||||||
Trailing spaces in expected output are significant:
|
Trailing spaces in expected output are significant:
|
||||||
|
|
||||||
>>> x, y = 'foo', ''
|
>>> x, y = 'foo', ''
|
||||||
>>> print x, y
|
>>> print(x, y)
|
||||||
foo \n
|
foo \n
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -2054,7 +2054,7 @@ def test_unittest_reportflags():
|
||||||
... optionflags=doctest.DONT_ACCEPT_BLANKLINE)
|
... optionflags=doctest.DONT_ACCEPT_BLANKLINE)
|
||||||
>>> import unittest
|
>>> import unittest
|
||||||
>>> result = suite.run(unittest.TestResult())
|
>>> result = suite.run(unittest.TestResult())
|
||||||
>>> print result.failures[0][1] # doctest: +ELLIPSIS
|
>>> print(result.failures[0][1]) # doctest: +ELLIPSIS
|
||||||
Traceback ...
|
Traceback ...
|
||||||
Failed example:
|
Failed example:
|
||||||
favorite_color
|
favorite_color
|
||||||
|
@ -2071,7 +2071,7 @@ def test_unittest_reportflags():
|
||||||
Now, when we run the test:
|
Now, when we run the test:
|
||||||
|
|
||||||
>>> result = suite.run(unittest.TestResult())
|
>>> result = suite.run(unittest.TestResult())
|
||||||
>>> print result.failures[0][1] # doctest: +ELLIPSIS
|
>>> print(result.failures[0][1]) # doctest: +ELLIPSIS
|
||||||
Traceback ...
|
Traceback ...
|
||||||
Failed example:
|
Failed example:
|
||||||
favorite_color
|
favorite_color
|
||||||
|
@ -2092,7 +2092,7 @@ def test_unittest_reportflags():
|
||||||
Then the default eporting options are ignored:
|
Then the default eporting options are ignored:
|
||||||
|
|
||||||
>>> result = suite.run(unittest.TestResult())
|
>>> result = suite.run(unittest.TestResult())
|
||||||
>>> print result.failures[0][1] # doctest: +ELLIPSIS
|
>>> print(result.failures[0][1]) # doctest: +ELLIPSIS
|
||||||
Traceback ...
|
Traceback ...
|
||||||
Failed example:
|
Failed example:
|
||||||
favorite_color
|
favorite_color
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
u"""A module to test whether doctest recognizes some 2.2 features,
|
u"""A module to test whether doctest recognizes some 2.2 features,
|
||||||
like static and class methods.
|
like static and class methods.
|
||||||
|
|
||||||
>>> print 'yup' # 1
|
>>> print('yup') # 1
|
||||||
yup
|
yup
|
||||||
|
|
||||||
We include some (random) encoded (utf-8) text in the text surrounding
|
We include some (random) encoded (utf-8) text in the text surrounding
|
||||||
|
@ -17,7 +17,7 @@ from test import test_support
|
||||||
class C(object):
|
class C(object):
|
||||||
u"""Class C.
|
u"""Class C.
|
||||||
|
|
||||||
>>> print C() # 2
|
>>> print(C()) # 2
|
||||||
42
|
42
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,13 +31,13 @@ class C(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""C.__init__.
|
"""C.__init__.
|
||||||
|
|
||||||
>>> print C() # 3
|
>>> print(C()) # 3
|
||||||
42
|
42
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""
|
"""
|
||||||
>>> print C() # 4
|
>>> print(C()) # 4
|
||||||
42
|
42
|
||||||
"""
|
"""
|
||||||
return "42"
|
return "42"
|
||||||
|
@ -45,13 +45,13 @@ class C(object):
|
||||||
class D(object):
|
class D(object):
|
||||||
"""A nested D class.
|
"""A nested D class.
|
||||||
|
|
||||||
>>> print "In D!" # 5
|
>>> print("In D!") # 5
|
||||||
In D!
|
In D!
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def nested(self):
|
def nested(self):
|
||||||
"""
|
"""
|
||||||
>>> print 3 # 6
|
>>> print(3) # 6
|
||||||
3
|
3
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ class C(object):
|
||||||
"""
|
"""
|
||||||
>>> c = C() # 7
|
>>> c = C() # 7
|
||||||
>>> c.x = 12 # 8
|
>>> c.x = 12 # 8
|
||||||
>>> print c.x # 9
|
>>> print(c.x) # 9
|
||||||
-12
|
-12
|
||||||
"""
|
"""
|
||||||
return -self._x
|
return -self._x
|
||||||
|
@ -68,7 +68,7 @@ class C(object):
|
||||||
"""
|
"""
|
||||||
>>> c = C() # 10
|
>>> c = C() # 10
|
||||||
>>> c.x = 12 # 11
|
>>> c.x = 12 # 11
|
||||||
>>> print c.x # 12
|
>>> print(c.x) # 12
|
||||||
-12
|
-12
|
||||||
"""
|
"""
|
||||||
self._x = value
|
self._x = value
|
||||||
|
@ -76,7 +76,7 @@ class C(object):
|
||||||
x = property(getx, setx, doc="""\
|
x = property(getx, setx, doc="""\
|
||||||
>>> c = C() # 13
|
>>> c = C() # 13
|
||||||
>>> c.x = 12 # 14
|
>>> c.x = 12 # 14
|
||||||
>>> print c.x # 15
|
>>> print(c.x) # 15
|
||||||
-12
|
-12
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
@ -85,9 +85,9 @@ class C(object):
|
||||||
"""
|
"""
|
||||||
A static method.
|
A static method.
|
||||||
|
|
||||||
>>> print C.statm() # 16
|
>>> print(C.statm()) # 16
|
||||||
666
|
666
|
||||||
>>> print C().statm() # 17
|
>>> print(C().statm()) # 17
|
||||||
666
|
666
|
||||||
"""
|
"""
|
||||||
return 666
|
return 666
|
||||||
|
@ -97,9 +97,9 @@ class C(object):
|
||||||
"""
|
"""
|
||||||
A class method.
|
A class method.
|
||||||
|
|
||||||
>>> print C.clsm(22) # 18
|
>>> print(C.clsm(22)) # 18
|
||||||
22
|
22
|
||||||
>>> print C().clsm(23) # 19
|
>>> print(C().clsm(23)) # 19
|
||||||
23
|
23
|
||||||
"""
|
"""
|
||||||
return val
|
return val
|
||||||
|
|
|
@ -6,7 +6,7 @@ Let's try a simple generator:
|
||||||
... yield 2
|
... yield 2
|
||||||
|
|
||||||
>>> for i in f():
|
>>> for i in f():
|
||||||
... print i
|
... print(i)
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
>>> g = f()
|
>>> g = f()
|
||||||
|
@ -78,7 +78,7 @@ However, they are not exactly equivalent:
|
||||||
... raise StopIteration
|
... raise StopIteration
|
||||||
... except:
|
... except:
|
||||||
... yield 42
|
... yield 42
|
||||||
>>> print list(g2())
|
>>> print(list(g2()))
|
||||||
[42]
|
[42]
|
||||||
|
|
||||||
This may be surprising at first:
|
This may be surprising at first:
|
||||||
|
@ -105,14 +105,14 @@ Generators always return to the most recent caller:
|
||||||
|
|
||||||
>>> def creator():
|
>>> def creator():
|
||||||
... r = yrange(5)
|
... r = yrange(5)
|
||||||
... print "creator", r.next()
|
... print("creator", r.next())
|
||||||
... return r
|
... return r
|
||||||
...
|
...
|
||||||
>>> def caller():
|
>>> def caller():
|
||||||
... r = creator()
|
... r = creator()
|
||||||
... for i in r:
|
... for i in r:
|
||||||
... print "caller", i
|
... print("caller", i)
|
||||||
...
|
...
|
||||||
>>> caller()
|
>>> caller()
|
||||||
creator 0
|
creator 0
|
||||||
caller 1
|
caller 1
|
||||||
|
@ -161,7 +161,7 @@ Specification: Return
|
||||||
... return
|
... return
|
||||||
... except:
|
... except:
|
||||||
... yield 1
|
... yield 1
|
||||||
>>> print list(f1())
|
>>> print(list(f1()))
|
||||||
[]
|
[]
|
||||||
|
|
||||||
because, as in any function, return simply exits, but
|
because, as in any function, return simply exits, but
|
||||||
|
@ -171,7 +171,7 @@ Specification: Return
|
||||||
... raise StopIteration
|
... raise StopIteration
|
||||||
... except:
|
... except:
|
||||||
... yield 42
|
... yield 42
|
||||||
>>> print list(f2())
|
>>> print(list(f2()))
|
||||||
[42]
|
[42]
|
||||||
|
|
||||||
because StopIteration is captured by a bare "except", as is any
|
because StopIteration is captured by a bare "except", as is any
|
||||||
|
@ -221,7 +221,7 @@ Specification: Try/Except/Finally
|
||||||
... finally:
|
... finally:
|
||||||
... yield 10
|
... yield 10
|
||||||
... yield 11
|
... yield 11
|
||||||
>>> print list(f())
|
>>> print(list(f()))
|
||||||
[1, 2, 4, 5, 8, 9, 10, 11]
|
[1, 2, 4, 5, 8, 9, 10, 11]
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
@ -270,7 +270,7 @@ Guido's binary tree example.
|
||||||
>>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
>>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
>>> # Print the nodes of the tree in in-order.
|
>>> # Print the nodes of the tree in in-order.
|
||||||
>>> for x in t:
|
>>> for x in t:
|
||||||
... print x,
|
... print(x, end=' ')
|
||||||
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
||||||
|
|
||||||
>>> # A non-recursive generator.
|
>>> # A non-recursive generator.
|
||||||
|
@ -291,7 +291,7 @@ Guido's binary tree example.
|
||||||
|
|
||||||
>>> # Exercise the non-recursive generator.
|
>>> # Exercise the non-recursive generator.
|
||||||
>>> for x in t:
|
>>> for x in t:
|
||||||
... print x,
|
... print(x, end=' ')
|
||||||
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -345,9 +345,9 @@ Next one was posted to c.l.py.
|
||||||
|
|
||||||
>>> seq = range(1, 5)
|
>>> seq = range(1, 5)
|
||||||
>>> for k in range(len(seq) + 2):
|
>>> for k in range(len(seq) + 2):
|
||||||
... print "%d-combs of %s:" % (k, seq)
|
... print("%d-combs of %s:" % (k, seq))
|
||||||
... for c in gcomb(seq, k):
|
... for c in gcomb(seq, k):
|
||||||
... print " ", c
|
... print(" ", c)
|
||||||
0-combs of [1, 2, 3, 4]:
|
0-combs of [1, 2, 3, 4]:
|
||||||
[]
|
[]
|
||||||
1-combs of [1, 2, 3, 4]:
|
1-combs of [1, 2, 3, 4]:
|
||||||
|
@ -383,7 +383,7 @@ From the Iterators list, about the types of these things.
|
||||||
<type 'generator'>
|
<type 'generator'>
|
||||||
>>> [s for s in dir(i) if not s.startswith('_')]
|
>>> [s for s in dir(i) if not s.startswith('_')]
|
||||||
['close', 'gi_frame', 'gi_running', 'next', 'send', 'throw']
|
['close', 'gi_frame', 'gi_running', 'next', 'send', 'throw']
|
||||||
>>> print i.next.__doc__
|
>>> print(i.next.__doc__)
|
||||||
x.next() -> the next value, or raise StopIteration
|
x.next() -> the next value, or raise StopIteration
|
||||||
>>> iter(i) is i
|
>>> iter(i) is i
|
||||||
True
|
True
|
||||||
|
@ -447,14 +447,14 @@ Subject: Re: PEP 255: Simple Generators
|
||||||
>>> gen = random.WichmannHill(42)
|
>>> gen = random.WichmannHill(42)
|
||||||
>>> while 1:
|
>>> while 1:
|
||||||
... for s in sets:
|
... for s in sets:
|
||||||
... print "%s->%s" % (s, s.find()),
|
... print("%s->%s" % (s, s.find()), end=' ')
|
||||||
... print
|
... print()
|
||||||
... if len(roots) > 1:
|
... if len(roots) > 1:
|
||||||
... s1 = gen.choice(roots)
|
... s1 = gen.choice(roots)
|
||||||
... roots.remove(s1)
|
... roots.remove(s1)
|
||||||
... s2 = gen.choice(roots)
|
... s2 = gen.choice(roots)
|
||||||
... s1.union(s2)
|
... s1.union(s2)
|
||||||
... print "merged", s1, "into", s2
|
... print("merged", s1, "into", s2)
|
||||||
... else:
|
... else:
|
||||||
... break
|
... break
|
||||||
A->A B->B C->C D->D E->E F->F G->G H->H I->I J->J K->K L->L M->M
|
A->A B->B C->C D->D E->E F->F G->G H->H I->I J->J K->K L->L M->M
|
||||||
|
@ -576,7 +576,7 @@ address space, and it *looked* like a very slow leak.
|
||||||
|
|
||||||
>>> result = m235()
|
>>> result = m235()
|
||||||
>>> for i in range(3):
|
>>> for i in range(3):
|
||||||
... print firstn(result, 15)
|
... print(firstn(result, 15))
|
||||||
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
||||||
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
||||||
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
||||||
|
@ -613,7 +613,7 @@ efficient.
|
||||||
|
|
||||||
>>> m235 = LazyList(m235())
|
>>> m235 = LazyList(m235())
|
||||||
>>> for i in range(5):
|
>>> for i in range(5):
|
||||||
... print [m235[j] for j in range(15*i, 15*(i+1))]
|
... print([m235[j] for j in range(15*i, 15*(i+1))])
|
||||||
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
||||||
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
||||||
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
||||||
|
@ -684,7 +684,7 @@ m235 to share a single generator".
|
||||||
|
|
||||||
>>> it = m235()
|
>>> it = m235()
|
||||||
>>> for i in range(5):
|
>>> for i in range(5):
|
||||||
... print firstn(it, 15)
|
... print(firstn(it, 15))
|
||||||
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
|
||||||
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
|
||||||
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
|
||||||
|
@ -890,13 +890,13 @@ This one caused a crash (see SF bug 567538):
|
||||||
... yield i
|
... yield i
|
||||||
...
|
...
|
||||||
>>> g = f()
|
>>> g = f()
|
||||||
>>> print g.next()
|
>>> print(g.next())
|
||||||
0
|
0
|
||||||
>>> print g.next()
|
>>> print(g.next())
|
||||||
1
|
1
|
||||||
>>> print g.next()
|
>>> print(g.next())
|
||||||
2
|
2
|
||||||
>>> print g.next()
|
>>> print(g.next())
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
StopIteration
|
StopIteration
|
||||||
"""
|
"""
|
||||||
|
@ -1291,7 +1291,7 @@ Generate the 3-bit binary numbers in order. This illustrates dumbest-
|
||||||
possible use of conjoin, just to generate the full cross-product.
|
possible use of conjoin, just to generate the full cross-product.
|
||||||
|
|
||||||
>>> for c in conjoin([lambda: iter((0, 1))] * 3):
|
>>> for c in conjoin([lambda: iter((0, 1))] * 3):
|
||||||
... print c
|
... print(c)
|
||||||
[0, 0, 0]
|
[0, 0, 0]
|
||||||
[0, 0, 1]
|
[0, 0, 1]
|
||||||
[0, 1, 0]
|
[0, 1, 0]
|
||||||
|
@ -1311,7 +1311,7 @@ generated sequence, you need to copy its results.
|
||||||
|
|
||||||
>>> for n in range(10):
|
>>> for n in range(10):
|
||||||
... all = list(gencopy(conjoin([lambda: iter((0, 1))] * n)))
|
... all = list(gencopy(conjoin([lambda: iter((0, 1))] * n)))
|
||||||
... print n, len(all), all[0] == [0] * n, all[-1] == [1] * n
|
... print(n, len(all), all[0] == [0] * n, all[-1] == [1] * n)
|
||||||
0 1 True True
|
0 1 True True
|
||||||
1 2 True True
|
1 2 True True
|
||||||
2 4 True True
|
2 4 True True
|
||||||
|
@ -1331,7 +1331,7 @@ And run an 8-queens solver.
|
||||||
>>> for row2col in q.solve():
|
>>> for row2col in q.solve():
|
||||||
... count += 1
|
... count += 1
|
||||||
... if count <= LIMIT:
|
... if count <= LIMIT:
|
||||||
... print "Solution", count
|
... print("Solution", count)
|
||||||
... q.printsolution(row2col)
|
... q.printsolution(row2col)
|
||||||
Solution 1
|
Solution 1
|
||||||
+-+-+-+-+-+-+-+-+
|
+-+-+-+-+-+-+-+-+
|
||||||
|
@ -1370,7 +1370,7 @@ Solution 2
|
||||||
| | | | |Q| | | |
|
| | | | |Q| | | |
|
||||||
+-+-+-+-+-+-+-+-+
|
+-+-+-+-+-+-+-+-+
|
||||||
|
|
||||||
>>> print count, "solutions in all."
|
>>> print(count, "solutions in all.")
|
||||||
92 solutions in all.
|
92 solutions in all.
|
||||||
|
|
||||||
And run a Knight's Tour on a 10x10 board. Note that there are about
|
And run a Knight's Tour on a 10x10 board. Note that there are about
|
||||||
|
@ -1382,7 +1382,7 @@ And run a Knight's Tour on a 10x10 board. Note that there are about
|
||||||
>>> for x in k.solve():
|
>>> for x in k.solve():
|
||||||
... count += 1
|
... count += 1
|
||||||
... if count <= LIMIT:
|
... if count <= LIMIT:
|
||||||
... print "Solution", count
|
... print("Solution", count)
|
||||||
... k.printsolution(x)
|
... k.printsolution(x)
|
||||||
... else:
|
... else:
|
||||||
... break
|
... break
|
||||||
|
@ -1460,7 +1460,7 @@ coroutine_tests = """\
|
||||||
Sending a value into a started generator:
|
Sending a value into a started generator:
|
||||||
|
|
||||||
>>> def f():
|
>>> def f():
|
||||||
... print (yield 1)
|
... print((yield 1))
|
||||||
... yield 2
|
... yield 2
|
||||||
>>> g = f()
|
>>> g = f()
|
||||||
>>> g.next()
|
>>> g.next()
|
||||||
|
@ -1507,16 +1507,16 @@ A yield expression with augmented assignment.
|
||||||
>>> seq = []
|
>>> seq = []
|
||||||
>>> c = coroutine(seq)
|
>>> c = coroutine(seq)
|
||||||
>>> c.next()
|
>>> c.next()
|
||||||
>>> print seq
|
>>> print(seq)
|
||||||
[]
|
[]
|
||||||
>>> c.send(10)
|
>>> c.send(10)
|
||||||
>>> print seq
|
>>> print(seq)
|
||||||
[10]
|
[10]
|
||||||
>>> c.send(10)
|
>>> c.send(10)
|
||||||
>>> print seq
|
>>> print(seq)
|
||||||
[10, 20]
|
[10, 20]
|
||||||
>>> c.send(10)
|
>>> c.send(10)
|
||||||
>>> print seq
|
>>> print(seq)
|
||||||
[10, 20, 30]
|
[10, 20, 30]
|
||||||
|
|
||||||
|
|
||||||
|
@ -1553,9 +1553,9 @@ Now check some throw() conditions:
|
||||||
>>> def f():
|
>>> def f():
|
||||||
... while True:
|
... while True:
|
||||||
... try:
|
... try:
|
||||||
... print (yield)
|
... print((yield))
|
||||||
... except ValueError as v:
|
... except ValueError as v:
|
||||||
... print "caught ValueError (%s)" % (v),
|
... print("caught ValueError (%s)" % (v), end=' ')
|
||||||
>>> import sys
|
>>> import sys
|
||||||
>>> g = f()
|
>>> g = f()
|
||||||
>>> g.next()
|
>>> g.next()
|
||||||
|
@ -1616,7 +1616,7 @@ Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError
|
TypeError
|
||||||
|
|
||||||
>>> print g.gi_frame
|
>>> print(g.gi_frame)
|
||||||
None
|
None
|
||||||
|
|
||||||
>>> g.send(2)
|
>>> g.send(2)
|
||||||
|
@ -1639,7 +1639,7 @@ Now let's try closing a generator:
|
||||||
>>> def f():
|
>>> def f():
|
||||||
... try: yield
|
... try: yield
|
||||||
... except GeneratorExit:
|
... except GeneratorExit:
|
||||||
... print "exiting"
|
... print("exiting")
|
||||||
|
|
||||||
>>> g = f()
|
>>> g = f()
|
||||||
>>> g.next()
|
>>> g.next()
|
||||||
|
@ -1660,7 +1660,7 @@ And finalization:
|
||||||
>>> def f():
|
>>> def f():
|
||||||
... try: yield
|
... try: yield
|
||||||
... finally:
|
... finally:
|
||||||
... print "exiting"
|
... print("exiting")
|
||||||
|
|
||||||
>>> g = f()
|
>>> g = f()
|
||||||
>>> g.next()
|
>>> g.next()
|
||||||
|
|
|
@ -157,12 +157,12 @@ Generators always return to the most recent caller:
|
||||||
|
|
||||||
>>> def creator():
|
>>> def creator():
|
||||||
... r = yrange(5)
|
... r = yrange(5)
|
||||||
... print "creator", r.next()
|
... print("creator", r.next())
|
||||||
... return r
|
... return r
|
||||||
>>> def caller():
|
>>> def caller():
|
||||||
... r = creator()
|
... r = creator()
|
||||||
... for i in r:
|
... for i in r:
|
||||||
... print "caller", i
|
... print("caller", i)
|
||||||
>>> caller()
|
>>> caller()
|
||||||
creator 0
|
creator 0
|
||||||
caller 1
|
caller 1
|
||||||
|
@ -221,7 +221,7 @@ Check that generator attributes are present
|
||||||
>>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
|
>>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
|
||||||
True
|
True
|
||||||
|
|
||||||
>>> print g.next.__doc__
|
>>> print(g.next.__doc__)
|
||||||
x.next() -> the next value, or raise StopIteration
|
x.next() -> the next value, or raise StopIteration
|
||||||
>>> import types
|
>>> import types
|
||||||
>>> isinstance(g, types.GeneratorType)
|
>>> isinstance(g, types.GeneratorType)
|
||||||
|
|
|
@ -764,24 +764,24 @@ libreftest = """ Doctest for examples in the library reference: libitertools.tex
|
||||||
|
|
||||||
>>> amounts = [120.15, 764.05, 823.14]
|
>>> amounts = [120.15, 764.05, 823.14]
|
||||||
>>> for checknum, amount in izip(count(1200), amounts):
|
>>> for checknum, amount in izip(count(1200), amounts):
|
||||||
... print 'Check %d is for $%.2f' % (checknum, amount)
|
... print('Check %d is for $%.2f' % (checknum, amount))
|
||||||
...
|
...
|
||||||
Check 1200 is for $120.15
|
Check 1200 is for $120.15
|
||||||
Check 1201 is for $764.05
|
Check 1201 is for $764.05
|
||||||
Check 1202 is for $823.14
|
Check 1202 is for $823.14
|
||||||
|
|
||||||
>>> import operator
|
>>> import operator
|
||||||
>>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
|
>>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
|
||||||
... print cube
|
... print(cube)
|
||||||
...
|
...
|
||||||
1
|
1
|
||||||
8
|
8
|
||||||
27
|
27
|
||||||
|
|
||||||
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
|
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
|
||||||
>>> for name in islice(reportlines, 3, None, 2):
|
>>> for name in islice(reportlines, 3, None, 2):
|
||||||
... print name.title()
|
... print(name.title())
|
||||||
...
|
...
|
||||||
Alex
|
Alex
|
||||||
Laura
|
Laura
|
||||||
Martin
|
Martin
|
||||||
|
@ -792,8 +792,8 @@ Samuele
|
||||||
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
|
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
|
||||||
>>> di = sorted(sorted(d.iteritems()), key=itemgetter(1))
|
>>> di = sorted(sorted(d.iteritems()), key=itemgetter(1))
|
||||||
>>> for k, g in groupby(di, itemgetter(1)):
|
>>> for k, g in groupby(di, itemgetter(1)):
|
||||||
... print k, map(itemgetter(0), g)
|
... print(k, map(itemgetter(0), g))
|
||||||
...
|
...
|
||||||
1 ['a', 'c', 'e']
|
1 ['a', 'c', 'e']
|
||||||
2 ['b', 'd', 'f']
|
2 ['b', 'd', 'f']
|
||||||
3 ['g']
|
3 ['g']
|
||||||
|
@ -803,8 +803,8 @@ Samuele
|
||||||
# same group.
|
# same group.
|
||||||
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
|
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
|
||||||
>>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
|
>>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
|
||||||
... print map(operator.itemgetter(1), g)
|
... print(map(operator.itemgetter(1), g))
|
||||||
...
|
...
|
||||||
[1]
|
[1]
|
||||||
[4, 5, 6]
|
[4, 5, 6]
|
||||||
[10]
|
[10]
|
||||||
|
|
|
@ -247,7 +247,7 @@ continue in for loop under finally shouuld be ok.
|
||||||
... finally:
|
... finally:
|
||||||
... for abc in range(10):
|
... for abc in range(10):
|
||||||
... continue
|
... continue
|
||||||
... print abc
|
... print(abc)
|
||||||
>>> test()
|
>>> test()
|
||||||
9
|
9
|
||||||
|
|
||||||
|
@ -328,11 +328,11 @@ so we need to be sure that a break is actually inside a loop. If it
|
||||||
isn't, there should be a syntax error.
|
isn't, there should be a syntax error.
|
||||||
|
|
||||||
>>> try:
|
>>> try:
|
||||||
... print 1
|
... print(1)
|
||||||
... break
|
... break
|
||||||
... print 2
|
... print(2)
|
||||||
... finally:
|
... finally:
|
||||||
... print 3
|
... print(3)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
SyntaxError: 'break' outside loop (<doctest test.test_syntax[42]>, line 3)
|
SyntaxError: 'break' outside loop (<doctest test.test_syntax[42]>, line 3)
|
||||||
|
|
|
@ -101,7 +101,7 @@ def test_request_headers_methods():
|
||||||
|
|
||||||
>>> r.has_header("Not-there")
|
>>> r.has_header("Not-there")
|
||||||
False
|
False
|
||||||
>>> print r.get_header("Not-there")
|
>>> print(r.get_header("Not-there"))
|
||||||
None
|
None
|
||||||
>>> r.get_header("Not-there", "default")
|
>>> r.get_header("Not-there", "default")
|
||||||
'default'
|
'default'
|
||||||
|
|
|
@ -1072,7 +1072,7 @@ libreftest = """ Doctest for examples in the library reference: libweakref.tex
|
||||||
...
|
...
|
||||||
>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
|
>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
|
||||||
>>> r = weakref.ref(obj)
|
>>> r = weakref.ref(obj)
|
||||||
>>> print r() is obj
|
>>> print(r() is obj)
|
||||||
True
|
True
|
||||||
|
|
||||||
>>> import weakref
|
>>> import weakref
|
||||||
|
@ -1085,7 +1085,7 @@ True
|
||||||
>>> o is o2
|
>>> o is o2
|
||||||
True
|
True
|
||||||
>>> del o, o2
|
>>> del o, o2
|
||||||
>>> print r()
|
>>> print(r())
|
||||||
None
|
None
|
||||||
|
|
||||||
>>> import weakref
|
>>> import weakref
|
||||||
|
@ -1140,9 +1140,9 @@ True
|
||||||
>>> try:
|
>>> try:
|
||||||
... id2obj(a_id)
|
... id2obj(a_id)
|
||||||
... except KeyError:
|
... except KeyError:
|
||||||
... print 'OK'
|
... print('OK')
|
||||||
... else:
|
... else:
|
||||||
... print 'WeakValueDictionary error'
|
... print('WeakValueDictionary error')
|
||||||
OK
|
OK
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -184,9 +184,9 @@ def parseliteral():
|
||||||
>>> element = ET.fromstring("<html><body>text</body></html>")
|
>>> element = ET.fromstring("<html><body>text</body></html>")
|
||||||
>>> ET.ElementTree(element).write(sys.stdout)
|
>>> ET.ElementTree(element).write(sys.stdout)
|
||||||
<html><body>text</body></html>
|
<html><body>text</body></html>
|
||||||
>>> print ET.tostring(element)
|
>>> print(ET.tostring(element))
|
||||||
<html><body>text</body></html>
|
<html><body>text</body></html>
|
||||||
>>> print ET.tostring(element, "ascii")
|
>>> print(ET.tostring(element, "ascii"))
|
||||||
<?xml version='1.0' encoding='ascii'?>
|
<?xml version='1.0' encoding='ascii'?>
|
||||||
<html><body>text</body></html>
|
<html><body>text</body></html>
|
||||||
>>> _, ids = ET.XMLID("<html><body>text</body></html>")
|
>>> _, ids = ET.XMLID("<html><body>text</body></html>")
|
||||||
|
@ -301,7 +301,7 @@ def xinclude():
|
||||||
|
|
||||||
>>> document = xinclude_loader("C1.xml")
|
>>> document = xinclude_loader("C1.xml")
|
||||||
>>> ElementInclude.include(document, xinclude_loader)
|
>>> ElementInclude.include(document, xinclude_loader)
|
||||||
>>> print serialize(ET, document) # C1
|
>>> print(serialize(ET, document)) # C1
|
||||||
<document>
|
<document>
|
||||||
<p>120 Mz is adequate for an average home user.</p>
|
<p>120 Mz is adequate for an average home user.</p>
|
||||||
<disclaimer>
|
<disclaimer>
|
||||||
|
@ -315,7 +315,7 @@ def xinclude():
|
||||||
|
|
||||||
>>> document = xinclude_loader("C2.xml")
|
>>> document = xinclude_loader("C2.xml")
|
||||||
>>> ElementInclude.include(document, xinclude_loader)
|
>>> ElementInclude.include(document, xinclude_loader)
|
||||||
>>> print serialize(ET, document) # C2
|
>>> print(serialize(ET, document)) # C2
|
||||||
<document>
|
<document>
|
||||||
<p>This document has been accessed
|
<p>This document has been accessed
|
||||||
324387 times.</p>
|
324387 times.</p>
|
||||||
|
@ -325,7 +325,7 @@ def xinclude():
|
||||||
|
|
||||||
>>> document = xinclude_loader("C3.xml")
|
>>> document = xinclude_loader("C3.xml")
|
||||||
>>> ElementInclude.include(document, xinclude_loader)
|
>>> ElementInclude.include(document, xinclude_loader)
|
||||||
>>> print serialize(ET, document) # C3
|
>>> print(serialize(ET, document)) # C3
|
||||||
<document>
|
<document>
|
||||||
<p>The following is the source of the "data.xml" resource:</p>
|
<p>The following is the source of the "data.xml" resource:</p>
|
||||||
<example><?xml version='1.0'?>
|
<example><?xml version='1.0'?>
|
||||||
|
|
|
@ -176,9 +176,9 @@ def parseliteral():
|
||||||
>>> element = ET.fromstring("<html><body>text</body></html>")
|
>>> element = ET.fromstring("<html><body>text</body></html>")
|
||||||
>>> ET.ElementTree(element).write(sys.stdout)
|
>>> ET.ElementTree(element).write(sys.stdout)
|
||||||
<html><body>text</body></html>
|
<html><body>text</body></html>
|
||||||
>>> print ET.tostring(element)
|
>>> print(ET.tostring(element))
|
||||||
<html><body>text</body></html>
|
<html><body>text</body></html>
|
||||||
>>> print ET.tostring(element, "ascii")
|
>>> print(ET.tostring(element, "ascii"))
|
||||||
<?xml version='1.0' encoding='ascii'?>
|
<?xml version='1.0' encoding='ascii'?>
|
||||||
<html><body>text</body></html>
|
<html><body>text</body></html>
|
||||||
>>> _, ids = ET.XMLID("<html><body>text</body></html>")
|
>>> _, ids = ET.XMLID("<html><body>text</body></html>")
|
||||||
|
|
Loading…
Reference in New Issue