librelist archives

« back to archive

Faking resource and context - question to the document

Faking resource and context - question to the document

From:
sean chen
Date:
2013-06-11 @ 18:10
I am reading the document and met a problem here, I wonder if I can get
help from the community :-)

from contextlib import contextmanagerfrom flask import appcontext_pushed
@contextmanagerdef user_set(app, user):
    def handler(sender, **kwargs):
        g.user = user
    with appcontext_pushed.connected_to(handler, app):
        yield


Here I have couple of questions:
  1) "sender" was never used, neither the "**kwargs", why do we need such
arguments? -- I guess I need to understand the connected_to function to
understand this part?
  2) I did not understand what user_set is trying to do here -- it yields
no object.


from flask import json, jsonify
@app.route('/users/me')def users_me():
    return jsonify(username=g.user.username)
with user_set(app, my_user):
    with app.test_client() as c:
        resp = c.get('/users/me')
        data = json.loads(resp.data)
        self.assert_equal(data['username'], my_user.username)


Now, user_set is used in "with", as I did not understand the yield (i.e.,
what does user_set yield), I guess I don't know what exactly the this whole
example is doing, though I GUESSed what it tries to do.

Thanks,
Sean

Re: [flask] Faking resource and context - question to the document

From:
Alex Ogier
Date:
2013-06-11 @ 18:47
This will make a lot more sense after reading the documentation on
contextlib and contextmanager:
http://docs.python.org/2/library/contextlib.html

Basically what it does is turn a python generator that yields once into an
object that can be used as the argument of a 'with' statement. The 'yield'
here doesn't return anything, its only purpose is to yield control to the
calling function without unwinding the global state it set up with
'appcontext_pushed.connected_to'.

As for 'sender' and '**kwargs' those are just part of the API of flask
signals, as you suspect. They aren't used here because the user_set
function just operates on the global 'g' object.
On Jun 11, 2013 2:12 PM, "sean chen" <alazylearner@gmail.com> wrote:

> I am reading the document and met a problem here, I wonder if I can get
> help from the community :-)
>
> from contextlib import contextmanagerfrom flask import appcontext_pushed
> @contextmanagerdef user_set(app, user):
>     def handler(sender, **kwargs):
>         g.user = user
>     with appcontext_pushed.connected_to(handler, app):
>         yield
>
>
> Here I have couple of questions:
>   1) "sender" was never used, neither the "**kwargs", why do we need such
> arguments? -- I guess I need to understand the connected_to function to
> understand this part?
>   2) I did not understand what user_set is trying to do here -- it yields
> no object.
>
>
> from flask import json, jsonify
> @app.route('/users/me')def users_me():
>     return jsonify(username=g.user.username)
> with user_set(app, my_user):
>     with app.test_client() as c:
>         resp = c.get('/users/me')
>         data = json.loads(resp.data)
>         self.assert_equal(data['username'], my_user.username)
>
>
> Now, user_set is used in "with", as I did not understand the yield (i.e.,
> what does user_set yield), I guess I don't know what exactly the this whole
> example is doing, though I GUESSed what it tries to do.
>
> Thanks,
> Sean
>
>
>