Wyatt Baldwin wrote:
>> 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. 

Unfortunately it doesn't, I def _jsonout(self, d): and I get "TypeError: 
<lambda>() takes exactly 2 arguments (1 given)" this time.

>You might want to refactor `_jsonout` into a method in your base
> controller, 

True, but another place, namely lib/, may be a good place to use such 
decorators as well.

> then `return self._jsonout(obj_to_jsonify)`.

Hmm aren't conditionally existing variables (self._jsonout) evil?

> 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.

True, but that was just an example, I try to get decorator working first 
and foremost.

<snip>
> 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).

OK, but suppose I want to @jsonify that reusable method in the code put 
in lib/. I can't do it:


IDLE 2.6.1
 >>> import pylons.decorators
 >>> @pylons.decorators.jsonify
def _jsonout(self, parlist):
        return dict(parlist)

 >>> self=None
 >>> _jsonout(self,[4,5,6])

Traceback (most recent call last):
   File "<pyshell#6>", line 1, in <module>
     _jsonout(self,[4,5,6])
   File "<string>", line 1, in <lambda>
   File 
"c:\python26\lib\site-packages\pylons-0.9.7rc4-py2.6.egg\pylons\decorators\__init__.py",
 
line 34, in jsonify
     self._py_object.response.headers['Content-Type'] = 'application/json'
AttributeError: 'NoneType' object has no attribute '_py_object'
 >>>

 >>>

 >>>
 >>> self=[]
 >>> @pylons.decorators.jsonify
def _jsonout(self, parlist):
        return dict(parlist)

 >>> _jsonout(self,[4,5,6])

Traceback (most recent call last):
   File "<pyshell#13>", line 1, in <module>
     _jsonout(self,[4,5,6])
   File "<string>", line 1, in <lambda>
   File 
"c:\python26\lib\site-packages\pylons-0.9.7rc4-py2.6.egg\pylons\decorators\__init__.py",
 
line 34, in jsonify
     self._py_object.response.headers['Content-Type'] = 'application/json'
AttributeError: 'list' object has no attribute '_py_object'


It seems that the only place where I can use pylons decorators is a 
controller method.

But if I take the path advocated by some people on this forum, namely 
dividing the app into "core functionality" modules and controllers being 
mainly interface to that core (I sure would like to do it that way), 
there is a good reason to use pylons decorators in places other than 
controller methods.

So the question is, how can I do that? Do I have to inherit from 
BaseController in the code in lib/ just to use pylons decorators?

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