Skip to content

Fix Context Managers examples #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2074,10 +2074,10 @@ If we were writing a Python module to write TeX, we might do something like this
the environments are closed properly::

>>> def start(env):
... return '\begin{}'.format(env)
... return '\\begin{{{}}}'.format(env)

>>> def end(env):
... return '\end{}'.format(env)
... return '\\end{{{}}}'.format(env)

>>> def may_error():
... import random
Expand All @@ -2102,18 +2102,18 @@ Function Based Context Managers
-------------------------------

To create a context manager with a function, decorate with
``contextlib.contextmanager``, and yield where you want to bookend::
``contextlib.contextmanager``, and yield where you want to insert your block::

>>> import contextlib
>>> @contextlib.contextmanager
... def env(name, content):
... content.append(r'\begin{}'.format(name))
... content.append('\\begin{{{}}}'.format(name))
... try:
... yield
... except ValueError:
... pass
... finally:
... content.append(r'\end{}'.format(name))
... content.append('\\end{{{}}}'.format(name))

Our code looks better now, and there will always be a closing tag::

Expand All @@ -2122,7 +2122,7 @@ Our code looks better now, and there will always be a closing tag::
... out.append(may_error())

>>> out
['\\begincenter', 'content', '\\endcenter']
['\\begin{center}', 'content', '\\end{center}']

Class Based Context Managers
----------------------------
Expand All @@ -2135,14 +2135,14 @@ To create a class based context manager, implement the ``__enter__`` and ``__exi
... self.content = content
...
... def __enter__(self):
... self.content.append(r'\begin{}'.format(
... self.content.append('\\begin{{{}}}'.format(
... self.name))
...
... def __exit__(self, type, value, tb):
... # if error in block, t, v, & tb
... # have non None values
... # return True to hide exception
... self.content.append(r'\end{}'.format(
... self.content.append('\\end{{{}}}'.format(
... self.name))
... return True

Expand All @@ -2153,7 +2153,7 @@ The code looks the same as using the function based context manager::
... out.append(may_error())

>>> out # may_error had an issue
['\\begincenter', '\\endcenter']
['\\begin{center}', '\\end{center}']


Context objects
Expand Down