ch18: update from book draft
This commit is contained in:
parent
d63b4844f1
commit
0ca34e2676
@ -4,7 +4,7 @@ A "mirroring" ``stdout`` context.
|
|||||||
While active, the context manager reverses text output to
|
While active, the context manager reverses text output to
|
||||||
``stdout``::
|
``stdout``::
|
||||||
|
|
||||||
# BEGIN MIRROR_DEMO_1
|
# tag::MIRROR_DEMO_1[]
|
||||||
|
|
||||||
>>> from mirror import LookingGlass
|
>>> from mirror import LookingGlass
|
||||||
>>> with LookingGlass() as what: # <1>
|
>>> with LookingGlass() as what: # <1>
|
||||||
@ -18,12 +18,12 @@ While active, the context manager reverses text output to
|
|||||||
>>> print('Back to normal.') # <5>
|
>>> print('Back to normal.') # <5>
|
||||||
Back to normal.
|
Back to normal.
|
||||||
|
|
||||||
# END MIRROR_DEMO_1
|
# end::MIRROR_DEMO_1[]
|
||||||
|
|
||||||
|
|
||||||
This exposes the context manager operation::
|
This exposes the context manager operation::
|
||||||
|
|
||||||
# BEGIN MIRROR_DEMO_2
|
# tag::MIRROR_DEMO_2[]
|
||||||
|
|
||||||
>>> from mirror import LookingGlass
|
>>> from mirror import LookingGlass
|
||||||
>>> manager = LookingGlass() # <1>
|
>>> manager = LookingGlass() # <1>
|
||||||
@ -40,11 +40,11 @@ This exposes the context manager operation::
|
|||||||
>>> monster
|
>>> monster
|
||||||
'JABBERWOCKY'
|
'JABBERWOCKY'
|
||||||
|
|
||||||
# END MIRROR_DEMO_2
|
# end::MIRROR_DEMO_2[]
|
||||||
|
|
||||||
The context manager can handle and "swallow" exceptions.
|
The context manager can handle and "swallow" exceptions.
|
||||||
|
|
||||||
# BEGIN MIRROR_DEMO_3
|
# tag::MIRROR_DEMO_3[]
|
||||||
|
|
||||||
>>> from mirror import LookingGlass
|
>>> from mirror import LookingGlass
|
||||||
>>> with LookingGlass():
|
>>> with LookingGlass():
|
||||||
@ -63,12 +63,12 @@ The context manager can handle and "swallow" exceptions.
|
|||||||
...
|
...
|
||||||
NameError: name 'no_such_name' is not defined
|
NameError: name 'no_such_name' is not defined
|
||||||
|
|
||||||
# END MIRROR_DEMO_3
|
# end::MIRROR_DEMO_3[]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# BEGIN MIRROR_EX
|
# tag::MIRROR_EX[]
|
||||||
class LookingGlass:
|
class LookingGlass:
|
||||||
|
|
||||||
def __enter__(self): # <1>
|
def __enter__(self): # <1>
|
||||||
@ -89,4 +89,4 @@ class LookingGlass:
|
|||||||
# <11>
|
# <11>
|
||||||
|
|
||||||
|
|
||||||
# END MIRROR_EX
|
# end::MIRROR_EX[]
|
||||||
|
@ -4,7 +4,7 @@ A "mirroring" ``stdout`` context manager.
|
|||||||
While active, the context manager reverses text output to
|
While active, the context manager reverses text output to
|
||||||
``stdout``::
|
``stdout``::
|
||||||
|
|
||||||
# BEGIN MIRROR_GEN_DEMO_1
|
# tag::MIRROR_GEN_DEMO_1[]
|
||||||
|
|
||||||
>>> from mirror_gen import looking_glass
|
>>> from mirror_gen import looking_glass
|
||||||
>>> with looking_glass() as what: # <1>
|
>>> with looking_glass() as what: # <1>
|
||||||
@ -16,12 +16,12 @@ While active, the context manager reverses text output to
|
|||||||
>>> what
|
>>> what
|
||||||
'JABBERWOCKY'
|
'JABBERWOCKY'
|
||||||
|
|
||||||
# END MIRROR_GEN_DEMO_1
|
# end::MIRROR_GEN_DEMO_1[]
|
||||||
|
|
||||||
|
|
||||||
This exposes the context manager operation::
|
This exposes the context manager operation::
|
||||||
|
|
||||||
# BEGIN MIRROR_GEN_DEMO_2
|
# tag::MIRROR_GEN_DEMO_2[]
|
||||||
|
|
||||||
>>> from mirror_gen import looking_glass
|
>>> from mirror_gen import looking_glass
|
||||||
>>> manager = looking_glass() # <1>
|
>>> manager = looking_glass() # <1>
|
||||||
@ -38,12 +38,12 @@ This exposes the context manager operation::
|
|||||||
>>> monster
|
>>> monster
|
||||||
'JABBERWOCKY'
|
'JABBERWOCKY'
|
||||||
|
|
||||||
# END MIRROR_GEN_DEMO_2
|
# end::MIRROR_GEN_DEMO_2[]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# BEGIN MIRROR_GEN_EX
|
# tag::MIRROR_GEN_EX[]
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
|
|
||||||
@ -61,4 +61,4 @@ def looking_glass():
|
|||||||
sys.stdout.write = original_write # <6>
|
sys.stdout.write = original_write # <6>
|
||||||
|
|
||||||
|
|
||||||
# END MIRROR_GEN_EX
|
# end::MIRROR_GEN_EX[]
|
||||||
|
@ -4,7 +4,7 @@ A "mirroring" ``stdout`` context manager.
|
|||||||
While active, the context manager reverses text output to
|
While active, the context manager reverses text output to
|
||||||
``stdout``::
|
``stdout``::
|
||||||
|
|
||||||
# BEGIN MIRROR_GEN_DEMO_1
|
# tag::MIRROR_GEN_DEMO_1[]
|
||||||
|
|
||||||
>>> from mirror_gen import looking_glass
|
>>> from mirror_gen import looking_glass
|
||||||
>>> with looking_glass() as what: # <1>
|
>>> with looking_glass() as what: # <1>
|
||||||
@ -16,12 +16,12 @@ While active, the context manager reverses text output to
|
|||||||
>>> what
|
>>> what
|
||||||
'JABBERWOCKY'
|
'JABBERWOCKY'
|
||||||
|
|
||||||
# END MIRROR_GEN_DEMO_1
|
# end::MIRROR_GEN_DEMO_1[]
|
||||||
|
|
||||||
|
|
||||||
This exposes the context manager operation::
|
This exposes the context manager operation::
|
||||||
|
|
||||||
# BEGIN MIRROR_GEN_DEMO_2
|
# tag::MIRROR_GEN_DEMO_2[]
|
||||||
|
|
||||||
>>> from mirror_gen import looking_glass
|
>>> from mirror_gen import looking_glass
|
||||||
>>> manager = looking_glass() # <1>
|
>>> manager = looking_glass() # <1>
|
||||||
@ -38,7 +38,7 @@ This exposes the context manager operation::
|
|||||||
>>> monster
|
>>> monster
|
||||||
'JABBERWOCKY'
|
'JABBERWOCKY'
|
||||||
|
|
||||||
# END MIRROR_GEN_DEMO_2
|
# end::MIRROR_GEN_DEMO_2[]
|
||||||
|
|
||||||
The context manager can handle and "swallow" exceptions.
|
The context manager can handle and "swallow" exceptions.
|
||||||
The following test does not pass under doctest (a
|
The following test does not pass under doctest (a
|
||||||
@ -46,7 +46,7 @@ ZeroDivisionError is reported by doctest) but passes
|
|||||||
if executed by hand in the Python 3 console (the exception
|
if executed by hand in the Python 3 console (the exception
|
||||||
is handled by the context manager):
|
is handled by the context manager):
|
||||||
|
|
||||||
# BEGIN MIRROR_GEN_DEMO_3
|
# tag::MIRROR_GEN_DEMO_3[]
|
||||||
|
|
||||||
>>> from mirror_gen import looking_glass
|
>>> from mirror_gen import looking_glass
|
||||||
>>> with looking_glass():
|
>>> with looking_glass():
|
||||||
@ -57,7 +57,7 @@ is handled by the context manager):
|
|||||||
ytpmuD ytpmuH
|
ytpmuD ytpmuH
|
||||||
Please DO NOT divide by zero!
|
Please DO NOT divide by zero!
|
||||||
|
|
||||||
# END MIRROR_GEN_DEMO_3
|
# end::MIRROR_GEN_DEMO_3[]
|
||||||
|
|
||||||
>>> with looking_glass():
|
>>> with looking_glass():
|
||||||
... print('Humpty Dumpty')
|
... print('Humpty Dumpty')
|
||||||
@ -73,7 +73,7 @@ is handled by the context manager):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# BEGIN MIRROR_GEN_EXC
|
# tag::MIRROR_GEN_EXC[]
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
|
|
||||||
@ -98,4 +98,4 @@ def looking_glass():
|
|||||||
print(msg) # <4>
|
print(msg) # <4>
|
||||||
|
|
||||||
|
|
||||||
# END MIRROR_GEN_EXC
|
# end::MIRROR_GEN_EXC[]
|
||||||
|
Loading…
Reference in New Issue
Block a user