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

R

Ron Adam

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
 
B

Brian Beck

Ron said:
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?

I don't think this would be as useful as you think, for Path objects at
least. Path objects represent *a* path, and building a tree as you have
proposed involves storing much more information than necessary. For
instance, if I have the path /A/B/X, a tree-structured object would
logically also store the siblings of X and B (subpaths of B and A).
 
R

Ron Adam

Brian said:
I don't think this would be as useful as you think, for Path objects at
least. Path objects represent *a* path, and building a tree as you have
proposed involves storing much more information than necessary. For
instance, if I have the path /A/B/X, a tree-structured object would
logically also store the siblings of X and B (subpaths of B and A).


But you miss understand... I'm not suggesting a path object be a tree,
but it should/could be used as a key for accessing data stored in a tree
structure.

The path object it self would be much the same thing as what is already
being discussed. Using it as a key to a tree data structure is an
additional use for it.

Regards,
Ron
 
R

Ron Adam

Here's an example of how path objects could possibly be used to store
and retrieve data from tree_dictionary class.

I used lists here in place of path objects, but I think path objects
would be better. I think paths used this way creates a consistant way
to access data stored in both internal and external tree structurs.

A more realistic example would be to store formated (html) strings in
the tree and then use imbeded paths to jump from page to page.

The tree class defined here is not complete, it's just the minimum to
get this example to work. I wouldn't mind at all if anyone posted
improvements. (hint hint) ;-)

Cheers,
Ron Adam


+---- output ------

Define paths:
path1 = ['hello', 'world']
path2 = ['hello', 'there', 'world']
path3 = ['hello', 'there', 'wide', 'world']

Store path keys in tree:
hello
world
None
there
world
None
wide
world
None

Get path list from tree:
['hello', 'world']
['hello', 'there', 'world']
['hello', 'there', 'wide', 'world']

Put items in tree using path:
hello
world
1
there
world
2
wide
world
3

Get items from tree using path:
path1: 1
path2: 2
path3: 3


+---- source code -----------

# Dictionary tree class (not finished)
class Tree(dict):
#TODO - remove_item method,
# remove_path method,
# __init__ method if needed.

def get_item(self, path):
d = self
for key in path[:-1]:
d=d[key]
return d[path[-1]]

def put_item(self, path, item):
d = self
for key in path[:-1]:
d=d[key]
d[path[-1]]=item

def store_path(self, path_=None):
# store item
key = path_[0]
if len(path_)==1:
self[key]=None
return
try:
self[key]
except KeyError:
self[key]=Tree()
self[key].store_path(path_[1:])

def get_paths(self, key=None, path=[], pathlist=[]):
if key==None:
key=self
keys = key.keys()
for k in keys:
if type(self[k]) is Tree:
path.append(k)
self[k].get_paths(key[k], path, pathlist)
else:
pathlist.append(path+[k])
return pathlist


def pretty_print_tree(t, tab=0):
for k in t.keys():
print ' '*tab, k
if type(t[k]) is Tree:
pretty_print_tree(t[k], tab+1)
else:
print ' '*(tab+1), t[k]

# Store data in a dictionary.

print 'Define paths:'
path1 = ['hello','world']
print 'path1 =', path1
path2 = ['hello','there','world']
print 'path2 =', path2
path3 = ['hello','there','wide','world']
print 'path3 =', path3

print '\nStore path keys in tree:'
tree = Tree()
tree.store_path(path1)
tree.store_path(path2)
tree.store_path(path3)
pretty_print_tree(tree)

print '\nGet path list from tree:'
path_list = tree.get_paths()
for path in path_list:
print path

print '\nPut items in tree using path:'
tree.put_item(path1, '1')
tree.put_item(path2, '2')
tree.put_item(path3, '3')
pretty_print_tree(tree)

print '\nGet items from tree using path:'
print 'path1:', tree.get_item(path1)
print 'path2:', tree.get_item(path2)
print 'path3:', tree.get_item(path3)


+---- end -------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top