On Jan 14, 5:39 am, mk <[email protected]> wrote:
> Hello,
>
> I can use @pylons.decorators.smth on the controller action without problem:
>
> @pylons.decorators.jsonify
> def jsontest2(self):
>      return [('spamids', [1,2,3]), ('bigmess',[4]),
> ('regular_mail',[5,6,7])]
>
> But when I try this:
>
> def jsontest(self):
>     @pylons.decorators.jsonify
>     def _jsonout(d):
>        return dict(d)
>    return _jsonout({'spamids':[1,2,3], 'bigmess':[4],
> 'regular_mail':[5,6,7]})
>
> I get AttributeError: 'dict' object has no attribute '_py_object'

I think that's because the @jsonify decorator expects the decorated
function to have a `self` arg, so `def _jsonout(self, d): ...` should
work. You might want to refactor `_jsonout` into a method in your base
controller, then `return self._jsonout(obj_to_jsonify)`.

Also, you're converting `d` to a `dict` even though it's already a
`dict`, which isn't the problem here, but which is unnecessary.


> And when I try this:
>
> def jsontest(self):
>     @pylons.decorators.jsonify
>     def _jsonout(fakeself, d):
>         return dict(d)
>     return _jsonout({'spamids':[1,2,3], 'bigmess':[4],
> 'regular_mail':[5,6,7]})
>
> I get TypeError: <lambda>() takes exactly 2 arguments (1 given).

You'd need to do `return _jsonout(self, {'spamids':[1,2,3], ...)`, but
I'd still recommend making `_jsonout` a reusable method (which also
avoids creating a new decorated function on every request).


> Traceback:
>
> Module helloworld.controllers.world:54 in jsontest
>
> _jsonout        <function _jsonout at 0x01932EB0>
> self    <helloworld.controllers.world.WorldController object at 0x01935EF0>
> view
> <<          def _jsonout(fakeself, d):
>                  return dict(d)
>              return _jsonout({'spamids':[1,2,3], 'bigmess':[4],
> 'regular_mail':[5,6,7]})
>
>         �[email protected]
>  >>  return _jsonout({'spamids':[1,2,3], 'bigmess':[4],
> 'regular_mail':[5,6,7]})
> TypeError: <lambda>() takes exactly 2 arguments (1 given)
>
> So my question is how do I use built-in Pylons decorators on function
> defined *inside* a controller (or elsewhere in the code, conceivably)?
>
> Regards,
> mk
--~--~---------~--~----~------------~-------~--~----~
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