[Zope-dev] Re: TALES idea: tuple unpacking

Evan Simpson evan@4-am.com
Wed, 23 Jul 2003 11:07:20 -0500


Jim Penny wrote:
> Hate this.  Looks like a typecast of some kind, int is way to overused
> for this.  If you must, why not index:  ?

Hmmm.  I hadn't thought of that before, but I've certainly wanted to 
tell the path traverser whether to use attribute, index, or key access 
on several occasions (using 'items' as a dictionary key bites me 
regularly).  This suggests the following prefixes:

'key:' -- use item access with the prefixed string.
'index:' -- use item access with the prefixed integer.
'attr:' -- use attribute access with the prefixed string.

In each case, the path traversal fails if the specified access method 
fails, rather than trying other access methods.  This would allow 
"options/a_mapping/key:items/index:0" rather than 
"python:options['a_mapping']['items'][0]".

There's an alternative, longer syntax that would be more consistent with 
the adapter concept from Zope 3.  Given that prefixes are meant to be 
namespaces, 'key', 'index', and 'attr' should be elements of a namespace 
(perhaps 'by' or 'as' to keep it reasonably short) rather than prefixes 
themselves.  In this case, we would have the path expression 
"options/a_mapping/by:key/items/by:index/0".  Note that this form has 
the advantage of allowing "a_list/by:index/?i".

Cheers,

Evan @ 4-am