diff --git a/18-context-mngr/mirror.py b/18-context-mngr/mirror.py index 67782ff..ba31944 100644 --- a/18-context-mngr/mirror.py +++ b/18-context-mngr/mirror.py @@ -4,7 +4,7 @@ A "mirroring" ``stdout`` context. While active, the context manager reverses text output to ``stdout``:: -# BEGIN MIRROR_DEMO_1 +# tag::MIRROR_DEMO_1[] >>> from mirror import LookingGlass >>> with LookingGlass() as what: # <1> @@ -18,12 +18,12 @@ While active, the context manager reverses text output to >>> print('Back to normal.') # <5> Back to normal. -# END MIRROR_DEMO_1 +# end::MIRROR_DEMO_1[] This exposes the context manager operation:: -# BEGIN MIRROR_DEMO_2 +# tag::MIRROR_DEMO_2[] >>> from mirror import LookingGlass >>> manager = LookingGlass() # <1> @@ -40,11 +40,11 @@ This exposes the context manager operation:: >>> monster 'JABBERWOCKY' -# END MIRROR_DEMO_2 +# end::MIRROR_DEMO_2[] The context manager can handle and "swallow" exceptions. -# BEGIN MIRROR_DEMO_3 +# tag::MIRROR_DEMO_3[] >>> from mirror import LookingGlass >>> with LookingGlass(): @@ -63,12 +63,12 @@ The context manager can handle and "swallow" exceptions. ... NameError: name 'no_such_name' is not defined -# END MIRROR_DEMO_3 +# end::MIRROR_DEMO_3[] """ -# BEGIN MIRROR_EX +# tag::MIRROR_EX[] class LookingGlass: def __enter__(self): # <1> @@ -89,4 +89,4 @@ class LookingGlass: # <11> -# END MIRROR_EX +# end::MIRROR_EX[] diff --git a/18-context-mngr/mirror_gen.py b/18-context-mngr/mirror_gen.py index 379e8ab..457955a 100644 --- a/18-context-mngr/mirror_gen.py +++ b/18-context-mngr/mirror_gen.py @@ -4,7 +4,7 @@ A "mirroring" ``stdout`` context manager. While active, the context manager reverses text output to ``stdout``:: -# BEGIN MIRROR_GEN_DEMO_1 +# tag::MIRROR_GEN_DEMO_1[] >>> from mirror_gen import looking_glass >>> with looking_glass() as what: # <1> @@ -16,12 +16,12 @@ While active, the context manager reverses text output to >>> what 'JABBERWOCKY' -# END MIRROR_GEN_DEMO_1 +# end::MIRROR_GEN_DEMO_1[] This exposes the context manager operation:: -# BEGIN MIRROR_GEN_DEMO_2 +# tag::MIRROR_GEN_DEMO_2[] >>> from mirror_gen import looking_glass >>> manager = looking_glass() # <1> @@ -38,12 +38,12 @@ This exposes the context manager operation:: >>> monster 'JABBERWOCKY' -# END MIRROR_GEN_DEMO_2 +# end::MIRROR_GEN_DEMO_2[] """ -# BEGIN MIRROR_GEN_EX +# tag::MIRROR_GEN_EX[] import contextlib @@ -61,4 +61,4 @@ def looking_glass(): sys.stdout.write = original_write # <6> -# END MIRROR_GEN_EX +# end::MIRROR_GEN_EX[] diff --git a/18-context-mngr/mirror_gen_exc.py b/18-context-mngr/mirror_gen_exc.py index 9dcc560..c446918 100644 --- a/18-context-mngr/mirror_gen_exc.py +++ b/18-context-mngr/mirror_gen_exc.py @@ -4,7 +4,7 @@ A "mirroring" ``stdout`` context manager. While active, the context manager reverses text output to ``stdout``:: -# BEGIN MIRROR_GEN_DEMO_1 +# tag::MIRROR_GEN_DEMO_1[] >>> from mirror_gen import looking_glass >>> with looking_glass() as what: # <1> @@ -16,12 +16,12 @@ While active, the context manager reverses text output to >>> what 'JABBERWOCKY' -# END MIRROR_GEN_DEMO_1 +# end::MIRROR_GEN_DEMO_1[] This exposes the context manager operation:: -# BEGIN MIRROR_GEN_DEMO_2 +# tag::MIRROR_GEN_DEMO_2[] >>> from mirror_gen import looking_glass >>> manager = looking_glass() # <1> @@ -38,7 +38,7 @@ This exposes the context manager operation:: >>> monster 'JABBERWOCKY' -# END MIRROR_GEN_DEMO_2 +# end::MIRROR_GEN_DEMO_2[] The context manager can handle and "swallow" exceptions. 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 is handled by the context manager): -# BEGIN MIRROR_GEN_DEMO_3 +# tag::MIRROR_GEN_DEMO_3[] >>> from mirror_gen import looking_glass >>> with looking_glass(): @@ -57,7 +57,7 @@ is handled by the context manager): ytpmuD ytpmuH Please DO NOT divide by zero! -# END MIRROR_GEN_DEMO_3 +# end::MIRROR_GEN_DEMO_3[] >>> with looking_glass(): ... print('Humpty Dumpty') @@ -73,7 +73,7 @@ is handled by the context manager): """ -# BEGIN MIRROR_GEN_EXC +# tag::MIRROR_GEN_EXC[] import contextlib @@ -98,4 +98,4 @@ def looking_glass(): print(msg) # <4> -# END MIRROR_GEN_EXC +# end::MIRROR_GEN_EXC[]