Iterating Through Dictionary of Lists

Discussion in 'Python' started by JB, Sep 11, 2009.

  1. JB

    JB Guest

    I have created a small program that generates a project tree from a
    dictionary. The dictionary is of key/value pairs where each key is a
    directory, and each value is a list. The list have unique values
    corresponding to the key, which is a directory where each value in the
    list becomes a subdirectory.

    The question that I have is how to do this process if one of the
    unique values in the list is itself a dict. For example, in the
    "projdir" dict below, suppose the "Analysis" value in the list
    corresponding to the "Engineering" key was itself a dict and was
    assigned {'Analysis' : 'Simulink'} for example.

    Thanks.
    James


    #-------------------------------------
    # DEFINE Root Dir & Path, Sch Numbers
    #-------------------------------------
    sch_names = ['AED500','AED600']
    dir_main = "Z:\\ABC_PROJ\\"
    dir_sub = "PCB_MDX\\"
    rootdir = dir_main + dir_sub
    #-------------------------------------
    # DEFINE Directory Tree for Project
    #-------------------------------------
    projdir = {'Project' : ['Schedule', 'Specifications',
    'Procedures'],
    'Schematics_PCB' : ['SCH','BOM','ASSY'],
    'Drawings' : ['System', 'Board', 'Packaging_3D'],
    'Engineering' : ['Analysis', 'Reports', 'Design Reviews']}
    #-------------------------------------
    # DEFINE Debug Status
    #-------------------------------------
    debug = True
    #-------------------------------------
    # Print Directory Path (Helper Func)
    #-------------------------------------
    def print_path(z,suffix):
    print z
    print z + suffix
    #-------------------------------------
    # Make a Directory Project Tree
    #-------------------------------------
    def make_tree_with_debug(proj,sch,root):
    counter = 0
    print "The dictionary was found to have the following: \n"

    #-------------------------------------------------------------------------
    # Iterate over items in the dictionary, creating tuples of key/value
    pairs

    #-------------------------------------------------------------------------
    for key, values in proj.iteritems():
    counter = counter + 1
    print "Key #" + str(counter) + " is " + "'" + key + "\'"
    print "For this key, the values are " + str(values)
    print "Thus, the results of generating directories for this key/
    values combo are: \n"
    #---------------------------------------------------------
    # Iterate over the invidividual unique values in the list
    # that is associated with each key in the dict
    #---------------------------------------------------------
    for unique in values:
    prefix = root + key + "\\" + unique
    suffix = "\\Previous \n"
    if key == 'Schematics_PCB':
    print unique
    for item in sch:
    z = prefix + "\\" + item
    print_path(z,suffix)
    else:
    #if unique.haskeys():
    # print unique.key
    z = prefix
    print_path(z,suffix)


    make_tree_with_debug(projdir,sch_names,rootdir)
     
    JB, Sep 11, 2009
    #1
    1. Advertising

  2. JB wrote:
    > I have created a small program that generates a project tree from a
    > dictionary. The dictionary is of key/value pairs where each key is a
    > directory, and each value is a list. The list have unique values
    > corresponding to the key, which is a directory where each value in the
    > list becomes a subdirectory.
    >
    > The question that I have is how to do this process if one of the
    > unique values in the list is itself a dict. For example, in the
    > "projdir" dict below, suppose the "Analysis" value in the list
    > corresponding to the "Engineering" key was itself a dict and was
    > assigned {'Analysis' : 'Simulink'} for example.


    You might want to read up on recursion, i.e. a function calling itself.

    You can find out if something is a dict like this:

    isinstance(x, dict)

    or, if you know it really is a dict and not a subtype:

    type(x) is dict

    Stefan
     
    Stefan Behnel, Sep 11, 2009
    #2
    1. Advertising

  3. JB

    JB Guest

    On Sep 11, 9:42 am, Stefan Behnel <> wrote:
    > JBwrote:
    > > I have created a small program that generates a project tree from a
    > > dictionary. The dictionary is of key/value pairs where each key is a
    > > directory, and each value is a list. The list have unique values
    > > corresponding to the key, which is a directory where each value in the
    > > list becomes a subdirectory.

    >
    > > The question that I have is how to do this process if one of the
    > > unique values in the list is itself a dict. For example, in the
    > > "projdir" dict below, suppose the "Analysis" value in the list
    > > corresponding to the "Engineering" key was itself a dict and was
    > > assigned {'Analysis' : 'Simulink'} for example.

    >
    > You might want to read up on recursion, i.e. a function calling itself.
    >
    > You can find out if something is a dict like this:
    >
    >         isinstance(x, dict)
    >
    > or, if you know it really is a dict and not a subtype:
    >
    >         type(x) is dict
    >
    > Stefan


    Stefan;

    Thanks for your valuable suggestion. I have employed your suggestion
    with the code below.
    I also converted this to object oriented design. With this type of
    construction, the
    program will easily handle the dictionary specified by the self.proj
    variable. This
    variable is a dictionary with some of the value variables also being
    dictionaries.
    I did not generalize the function call but have more of horizontal
    code for this
    area of the implementation. If this program were packaged with a GUI,
    it would
    make the beginning of a project management software platform.

    -James



    from time import strftime
    import os

    #-------------------------------------
    # Print/Make Directories
    #-------------------------------------
    class Tree:
    def __init__(self,dir_main,dir_sub,sch_names,drawing_name):
    self.proj = {'Project' : ['Schedule',
    'Specifications'],

    'Schematics_PCB' : [{'SCH':sch_names},{'BOM':sch_names},
    {'ASSY':sch_names},
    {'RELEASE':sch_names}],

    'Drawings' : [{'System':drawing_name},
    {'Board':sch_names},
    'Packaging_3D'],

    'Engineering' : [{'Analysis':
    ['Simulink','Matlab','Excel',
    'MathCad','Python']},
    {'Eng Reports':sch_names}, {'Design
    Reviews':sch_names},
    {'Procedures':['Board Test','System
    Test']}]}
    self.sch_names = sch_names
    self.drawing_name = drawing_name
    self.dir_main = dir_main
    self.dir_sub = dir_sub
    self.root = self.dir_main + self.dir_sub
    self.pathname = ''
    self.suffix = "\\Previous \n"
    self.recursion = 0.0
    self.zs = ''

    def gen_dir(self):
    if os.path.exists(self.pathname):
    pass
    else:
    os.makedirs(self.pathname)
    return

    def print_path(self):
    print self.pathname
    Tree.gen_dir(self)

    self.zs = self.pathname + self.suffix

    Tree.gen_dir(self)
    print self.zs
    #-------------------------------------
    # Make a Directory Project Tree
    #-------------------------------------
    def make_tree(self):
    counter = 0
    print "This project tree for " + self.dir_sub[0:-1] + " was completed
    by "
    print "Generated on " + strftime("%m/%d/%Y at %H.%M hours")
    print "The project tree / dictionary was found to have the following:
    \n"

    #-------------------------------------------------------------------------
    # Iterate over items in the dictionary, creating tuples of key/value
    pairs

    #-------------------------------------------------------------------------
    for key, values in self.proj.iteritems():
    counter = counter + 1
    print "Key #" + str(counter) + " is " + "'" + key + "\\"
    print "For this key, the values are " + str(values)
    print "Thus, the results of generating directories for this "
    print " key/values combo are: \n"
    #---------------------------------------------------------
    # Iterate over the invidividual unique values in the list
    # that is associated with each key in the dict, where
    # an individual value may itself be a dict
    #---------------------------------------------------------
    for unique in values:
    prefix = self.root + key + "/"
    if isinstance(unique, dict) == True:
    for kx,vx in unique.iteritems():
    if isinstance(vx,list) == True:
    for items in vx:
    self.pathname = prefix + kx + "\\" + items
    Tree.print_path(self)
    else:
    self.pathname = prefix + kx + "\\" + vx
    Tree.print_path(self)
    else:
    self.pathname = prefix + unique
    Tree.print_path(self)



    if __name__== "__main__":
    #-------------------------------------
    # DEFINE Root Dir & Path, Sch Numbers
    #-------------------------------------
    sch_names = ['AD501','AD502','AD503']
    drawing_name = 'AD601'
    dir_main = '\\\MAIN\\PUBLIC\\ABC\\TEST\\'
    dir_sub = "MDX\\"
    rootdir = dir_main + dir_sub
    #-------------------------------------
    # DEFINE Directory Tree for Project
    #-------------------------------------


    t = Tree(dir_main,dir_sub,sch_names,drawing_name)

    t.make_tree()
     
    JB, Sep 14, 2009
    #3
    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. Replies:
    4
    Views:
    471
    Steven Bethard
    Feb 17, 2005
  2. Replies:
    5
    Views:
    717
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Nov 11, 2005
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    435
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. wswilson
    Replies:
    4
    Views:
    338
    Maric Michaud
    Apr 19, 2007
  5. carl
    Replies:
    5
    Views:
    2,461
    James Kanze
    Nov 25, 2009
Loading...

Share This Page