Antoon Pardon wrote:

> Will it ever be possible to write things like:
> 
>   a = 4:9
>   for key, value in tree.items('alfa.': 'beta.'):

The first of these works fine, except you need to use the correct syntax:

>>> a = slice(4,9)
>>> range(10)[a]
[4, 5, 6, 7, 8]
>>> 

The second also works fine, provide tree is a type which supports it and 
you rewrite the call as tree.items(slice('alfa','beta.')) or perhaps 
tree['alfa':'beta.'].items(). To support slicing directly on a dictionary 
you could do:

>>> class sliceable(dict):
    def __getitem__(self, item):
        if isinstance(item, slice):
            return self.__class__((k,v) for (k,v) in self.iteritems()
                if item.start <= k < item.stop)
        return dict.__getitem__(self, item)

>>> d = sliceable({'alpha': 1, 'aaa': 2, 'beta': 3, 'bee': 4 })
>>> d['alpha':'beta']
{'alpha': 1, 'bee': 4}
>>> d['alpha':'beta.']
{'alpha': 1, 'beta': 3, 'bee': 4}
>>> for key, value in d['alpha':'beta.'].items():
        print key, value

        
alpha 1
beta 3
bee 4

It seems unlikely that this will make it into the builtin dict type, but 
you never know.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to