docs: ``with``, ``for``, ``self``
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by defuz · committed by defuz · parent 0bb4de1 · Browse files · 10 files·+24 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+24 added−24 removed
- other:+24 added−24 removed
Provenance
- Commit
663802e- Tree
6f2e168- Author
defuz <defuz.net@gmail.com>- Committer
defuz <defuz.net@gmail.com>- Parent
0bb4de1
10 files changed+24 added−24 removed
modified:docs/patterns/fabric.rst+3 added-3 removed
70 unmodified lines71 - `run` - executes a command on a remote server72 - `local` - executes a command on the local machine73 - `put` - uploads a file to the remote server74 - `cd` - changes the directory on the serverside. This has to be used75- in combination with the `with` statement.75+ in combination with the ``with`` statement.76 77 Running Fabfiles78 ----------------79 33 unmodified lines113 Make sure to activate watching for changes of the `.wsgi` file so114 that we can automatically reload the application by touching it.115 (See :ref:`mod_wsgi-deployment` for more information)116 117-So now the question is, where do the `application.wsgi` and118-`application.cfg` files come from?117+So now the question is, where do the :file:`application.wsgi` and118+:file:`application.cfg` files come from?119 120 The WSGI File121 -------------122 74 unmodified linesmodified:docs/tutorial/templates.rst+1 added-1 removed
54 unmodified lines55 show_entries.html56 -----------------57 58 This template extends the :file:`layout.html` template from above to display the59-messages. Note that the `for` loop iterates over the messages we passed59+messages. Note that the ``for`` loop iterates over the messages we passed60 in with the :func:`~flask.render_template` function. We also tell the61 form to submit to your `add_entry` function and use ``POST`` as HTTP62 method:63 48 unmodified linesmodified:docs/extensiondev.rst+1 added-1 removed
257 unmodified lines258 with app.app_context():259 cur = db.connection.cursor()260 cur.execute(...)261 262-At the end of the `with` block the teardown handles will be executed262+At the end of the ``with`` block the teardown handles will be executed263 automatically.264 265 Additionally, the ``init_app`` method is used to support the factory pattern266 for creating apps::154 unmodified linesmodified:docs/quickstart.rst+1 added-1 removed
508 unmodified lines509 will notice that code which depends on a request object will suddenly break510 because there is no request object. The solution is creating a request511 object yourself and binding it to the context. The easiest solution for512 unit testing is to use the :meth:`~flask.Flask.test_request_context`513-context manager. In combination with the `with` statement it will bind a513+context manager. In combination with the ``with`` statement it will bind a514 test request so that you can interact with it. Here is an example::515 516 from flask import request517 376 unmodified linesmodified:docs/reqcontext.rst+3 added-3 removed
35 unmodified lines36 us a :class:`~flask.ctx.RequestContext`:37 38 >>> ctx = app.test_request_context('/?next=http://example.com/')39 40-This context can be used in two ways. Either with the `with` statement40+This context can be used in two ways. Either with the ``with`` statement41 or by calling the :meth:`~flask.ctx.RequestContext.push` and42 :meth:`~flask.ctx.RequestContext.pop` methods:43 44 >>> ctx.push()29 unmodified lines74 return response(environ, start_response)75 76 The method :meth:`~Flask.request_context` returns a new77 :class:`~flask.ctx.RequestContext` object and uses it in combination with78-the `with` statement to bind the context. Everything that is called from79-the same thread from this point onwards until the end of the `with`78+the ``with`` statement to bind the context. Everything that is called from79+the same thread from this point onwards until the end of the ``with``80 statement will have access to the request globals (:data:`flask.request`81 and others).82 83 The request context internally works like a stack: The topmost level on140 unmodified linesmodified:docs/shell.rst+1 added-1 removed
43 unmodified lines44 us a :class:`~flask.ctx.RequestContext`:45 46 >>> ctx = app.test_request_context()47 48-Normally you would use the `with` statement to make this request object48+Normally you would use the ``with`` statement to make this request object49 active, but in the shell it's easier to use the50 :meth:`~flask.ctx.RequestContext.push` and51 :meth:`~flask.ctx.RequestContext.pop` methods by hand:52 51 unmodified linesmodified:docs/signals.rst+2 added-2 removed
75 unmodified lines76 Make sure to subscribe with an extra ``**extra`` argument so that your77 calls don't fail if Flask introduces new arguments to the signals.78 79 All the template rendering in the code issued by the application `app`80-in the body of the `with` block will now be recorded in the `templates`80+in the body of the ``with`` block will now be recorded in the `templates`81 variable. Whenever a template is rendered, the template object as well as82 context are appended to it.83 84 Additionally there is a convenient helper method62 unmodified lines147 def save(self):148 model_saved.send(self)149 150 Try to always pick a good sender. If you have a class that is emitting a151-signal, pass `self` as sender. If you are emitting a signal from a random151+signal, pass ``self`` as sender. If you are emitting a signal from a random152 function, you can pass ``current_app._get_current_object()`` as sender.153 154 .. admonition:: Passing Proxies as Senders155 197 unmodified linesmodified:docs/testing.rst+4 added-4 removed
200 unmodified lines201 --------------------202 203 Besides using the test client as shown above, there is also the204 :meth:`~flask.Flask.test_request_context` method that can be used205-in combination with the `with` statement to activate a request context205+in combination with the ``with`` statement to activate a request context206 temporarily. With this you can access the :class:`~flask.request`,207 :class:`~flask.g` and :class:`~flask.session` objects like in view208 functions. Here is a full example that demonstrates this approach::209 13 unmodified lines223 Note however that if you are using a test request context, the224 :meth:`~flask.Flask.before_request` functions are not automatically called225 same for :meth:`~flask.Flask.after_request` functions. However226 :meth:`~flask.Flask.teardown_request` functions are indeed executed when227-the test request context leaves the `with` block. If you do want the227+the test request context leaves the ``with`` block. If you do want the228 :meth:`~flask.Flask.before_request` functions to be called as well, you229 need to call :meth:`~flask.Flask.preprocess_request` yourself::230 231 app = flask.Flask(__name__)75 unmodified lines307 308 Sometimes it is helpful to trigger a regular request but still keep the309 context around for a little longer so that additional introspection can310 happen. With Flask 0.4 this is possible by using the311-:meth:`~flask.Flask.test_client` with a `with` block::311+:meth:`~flask.Flask.test_client` with a ``with`` block::312 313 app = flask.Flask(__name__)314 315 with app.test_client() as c:316 rv = c.get('/?tequila=42')317 assert request.args['tequila'] == '42'318 319 If you were to use just the :meth:`~flask.Flask.test_client` without320-the `with` block, the `assert` would fail with an error because `request`320+the ``with`` block, the ``assert`` would fail with an error because `request`321 is no longer available (because you are trying to use it outside of the actual request).322 323 324 Accessing and Modifying Sessions29 unmodified linesmodified:docs/views.rst+1 added-1 removed
69 unmodified lines70 return User.query.all()71 72 This of course is not that helpful for such a small example, but it's good73 enough to explain the basic principle. When you have a class-based view74-the question comes up what `self` points to. The way this works is that74+the question comes up what ``self`` points to. The way this works is that75 whenever the request is dispatched a new instance of the class is created76 and the :meth:`~flask.views.View.dispatch_request` method is called with77 the parameters from the URL rule. The class itself is instantiated with78 the parameters passed to the :meth:`~flask.views.View.as_view` function.159 unmodified linesmodified:flask/app.py+7 added-7 removed
836 unmodified lines837 838 app.testing = True839 client = app.test_client()840 841- The test client can be used in a `with` block to defer the closing down842- of the context until the end of the `with` block. This is useful if841+ The test client can be used in a ``with`` block to defer the closing down842+ of the context until the end of the ``with`` block. This is useful if843 you want to access the context locals for testing::844 845 with app.test_client() as c:846 rv = c.get('/?vodka=42')15 unmodified lines862 863 See :class:`~flask.testing.FlaskClient` for more information.864 865 .. versionchanged:: 0.4866- added support for `with` block usage for the client.866+ added support for ``with`` block usage for the client.867 868 .. versionadded:: 0.7869 The `use_cookies` parameter was added as well as the ability870 to override the client to be used by setting the940 unmodified lines1811 1812 def request_context(self, environ):1813 """Creates a :class:`~flask.ctx.RequestContext` from the given1814 environment and binds it to the current context. This must be used in1815- combination with the `with` statement because the request is only bound1816- to the current context for the duration of the `with` block.1815+ combination with the ``with`` statement because the request is only bound1816+ to the current context for the duration of the ``with`` block.1817 1818 Example usage::1819 1820 with app.request_context(environ):1821 do_something_with(request)1822 1823- The object returned can also be used without the `with` statement1823+ The object returned can also be used without the ``with`` statement1824 which is useful for working in the shell. The example above is1825 doing exactly the same as this code::1826 1827 ctx = app.request_context(environ)3 unmodified lines1831 finally:1832 ctx.pop()1833 1834 .. versionchanged:: 0.31835- Added support for non-with statement usage and `with` statement1835+ Added support for non-with statement usage and ``with`` statement1836 is now passed the ctx object.1837 1838 :param environ: a WSGI environment1839 """70 unmodified lines