Skip to content

Commit 0ca34e2

Browse files
committed
ch18: update from book draft
1 parent d63b484 commit 0ca34e2

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

18-context-mngr/mirror.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
While active, the context manager reverses text output to
55
``stdout``::
66
7-
# BEGIN MIRROR_DEMO_1
7+
# tag::MIRROR_DEMO_1[]
88
99
>>> from mirror import LookingGlass
1010
>>> with LookingGlass() as what: # <1>
@@ -18,12 +18,12 @@
1818
>>> print('Back to normal.') # <5>
1919
Back to normal.
2020
21-
# END MIRROR_DEMO_1
21+
# end::MIRROR_DEMO_1[]
2222
2323
2424
This exposes the context manager operation::
2525
26-
# BEGIN MIRROR_DEMO_2
26+
# tag::MIRROR_DEMO_2[]
2727
2828
>>> from mirror import LookingGlass
2929
>>> manager = LookingGlass() # <1>
@@ -40,11 +40,11 @@
4040
>>> monster
4141
'JABBERWOCKY'
4242
43-
# END MIRROR_DEMO_2
43+
# end::MIRROR_DEMO_2[]
4444
4545
The context manager can handle and "swallow" exceptions.
4646
47-
# BEGIN MIRROR_DEMO_3
47+
# tag::MIRROR_DEMO_3[]
4848
4949
>>> from mirror import LookingGlass
5050
>>> with LookingGlass():
@@ -63,12 +63,12 @@
6363
...
6464
NameError: name 'no_such_name' is not defined
6565
66-
# END MIRROR_DEMO_3
66+
# end::MIRROR_DEMO_3[]
6767
6868
"""
6969

7070

71-
# BEGIN MIRROR_EX
71+
# tag::MIRROR_EX[]
7272
class LookingGlass:
7373

7474
def __enter__(self): # <1>
@@ -89,4 +89,4 @@ def __exit__(self, exc_type, exc_value, traceback): # <6>
8989
# <11>
9090

9191

92-
# END MIRROR_EX
92+
# end::MIRROR_EX[]

18-context-mngr/mirror_gen.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
While active, the context manager reverses text output to
55
``stdout``::
66
7-
# BEGIN MIRROR_GEN_DEMO_1
7+
# tag::MIRROR_GEN_DEMO_1[]
88
99
>>> from mirror_gen import looking_glass
1010
>>> with looking_glass() as what: # <1>
@@ -16,12 +16,12 @@
1616
>>> what
1717
'JABBERWOCKY'
1818
19-
# END MIRROR_GEN_DEMO_1
19+
# end::MIRROR_GEN_DEMO_1[]
2020
2121
2222
This exposes the context manager operation::
2323
24-
# BEGIN MIRROR_GEN_DEMO_2
24+
# tag::MIRROR_GEN_DEMO_2[]
2525
2626
>>> from mirror_gen import looking_glass
2727
>>> manager = looking_glass() # <1>
@@ -38,12 +38,12 @@
3838
>>> monster
3939
'JABBERWOCKY'
4040
41-
# END MIRROR_GEN_DEMO_2
41+
# end::MIRROR_GEN_DEMO_2[]
4242
4343
"""
4444

4545

46-
# BEGIN MIRROR_GEN_EX
46+
# tag::MIRROR_GEN_EX[]
4747

4848
import contextlib
4949

@@ -61,4 +61,4 @@ def reverse_write(text): # <3>
6161
sys.stdout.write = original_write # <6>
6262

6363

64-
# END MIRROR_GEN_EX
64+
# end::MIRROR_GEN_EX[]

18-context-mngr/mirror_gen_exc.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
While active, the context manager reverses text output to
55
``stdout``::
66
7-
# BEGIN MIRROR_GEN_DEMO_1
7+
# tag::MIRROR_GEN_DEMO_1[]
88
99
>>> from mirror_gen import looking_glass
1010
>>> with looking_glass() as what: # <1>
@@ -16,12 +16,12 @@
1616
>>> what
1717
'JABBERWOCKY'
1818
19-
# END MIRROR_GEN_DEMO_1
19+
# end::MIRROR_GEN_DEMO_1[]
2020
2121
2222
This exposes the context manager operation::
2323
24-
# BEGIN MIRROR_GEN_DEMO_2
24+
# tag::MIRROR_GEN_DEMO_2[]
2525
2626
>>> from mirror_gen import looking_glass
2727
>>> manager = looking_glass() # <1>
@@ -38,15 +38,15 @@
3838
>>> monster
3939
'JABBERWOCKY'
4040
41-
# END MIRROR_GEN_DEMO_2
41+
# end::MIRROR_GEN_DEMO_2[]
4242
4343
The context manager can handle and "swallow" exceptions.
4444
The following test does not pass under doctest (a
4545
ZeroDivisionError is reported by doctest) but passes
4646
if executed by hand in the Python 3 console (the exception
4747
is handled by the context manager):
4848
49-
# BEGIN MIRROR_GEN_DEMO_3
49+
# tag::MIRROR_GEN_DEMO_3[]
5050
5151
>>> from mirror_gen import looking_glass
5252
>>> with looking_glass():
@@ -57,7 +57,7 @@
5757
ytpmuD ytpmuH
5858
Please DO NOT divide by zero!
5959
60-
# END MIRROR_GEN_DEMO_3
60+
# end::MIRROR_GEN_DEMO_3[]
6161
6262
>>> with looking_glass():
6363
... print('Humpty Dumpty')
@@ -73,7 +73,7 @@
7373
"""
7474

7575

76-
# BEGIN MIRROR_GEN_EXC
76+
# tag::MIRROR_GEN_EXC[]
7777

7878
import contextlib
7979

@@ -98,4 +98,4 @@ def reverse_write(text):
9898
print(msg) # <4>
9999

100100

101-
# END MIRROR_GEN_EXC
101+
# end::MIRROR_GEN_EXC[]

0 commit comments

Comments
 (0)