On Jan 15, 3:55 am, mk <[email protected]> wrote:
> Hello,
>
> In the interactive pylons debugger:
>
>  >>> dir(self)
> ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
> '__format__', '__getattribute__', '__hash__', '__init__', '__module__',
> '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
> '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
> '_dispatch_call', '_get_method_args', '_inspect_call', '_jsonout',
> '_perform_call', '_py_object', '_pylons_log_debug', 'a1', 'a2', 'b1',
> 'b2', 'c1', 'index', 'jsontest', 'jsontest2', 'jsontest3',
> 'start_response', 'test_abort']
>
>  >>> self.__class__
> <class 'helloworld.controllers.world.WorldController'>
>
> However, if *elsewhere* I inherit from BaseController or from anything
> that has _py_object attribute in the interactive debugger, this
> _py_object attribute is missing.
>
> Where does this attribute come from? For the life of me I can't find the
> place in the source that creates this attribute, all the places I could
> find were merely using (or setting) it.
>
>  From what I can read in the source, it seems that most objects in
> Pylons are StackedObjectProxy objects.
>
> I try to get through this rigmarole merely to use @pylons.decorators in
> the code in lib/ directory, because the decorators have to get an object
> with _py_object attr as the first argument (obviously, typically in
> pylons that's 'self').

Running

    find . -name '*.py' | xargs grep '_py_object ='

in lib/python2.5/site-packages/Pylons-0.9.7rc4-py2.5.egg/pylons
returns:

    ./controllers/core.py

Looking in ./controllers/core.py, we find this on line 175, in
WSGIController.__call__:

    self._py_object = environ['pylons.pylons']

Those decorators are specifically designed to work with Pylons
controllers in a WSGI request context. You *could* use them elsewhere,
after jumping through some hoops, but I'm not sure why you'd want to.

In my current Pylons project, I have a method like this in my
BaseController:

    @jsonify
    def response(self, results, **extra_data):
        result_count = len(results)
        return dict(response=dict(
            result_count=result_count,
            results=results,
            **extra_data
        ))

Then my controllers can just `return self.response(results)`.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to