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

Discussion in 'Python' started by Ron Adam, Aug 1, 2005.

  1. Ron Adam

    Ron Adam Guest

    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
     
    Ron Adam, Aug 1, 2005
    #1
    1. Advertising

  2. Ron Adam

    Brian Beck Guest

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

    Ron Adam wrote:
    > 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).

    --
    Brian Beck
    Adventurer of the First Order
     
    Brian Beck, Aug 1, 2005
    #2
    1. Advertising

  3. Ron Adam

    Ron Adam Guest

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

    Brian Beck wrote:
    > Ron Adam wrote:
    >
    >> 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).



    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
     
    Ron Adam, Aug 1, 2005
    #3
  4. Ron Adam

    Ron Adam Guest

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

    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 -------
     
    Ron Adam, Aug 2, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,244
  2. Christoph Becker-Freyseng

    PEP for new modules (I read PEP 2)

    Christoph Becker-Freyseng, Jan 15, 2004, in forum: Python
    Replies:
    3
    Views:
    401
    Gerrit Holl
    Jan 16, 2004
  3. Michael Hoffman

    PEP on path module for standard library

    Michael Hoffman, Jul 21, 2005, in forum: Python
    Replies:
    70
    Views:
    1,299
  4. Mike Orr
    Replies:
    7
    Views:
    331
    Andrew Dalke
    Jul 31, 2005
  5. M P
    Replies:
    1
    Views:
    559
Loading...

Share This Page