mirror of https://github.com/python/cpython
gh-99087: Add missing newline for prompts in docs (GH-98993)
Add newline for prompts so copying to REPL does not cause errors.
This commit is contained in:
parent
3e06b5030b
commit
286e3c76a9
|
@ -158,6 +158,7 @@ And a function to display the chores for a given day::
|
|||
... for chore, days in chores.items():
|
||||
... if day in days:
|
||||
... print(chore)
|
||||
...
|
||||
>>> show_chores(chores_for_ethan, Weekday.SATURDAY)
|
||||
answer SO questions
|
||||
|
||||
|
@ -712,6 +713,7 @@ It is also possible to name the combinations::
|
|||
... W = 2
|
||||
... X = 1
|
||||
... RWX = 7
|
||||
...
|
||||
>>> Perm.RWX
|
||||
<Perm.RWX: 7>
|
||||
>>> ~Perm.RWX
|
||||
|
|
|
@ -565,6 +565,7 @@ arguments they contain. For example::
|
|||
|
||||
>>> with open('args.txt', 'w', encoding=sys.getfilesystemencoding()) as fp:
|
||||
... fp.write('-f\nbar')
|
||||
...
|
||||
>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
|
||||
>>> parser.add_argument('-f')
|
||||
>>> parser.parse_args(['-f', 'foo', '@args.txt'])
|
||||
|
|
|
@ -320,9 +320,11 @@ Writing and reading a bzip2-compressed file in binary mode:
|
|||
>>> with bz2.open("myfile.bz2", "wb") as f:
|
||||
... # Write compressed data to file
|
||||
... unused = f.write(data)
|
||||
...
|
||||
>>> with bz2.open("myfile.bz2", "rb") as f:
|
||||
... # Decompress data from file
|
||||
... content = f.read()
|
||||
...
|
||||
>>> content == data # Check equality to original object after round-trip
|
||||
True
|
||||
|
||||
|
|
|
@ -229,6 +229,7 @@ For example::
|
|||
>>> cnt = Counter()
|
||||
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
|
||||
... cnt[word] += 1
|
||||
...
|
||||
>>> cnt
|
||||
Counter({'blue': 3, 'red': 2, 'green': 1})
|
||||
|
||||
|
@ -818,6 +819,7 @@ zero):
|
|||
|
||||
>>> def constant_factory(value):
|
||||
... return lambda: value
|
||||
...
|
||||
>>> d = defaultdict(constant_factory('<missing>'))
|
||||
>>> d.update(name='John', action='ran')
|
||||
>>> '%(name)s %(action)s to %(object)s' % d
|
||||
|
|
|
@ -765,6 +765,7 @@ Example of counting days to an event::
|
|||
>>> my_birthday = date(today.year, 6, 24)
|
||||
>>> if my_birthday < today:
|
||||
... my_birthday = my_birthday.replace(year=today.year + 1)
|
||||
...
|
||||
>>> my_birthday
|
||||
datetime.date(2008, 6, 24)
|
||||
>>> time_to_birthday = abs(my_birthday - today)
|
||||
|
|
|
@ -2057,6 +2057,7 @@ to handle the :meth:`quantize` step:
|
|||
|
||||
>>> def mul(x, y, fp=TWOPLACES):
|
||||
... return (x * y).quantize(fp)
|
||||
...
|
||||
>>> def div(x, y, fp=TWOPLACES):
|
||||
... return (x / y).quantize(fp)
|
||||
|
||||
|
|
|
@ -351,6 +351,7 @@ The fine print:
|
|||
|
||||
>>> def f(x):
|
||||
... r'''Backslashes in a raw docstring: m\n'''
|
||||
...
|
||||
>>> print(f.__doc__)
|
||||
Backslashes in a raw docstring: m\n
|
||||
|
||||
|
@ -360,6 +361,7 @@ The fine print:
|
|||
|
||||
>>> def f(x):
|
||||
... '''Backslashes in a raw docstring: m\\n'''
|
||||
...
|
||||
>>> print(f.__doc__)
|
||||
Backslashes in a raw docstring: m\n
|
||||
|
||||
|
|
|
@ -97,6 +97,7 @@ file on disk and pass it to the system ``sendmail`` program on a Unix system:
|
|||
>>> from subprocess import Popen, PIPE
|
||||
>>> with open('mymsg.txt', 'rb') as f:
|
||||
... msg = message_from_binary_file(f, policy=policy.default)
|
||||
...
|
||||
>>> p = Popen(['sendmail', msg['To'].addresses[0]], stdin=PIPE)
|
||||
>>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
|
||||
>>> g.flatten(msg)
|
||||
|
|
|
@ -292,6 +292,7 @@ Data Types
|
|||
... @classmethod
|
||||
... def today(cls):
|
||||
... print('today is %s' % cls(date.today().isoweekday()).name)
|
||||
...
|
||||
>>> dir(Weekday.SATURDAY)
|
||||
['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']
|
||||
|
||||
|
@ -312,6 +313,7 @@ Data Types
|
|||
... return (count + 1) * 3
|
||||
... FIRST = auto()
|
||||
... SECOND = auto()
|
||||
...
|
||||
>>> PowersOfThree.SECOND.value
|
||||
6
|
||||
|
||||
|
@ -336,6 +338,7 @@ Data Types
|
|||
... if member.value == value:
|
||||
... return member
|
||||
... return None
|
||||
...
|
||||
>>> Build.DEBUG.value
|
||||
'debug'
|
||||
>>> Build('deBUG')
|
||||
|
@ -353,6 +356,7 @@ Data Types
|
|||
... def __repr__(self):
|
||||
... cls_name = self.__class__.__name__
|
||||
... return f'{cls_name}.{self.name}'
|
||||
...
|
||||
>>> OtherStyle.ALTERNATE, str(OtherStyle.ALTERNATE), f"{OtherStyle.ALTERNATE}"
|
||||
(OtherStyle.ALTERNATE, 'OtherStyle.ALTERNATE', 'OtherStyle.ALTERNATE')
|
||||
|
||||
|
@ -367,6 +371,7 @@ Data Types
|
|||
... SOMETHING_ELSE = auto()
|
||||
... def __str__(self):
|
||||
... return f'{self.name}'
|
||||
...
|
||||
>>> OtherStyle.ALTERNATE, str(OtherStyle.ALTERNATE), f"{OtherStyle.ALTERNATE}"
|
||||
(<OtherStyle.ALTERNATE: 1>, 'ALTERNATE', 'ALTERNATE')
|
||||
|
||||
|
@ -381,6 +386,7 @@ Data Types
|
|||
... SOMETHING_ELSE = auto()
|
||||
... def __format__(self, spec):
|
||||
... return f'{self.name}'
|
||||
...
|
||||
>>> OtherStyle.ALTERNATE, str(OtherStyle.ALTERNATE), f"{OtherStyle.ALTERNATE}"
|
||||
(<OtherStyle.ALTERNATE: 1>, 'OtherStyle.ALTERNATE', 'ALTERNATE')
|
||||
|
||||
|
@ -403,6 +409,7 @@ Data Types
|
|||
... ONE = 1
|
||||
... TWO = 2
|
||||
... THREE = 3
|
||||
...
|
||||
>>> Numbers.THREE
|
||||
<Numbers.THREE: 3>
|
||||
>>> Numbers.ONE + Numbers.TWO
|
||||
|
@ -463,6 +470,7 @@ Data Types
|
|||
... RED = auto()
|
||||
... GREEN = auto()
|
||||
... BLUE = auto()
|
||||
...
|
||||
>>> purple = Color.RED | Color.BLUE
|
||||
>>> white = Color.RED | Color.GREEN | Color.BLUE
|
||||
>>> Color.GREEN in purple
|
||||
|
@ -570,6 +578,7 @@ Data Types
|
|||
... RED = auto()
|
||||
... GREEN = auto()
|
||||
... BLUE = auto()
|
||||
...
|
||||
>>> Color.RED & 2
|
||||
<Color: 0>
|
||||
>>> Color.RED | 2
|
||||
|
@ -695,6 +704,7 @@ Data Types
|
|||
... RED = auto()
|
||||
... GREEN = auto()
|
||||
... BLUE = auto()
|
||||
...
|
||||
>>> StrictFlag(2**2 + 2**4)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
|
@ -712,6 +722,7 @@ Data Types
|
|||
... RED = auto()
|
||||
... GREEN = auto()
|
||||
... BLUE = auto()
|
||||
...
|
||||
>>> ConformFlag(2**2 + 2**4)
|
||||
<ConformFlag.BLUE: 4>
|
||||
|
||||
|
@ -725,6 +736,7 @@ Data Types
|
|||
... RED = auto()
|
||||
... GREEN = auto()
|
||||
... BLUE = auto()
|
||||
...
|
||||
>>> EjectFlag(2**2 + 2**4)
|
||||
20
|
||||
|
||||
|
@ -738,6 +750,7 @@ Data Types
|
|||
... RED = auto()
|
||||
... GREEN = auto()
|
||||
... BLUE = auto()
|
||||
...
|
||||
>>> KeepFlag(2**2 + 2**4)
|
||||
<KeepFlag.BLUE|16: 20>
|
||||
|
||||
|
|
|
@ -462,6 +462,7 @@ are always available. They are listed here in alphabetical order.
|
|||
>>> class Shape:
|
||||
... def __dir__(self):
|
||||
... return ['area', 'perimeter', 'location']
|
||||
...
|
||||
>>> s = Shape()
|
||||
>>> dir(s)
|
||||
['area', 'location', 'perimeter']
|
||||
|
|
|
@ -497,6 +497,7 @@ update the hash:
|
|||
>>> h = blake2b()
|
||||
>>> for item in items:
|
||||
... h.update(item)
|
||||
...
|
||||
>>> h.hexdigest()
|
||||
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
|
||||
|
||||
|
|
|
@ -715,6 +715,7 @@ function.
|
|||
|
||||
>>> def test(a, b):
|
||||
... pass
|
||||
...
|
||||
>>> sig = signature(test)
|
||||
>>> new_sig = sig.replace(return_annotation="new return anno")
|
||||
>>> str(new_sig)
|
||||
|
@ -1054,6 +1055,7 @@ Classes and functions
|
|||
>>> from inspect import getcallargs
|
||||
>>> def f(a, b=1, *pos, **named):
|
||||
... pass
|
||||
...
|
||||
>>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
|
||||
True
|
||||
>>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
|
||||
|
|
|
@ -973,6 +973,7 @@ Functions
|
|||
>>> def dashrepl(matchobj):
|
||||
... if matchobj.group(0) == '-': return ' '
|
||||
... else: return '-'
|
||||
...
|
||||
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
|
||||
'pro--gram files'
|
||||
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
|
||||
|
@ -1672,6 +1673,7 @@ in each word of a sentence except for the first and last characters::
|
|||
... inner_word = list(m.group(2))
|
||||
... random.shuffle(inner_word)
|
||||
... return m.group(1) + "".join(inner_word) + m.group(3)
|
||||
...
|
||||
>>> text = "Professor Abdolmalek, please report your absences promptly."
|
||||
>>> re.sub(r"(\w)(\w+)(\w)", repl, text)
|
||||
'Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.'
|
||||
|
|
|
@ -397,6 +397,7 @@ Module functions
|
|||
>>> con = sqlite3.connect(":memory:")
|
||||
>>> def evil_trace(stmt):
|
||||
... 5/0
|
||||
...
|
||||
>>> con.set_trace_callback(evil_trace)
|
||||
>>> def debug(unraisable):
|
||||
... print(f"{unraisable.exc_value!r} in callback {unraisable.object.__name__}")
|
||||
|
|
|
@ -996,6 +996,7 @@ probability that the Python room will stay within its capacity limits?
|
|||
>>> seed(8675309)
|
||||
>>> def trial():
|
||||
... return choices(('Python', 'Ruby'), (p, q), k=n).count('Python')
|
||||
...
|
||||
>>> mean(trial() <= k for i in range(10_000))
|
||||
0.8398
|
||||
|
||||
|
|
|
@ -4459,6 +4459,7 @@ can be used interchangeably to index the same dictionary entry.
|
|||
>>> class Counter(dict):
|
||||
... def __missing__(self, key):
|
||||
... return 0
|
||||
...
|
||||
>>> c = Counter()
|
||||
>>> c['red']
|
||||
0
|
||||
|
@ -4716,6 +4717,7 @@ An example of dictionary view usage::
|
|||
>>> n = 0
|
||||
>>> for val in values:
|
||||
... n += val
|
||||
...
|
||||
>>> print(n)
|
||||
504
|
||||
|
||||
|
|
|
@ -1604,6 +1604,7 @@ decorator:
|
|||
>>> @patch.dict(foo, {'newkey': 'newvalue'})
|
||||
... def test():
|
||||
... assert foo == {'newkey': 'newvalue'}
|
||||
...
|
||||
>>> test()
|
||||
>>> assert foo == {}
|
||||
|
||||
|
|
|
@ -1212,6 +1212,7 @@ Example of changing the attribute "target" of every link in first paragraph::
|
|||
[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
|
||||
>>> for i in links: # Iterates through all found links
|
||||
... i.attrib["target"] = "blank"
|
||||
...
|
||||
>>> tree.write("output.xhtml")
|
||||
|
||||
.. _elementtree-qname-objects:
|
||||
|
|
|
@ -672,6 +672,7 @@ The :class:`PyZipFile` constructor takes the same parameters as the
|
|||
>>> def notests(s):
|
||||
... fn = os.path.basename(s)
|
||||
... return (not (fn == 'test' or fn.startswith('test_')))
|
||||
...
|
||||
>>> zf.writepy('myprog', filterfunc=notests)
|
||||
|
||||
The :meth:`writepy` method makes archives with file names like
|
||||
|
|
|
@ -1331,6 +1331,7 @@ changes, or look through the Subversion logs for all the details.
|
|||
>>> from inspect import getcallargs
|
||||
>>> def f(a, b=1, *pos, **named):
|
||||
... pass
|
||||
...
|
||||
>>> getcallargs(f, 1, 2, 3)
|
||||
{'a': 1, 'b': 2, 'pos': (3,), 'named': {}}
|
||||
>>> getcallargs(f, a=2, x=4)
|
||||
|
|
|
@ -468,6 +468,7 @@ Some smaller changes made to the core Python language are:
|
|||
>>> class LowerCasedDict(dict):
|
||||
... def __getitem__(self, key):
|
||||
... return dict.__getitem__(self, key.lower())
|
||||
...
|
||||
>>> lcd = LowerCasedDict(part='widgets', quantity=10)
|
||||
>>> 'There are {QUANTITY} {Part} in stock'.format_map(lcd)
|
||||
'There are 10 widgets in stock'
|
||||
|
@ -475,6 +476,7 @@ Some smaller changes made to the core Python language are:
|
|||
>>> class PlaceholderDict(dict):
|
||||
... def __missing__(self, key):
|
||||
... return '<{}>'.format(key)
|
||||
...
|
||||
>>> 'Hello {name}, welcome to {location}'.format_map(PlaceholderDict())
|
||||
'Hello <name>, welcome to <location>'
|
||||
|
||||
|
@ -1886,6 +1888,7 @@ inspect
|
|||
>>> from inspect import getgeneratorstate
|
||||
>>> def gen():
|
||||
... yield 'demo'
|
||||
...
|
||||
>>> g = gen()
|
||||
>>> getgeneratorstate(g)
|
||||
'GEN_CREATED'
|
||||
|
|
|
@ -560,6 +560,7 @@ Example with (non-bound) methods::
|
|||
>>> class C:
|
||||
... def meth(self):
|
||||
... pass
|
||||
...
|
||||
>>> C.meth.__name__
|
||||
'meth'
|
||||
>>> C.meth.__qualname__
|
||||
|
|
Loading…
Reference in New Issue