@@ -313,14 +313,14 @@ aspects that are visible to the programmer:
313313 of the actual file that was imported:
314314
315315 >>> import collections
316- >>> collections.__cached__
316+ >>> collections.__cached__ # doctest: +SKIP
317317 'c:/py32/lib/__pycache__/collections.cpython-32.pyc'
318318
319319* The tag that is unique to each interpreter is accessible from the :mod: `imp `
320320 module:
321321
322322 >>> import imp
323- >>> imp.get_tag()
323+ >>> imp.get_tag() # doctest: +SKIP
324324 'cpython-32'
325325
326326* Scripts that try to deduce source filename from the imported file now need to
@@ -329,7 +329,7 @@ aspects that are visible to the programmer:
329329
330330 >>> imp.source_from_cache(' c:/py32/lib/__pycache__/collections.cpython-32.pyc' )
331331 'c:/py32/lib/collections.py'
332- >>> imp.cache_from_source(' c:/py32/lib/collections.py' )
332+ >>> imp.cache_from_source(' c:/py32/lib/collections.py' ) # doctest: +SKIP
333333 'c:/py32/lib/__pycache__/collections.cpython-32.pyc'
334334
335335* The :mod: `py_compile ` and :mod: `compileall ` modules have been updated to
@@ -532,7 +532,7 @@ Some smaller changes made to the core Python language are:
532532 original object.
533533
534534 >>> with memoryview (b ' abcdefgh' ) as v:
535- print(v.tolist())
535+ ... print (v.tolist())
536536 [97, 98, 99, 100, 101, 102, 103, 104]
537537
538538 (Added by Antoine Pitrou; :issue: `9757 `.)
@@ -568,9 +568,10 @@ Some smaller changes made to the core Python language are:
568568 expect a tuple as an argument. This is a big step forward in making the C
569569 structures as flexible as their pure Python counterparts:
570570
571+ >>> import sys
571572 >>> isinstance (sys.version_info, tuple )
572573 True
573- >>> ' Version %d .%d .%d %s (%d )' % sys.version_info
574+ >>> ' Version %d .%d .%d %s (%d )' % sys.version_info # doctest: +SKIP
574575 'Version 3.2.0 final(0)'
575576
576577 (Suggested by Arfrever Frehtes Taifersar Arahesis and implemented
@@ -757,18 +758,18 @@ functools
757758
758759 >>> import functools
759760 >>> @ functools.lru_cache(maxsize = 300 )
760- >>> def get_phone_number (name ):
761- c = conn.cursor()
762- c.execute('SELECT phonenumber FROM phonelist WHERE name=?', (name,))
763- return c.fetchone()[0]
761+ ... def get_phone_number (name ):
762+ ... c = conn.cursor()
763+ ... c.execute(' SELECT phonenumber FROM phonelist WHERE name=?' , (name,))
764+ ... return c.fetchone()[0 ]
764765
765- >>> for name in user_requests:
766- get_phone_number(name) # cached lookup
766+ >>> for name in user_requests: # doctest: +SKIP
767+ ... get_phone_number(name) # cached lookup
767768
768769 To help with choosing an effective cache size, the wrapped function is
769770 instrumented for tracking cache statistics:
770771
771- >>> get_phone_number.cache_info()
772+ >>> get_phone_number.cache_info() # doctest: +SKIP
772773 CacheInfo(hits=4805, misses=980, maxsize=300, currsize=300)
773774
774775 If the phonelist table gets updated, the outdated contents of the cache can be
@@ -823,7 +824,7 @@ functools
823824 modern :term: `key function `:
824825
825826 >>> # locale-aware sort order
826- >>> sorted (iterable, key = cmp_to_key(locale.strcoll))
827+ >>> sorted (iterable, key = cmp_to_key(locale.strcoll)) # doctest: +SKIP
827828
828829 For sorting examples and a brief sorting tutorial, see the `Sorting HowTo
829830 <https://wiki.python.org/moin/HowTo/Sorting/> `_ tutorial.
@@ -861,7 +862,8 @@ collections
861862 which only have positive counts, and the latter is more suitable for use cases
862863 that allow negative counts:
863864
864- >>> tally = Counter(dogs = 5 , cat = 3 )
865+ >>> from collections import Counter
866+ >>> tally = Counter(dogs = 5 , cats = 3 )
865867 >>> tally -= Counter(dogs = 2 , cats = 8 ) # saturating subtraction
866868 >>> tally
867869 Counter({'dogs': 3})
@@ -884,6 +886,7 @@ collections
884886 an ordered dictionary can be used to track order of access by aging entries
885887 from the oldest to the most recently accessed.
886888
889+ >>> from collections import OrderedDict
887890 >>> d = OrderedDict.fromkeys([' a' , ' b' , ' X' , ' d' , ' e' ])
888891 >>> list (d)
889892 ['a', 'b', 'X', 'd', 'e']
@@ -897,6 +900,7 @@ collections
897900 :meth: `~collections.deque.count ` and :meth: `~collections.deque.reverse ` that
898901 make them more substitutable for :class: `list ` objects:
899902
903+ >>> from collections import deque
900904 >>> d = deque(' simsalabim' )
901905 >>> d.count(' s' )
902906 2
@@ -1042,20 +1046,23 @@ The :func:`~math.isfinite` function provides a reliable and fast way to detect
10421046special values. It returns *True * for regular numbers and *False * for *Nan * or
10431047*Infinity *:
10441048
1049+ >>> from math import isfinite
10451050>>> [isfinite(x) for x in (123 , 4.56 , float (' Nan' ), float (' Inf' ))]
10461051[True, True, False, False]
10471052
10481053The :func: `~math.expm1 ` function computes ``e**x-1 `` for small values of *x *
10491054without incurring the loss of precision that usually accompanies the subtraction
10501055of nearly equal quantities:
10511056
1057+ >>> from math import expm1
10521058>>> expm1(0.013671875 ) # more accurate way to compute e**x-1 for a small x
105310590.013765762467652909
10541060
10551061The :func: `~math.erf ` function computes a probability integral or `Gaussian
10561062error function <https://en.wikipedia.org/wiki/Error_function> `_. The
10571063complementary error function, :func: `~math.erfc `, is ``1 - erf(x) ``:
10581064
1065+ >>> from math import erf, erfc, sqrt
10591066>>> erf(1.0 / sqrt(2.0 )) # portion of normal distribution within 1 standard deviation
106010670.682689492137086
10611068>>> erfc(1.0 / sqrt(2.0 )) # portion of normal distribution outside 1 standard deviation
@@ -1069,6 +1076,7 @@ the function is related to factorials, it grows large even for small values of
10691076*x *, so there is also a :func: `~math.lgamma ` function for computing the natural
10701077logarithm of the gamma function:
10711078
1079+ >>> from math import gamma, lgamma
10721080>>> gamma(7.0 ) # six factorial
10731081720.0
10741082>>> lgamma(801.0 ) # log(800 factorial)
@@ -1287,7 +1295,7 @@ Some of the hashing details are exposed through a new attribute,
12871295prime modulus, the hash values for *infinity * and *nan *, and the multiplier
12881296used for the imaginary part of a number:
12891297
1290- >>> sys.hash_info
1298+ >>> sys.hash_info # doctest: +SKIP
12911299sys.hash_info(width=64, modulus=2305843009213693951, inf=314159, nan=0, imag=1000003)
12921300
12931301An early decision to limit the inter-operability of various numeric types has
@@ -1310,6 +1318,8 @@ Similar changes were made to :class:`fractions.Fraction` so that the
13101318:meth: `~fractions.Fraction.from_float() ` and :meth: `~fractions.Fraction.from_decimal `
13111319methods are no longer needed (:issue: `8294 `):
13121320
1321+ >>> from decimal import Decimal
1322+ >>> from fractions import Fraction
13131323>>> Decimal(1.1 )
13141324Decimal('1.100000000000000088817841970012523233890533447265625')
13151325>>> Fraction(1.1 )
@@ -1392,6 +1402,7 @@ The :mod:`gzip` module also gains the :func:`~gzip.compress` and
13921402decompression. Keep in mind that text needs to be encoded as :class: `bytes `
13931403before compressing and decompressing:
13941404
1405+ >>> import gzip
13951406>>> s = ' Three shall be the number thou shalt count, '
13961407>>> s += ' and the number of the counting shall be three'
13971408>>> b = s.encode() # convert to utf-8
@@ -1401,7 +1412,7 @@ before compressing and decompressing:
14011412>>> len (c)
1402141377
14031414>>> gzip.decompress(c).decode()[:42 ] # decompress and convert to text
1404- 'Three shall be the number thou shalt count, '
1415+ 'Three shall be the number thou shalt count'
14051416
14061417(Contributed by Anand B. Pillai in :issue: `3488 `; and by Antoine Pitrou, Nir
14071418Aides and Brian Curtin in :issue: `9962 `, :issue: `1675951 `, :issue: `7471 ` and
@@ -1503,6 +1514,7 @@ variables. The :mod:`os` module provides two new functions,
15031514:func: `~os.fsencode ` and :func: `~os.fsdecode `, for encoding and decoding
15041515filenames:
15051516
1517+ >>> import os
15061518>>> filename = ' Sehenswürdigkeiten'
15071519>>> os.fsencode(filename)
15081520b'Sehensw\xc3\xbcrdigkeiten'
@@ -1740,6 +1752,7 @@ names.
17401752 :class: `unittest.case.TestCase ` class can now be instantiated without
17411753 arguments:
17421754
1755+ >>> from unittest import TestCase
17431756 >>> TestCase().assertEqual(pow (2 , 3 ), 8 )
17441757
17451758 (Contributed by Michael Foord.)
@@ -2201,7 +2214,7 @@ The :func:`~urllib.parse.urlparse` function now supports `IPv6
22012214<https://en.wikipedia.org/wiki/IPv6> `_ addresses as described in :rfc: `2732 `:
22022215
22032216 >>> import urllib.parse
2204- >>> urllib.parse.urlparse(' http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/' )
2217+ >>> urllib.parse.urlparse(' http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/' ) # doctest: +NORMALIZE_WHITESPACE
22052218 ParseResult(scheme='http',
22062219 netloc='[dead:beef:cafe:5417:affe:8FA3:deaf:feed]',
22072220 path='/foo/',
@@ -2235,7 +2248,7 @@ functions now accept ASCII-encoded byte strings as input, so long as they are
22352248not mixed with regular strings. If ASCII-encoded byte strings are given as
22362249parameters, the return types will also be an ASCII-encoded byte strings:
22372250
2238- >>> urllib.parse.urlparse(b ' http://www.python.org:80/about/' )
2251+ >>> urllib.parse.urlparse(b ' http://www.python.org:80/about/' ) # doctest: +NORMALIZE_WHITESPACE
22392252 ParseResultBytes(scheme=b'http', netloc=b'www.python.org:80',
22402253 path=b'/about/', params=b'', query=b'', fragment=b'')
22412254
0 commit comments