So I've run into a minor TAL problem recently. I'm trying to test to see if an object is a List or a Tuple (or more generally a sequence that isn't a string), but can't figure out a way. I've tried the following: python:isinstance(obj, list) doesn't work (it doesn't recoginze 'list' as a 'class, type or tuple of classes and types'), strangely: python:isinstance(obj, str) works ('str' is ok, but not 'list' or 'tuple'?). Also: python:list(obj) works despite the fact that 'list' is not recognized as a class! using: python:type(obj) is type([]) fails because type() is not recognized in TAL. using: python:hasattr(obj, '__iter__') fails also, though I'm not sure why (probably TAL doesn't bother with variables starting with '_'). And it's impossible to pull 'ListType' etc. in using modules['types'], because of the lack of security assertions on the 'types' module. There must be a way to do a simple type check for a tuple without a python script, no? Right now I'm using: tal:define="dictish nocall:obj/keys|nothing; listish python:not dictish and len(obj) and (len(obj[0]) != 1)" Which is far too kludgy, brittle, and plain incorrect for my liking. Thanks, Alec Mitchell
Alec Mitchell wrote:
So I've run into a minor TAL problem recently. I'm trying to test to see if an object is a List or a Tuple (or more generally a sequence that isn't a string), but can't figure out a way. I've tried the following:
python:isinstance(obj, list) doesn't work (it doesn't recoginze 'list' as a 'class, type or tuple of classes and types'), strangely: python:isinstance(obj, str) works ('str' is ok, but not 'list' or 'tuple'?). Also: python:list(obj) works despite the fact that 'list' is not recognized as a class!
using: python:type(obj) is type([]) fails because type() is not recognized in TAL.
using: python:hasattr(obj, '__iter__') fails also, though I'm not sure why (probably TAL doesn't bother with variables starting with '_').
And it's impossible to pull 'ListType' etc. in using modules['types'], because of the lack of security assertions on the 'types' module.
There must be a way to do a simple type check for a tuple without a python script, no? Right now I'm using:
tal:define="dictish nocall:obj/keys|nothing; listish python:not dictish and len(obj) and (len(obj[0]) != 1)"
Which is far too kludgy, brittle, and plain incorrect for my liking.
'same_type' is what you want: http://www.plope.com/Books/2_7Edition/BasicScripting.stx Tres. -- =============================================================== Tres Seaver tseaver@zope.com Zope Corporation "Zope Dealers" http://www.zope.com
On Friday 12 November 2004 11:12 am, Tres Seaver wrote:
'same_type' is what you want:
Wow, now I feel all remedial. I guess that's what knowing some python before learning Zope gets me. Thanks, very useful. Alec
Alec Mitchell wrote:
On Friday 12 November 2004 11:12 am, Tres Seaver wrote:
'same_type' is what you want:
Wow, now I feel all remedial. I guess that's what knowing some python before learning Zope gets me. Thanks, very useful.
I wouldn't feel badly that you didn't know about an edge case like 'same_type'. Some weirdness like that is due to the fact that Zope tries to allow "untrusted" code to do most of what "normal" Python can do, but the "normal" Python facilities are potentially exploitable to break out of the "untrusted" jail, so we have to provide workarounds. Tres. -- =============================================================== Tres Seaver tseaver@zope.com Zope Corporation "Zope Dealers" http://www.zope.com
Hi, Am Fr, den 12.11.2004 schrieb Alec Mitchell um 20:01: ...
There must be a way to do a simple type check for a tuple without a python script, no? Right now I'm using:
tal:define="dictish nocall:obj/keys|nothing; listish python:not dictish and len(obj) and (len(obj[0]) != 1)"
Which is far too kludgy, brittle, and plain incorrect for my liking.
Hm. apart from what Tres gave you (same_type) why this fear on python scripts? I mean, where you get the objects from - wouldn't it be better to provide all information you need for your ZPT in a convenient format? If you read it directly from request/form, you can also get the types right in the first place - with :list, :records etc. just 2ct ;) Regards Tino
participants (3)
-
Alec Mitchell -
Tino Wildenhain -
Tres Seaver