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

Ron Adam rrr at ronadam.com
Tue Aug 2 00:27:44 EDT 2005



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 -------






More information about the Python-list mailing list