Path as a dictionary tree key? (was Re: PEP on path module for standard library)

Ron Adam rrr at ronadam.com
Mon Aug 1 17:13:42 EDT 2005



I'm wondering if a class that acts as a interface to a tree data 
structure stored in a dictionary could also be useful as a base class 
for accessing filesystems, urls, and zip (or rar) files.

Then a path object could then be used as a dictionary_tree key.

This idea seems much more useful to me than the specific file path 
object being proposed.  It could be used for all sorts of things and 
extends Python in a more general way.

So given a toy example and a not yet written Tree class which is just a 
dictionary with alternate methods for managing tree data.

D = {'a': {'b': 'data1', 'c': 'data2'}}
D_tree = Tree(D)

As:
    'a'
        'b'
            'data1'
        'c'
            'data2'

A path object to get 'data1' would be.

    path = Path('a','b')

    item = D_tree[path]

or

    item = D_tree[Path('a','b')]


That would be in place of..

    item = D[path[0]][path[1]]     -> item = D['a']['b']


This give a more general purpose for path objects.  Working out ways to 
retrieve path objects from a dictionary_tree also would be useful I 
think.  I think a Tree class would also be a useful addition as well.

Any thoughts on this?


Cheers,
Ron




More information about the Python-list mailing list