Hi All,

I am using the following code to populate a tree control. The dictionary currently I am using is ‘dictFolders’ and its structure is defined below.

lstFolders=[]
lstFolders = ["A","B","C","D"]
dictFolders={}
dictFolders = {"A":["Obj1","Obj2","Obj3","Obj4","Obj5","Obj6","Obj7","Obj8","Obj9"],
"B":["Doc1","Doc2","Doc3","Doc4","Doc5","Doc6","Doc7","Doc8","Doc9"],
"C":["Link1","Link2","Link3","Link4","Link5","Link6"],
"D":["Obj1","Obj2","Obj3","Obj4","Obj5","Obj6","Obj7"]}


# Setting the root node text to the Action ID entered
self.treeAction.SetItemText(self.treeAction.GetRootItem(),”Parent Folder Name”)


# Loop to Populate the tree containing Action Objects
print "length of dictFolders=", len(dictFolders)
for intIndex in range (0,len(dictFolders)):


#If tree control does not have any children then append Items
if self.treeAction.ItemHasChildren(nodeFolders[intIndex])==False:


for intiLoopIndex in range (0,len(dictFolders[lstFolders[intIndex]])):
self.treeAction.AppendItem(nodeFolders[intIndex],(dictFolders[lstFolders[intIndex]][intiLoopIndex]))
self.treeAction.ExpandAllChildren(nodeFolders[intIndex])


# Collapsing all children after populating
for intIndex in range(0,len(dictFolders.keys())):
self.treeAction.CollapseAllChildren(nodeFolders[intIndex])

Now I want to implement the same with a new dictionary whose structure is given below:

dictFoldersNew = {"A":{"XYZ": [["Name1", "UID1"], ["Name2", "UID2"]],
"PLM": [["Number1", "UUID1"], ["Number2", "UID2"]],
"RST": [["ID1", "UUID1"], ["ID2", "UID2"]]},
"B": [["IDB1", "UID1"], ["IDB2", "UID2"]],
"C": [["IDC1", "UID1"], ["IDC2", "UID2"]],
"D": [["IDD1", "UID1"], ["IDD2", "UID2"]]}

I want a second level of folders under A and the rest of the structure should remain the same. Under XYZ, PLM and RST I want to display UID1,Number1 and ID1 respectively. And under B,C and D, I want to display (IDB1,IDB2),( IDC1,IDC2). IDD1,IDD2) respectively.
How can I do this?... Presently I am unable to understand the new dictionary format.
Any help is much appreciated.

Regards,
Dinil

Recommended Answers

All 5 Replies

Sorry that I forgot to put tags to the code

lstFolders=[]
lstFolders = ["A","B","C","D"]
dictFolders={}
dictFolders = {"A":["Obj1","Obj2","Obj3","Obj4","Obj5","Obj6","Obj7","Obj8","Obj9"],
"B":["Doc1","Doc2","Doc3","Doc4","Doc5","Doc6","Doc7","Doc8","Doc9"],
"C":["Link1","Link2","Link3","Link4","Link5","Link6"],
"D":["Obj1","Obj2","Obj3","Obj4","Obj5","Obj6","Obj7"]}

# Setting the root node text to the Action ID entered
self.treeAction.SetItemText(self.treeAction.GetRootItem(),”Parent Folder Name”)

# Loop to Populate the tree containing Action Objects
print "length of dictFolders=", len(dictFolders)
for intIndex in range (0,len(dictFolders)):

#If tree control does not have any children then append Items
if self.treeAction.ItemHasChildren(nodeFolders[intIndex])==False:

for intiLoopIndex in range (0,len(dictFolders[lstFolders[intIndex]])):
self.treeAction.AppendItem(nodeFolders[intIndex],(dictFolders[lstFolders[intIndex]][intiLoopIndex]))
self.treeAction.ExpandAllChildren(nodeFolders[intIndex])

# Collapsing all children after populating
for intIndex in range(0,len(dictFolders.keys())):
self.treeAction.CollapseAllChildren(nodeFolders[intIndex])
Now I want to implement the same with a new dictionary whose structure is given below:
dictFoldersNew = {"A":{"XYZ": [["Name1", "UID1"], ["Name2", "UID2"]],
"PLM": [["Number1", "UUID1"], ["Number2", "UID2"]],
"RST": [["ID1", "UUID1"], ["ID2", "UID2"]]},
"B": [["IDB1", "UID1"], ["IDB2", "UID2"]],
"C": [["IDC1", "UID1"], ["IDC2", "UID2"]],
"D": [["IDD1", "UID1"], ["IDD2", "UID2"]]}

can you put your code in tags? I have failed to reconstruct exactly what you wrote and I gave up. Until I see your code, I will try figure out!

Thanks
Steve

The easiest way to understand it is to break each part down and give it a separate variable name.

"""      
dictFoldersNew = {"A":{"XYZ": [["Name1", "UID1"], ["Name2", "UID2"]],
"PLM": [["Number1", "UUID1"], ["Number2", "UID2"]],
"RST": [["ID1", "UUID1"], ["ID2", "UID2"]]},
"B": [["IDB1", "UID1"], ["IDB2", "UID2"]],
"C": [["IDC1", "UID1"], ["IDC2", "UID2"]],
"D": [["IDD1", "UID1"], ["IDD2", "UID2"]]}
"""      

top_level = {}
top_level["A"] = {}
top_level["B"] = []
top_level["C"] = []

##---  populate "A"
level_2_dic = top_level["A"]
level_2_dic["XYZ"] = []
level_2_dic["PLM"] = []

this_list = level_2_dic["XYZ"]
this_list.append(["Name1", "UIX1"])
this_list.append(["Name2", "UIY2"])

this_list = level_2_dic["PLM"]
this_list.append(["Number1", "UUID1"])
this_list.append(["Number2", "UUID2"])

##---  populate "B"
this_list = top_level["B"]
this_list.append(["IDB1", "UID1"])
this_list.append(["IDB2", "UID2"])

##---  populate "C"
this_list = top_level["C"]
this_list.append(["IDC1", "UIE1"])
this_list.append(["IDC2", "UIE2"])

##--- print all data
for key in top_level:
   print key
   if type(top_level[key]) is dict:
      level_2 = top_level[key]
      for sub_key in level_2:
         print "     dictionary", sub_key
         this_list = level_2[sub_key]
         for each_list in this_list:
            print "          list", sub_list
   elif type(top_level[key]) is list:
      print "     list"
      this_list in top_level[key]:
      for each_list in this_list:
         print "          list", sub_list
   print
   
##  you can also do this
print top_level["A"]["XYZ"][0]   ## first list in "XYZ" in "A"

You want to get away from the word processing thought pattern, where you have lines in documents in folders. None of that really exists. With programming you have one continuous stream of bytes, either in memory or on disk. Then you have pointers to the memory/disk location. So, you want to access the pointer to "A" and access the bytes at that location which gives the pointer to "XYZ", or on disk you want to access the pointer to the directory ('directs' you to a certain location), and then the pointer to a certain file, etc. The problem is viewing this data as discrete "lines in a document in a folder" instead of as a continuous stream of bytes which you have to subdivide and organize.

Hi All... Thanks for helping me out. Actually broke the new dictionary format and made the structure same as the old one. This is working perfectly fine for me.
Thanks again....

#Modified List to hold Folder names 
    lstFoldersNew=[]

    lstFoldersNew = ["AffectedObjects","Attachments","Links","AssociatedObjects"]

    #Temporary dictionary to hold the new dictionary in the old format
    tempDict={}

    #Temporary list to copy the new dictionary to tempDict
    listTemp=[]

    #Copy only the 'AffectedObjects' into a temp dictionary

    #Temporary dictionary to hold the new dictionary for Affected Objects in the old format
    tempDictForAffectedObjects={}

    #Temporary dictionary to hold the new dictionary for Affected Objects in the old format
    tempDictForAffectedObjectsModified={}

    #Temporary list only for Affected Objects to copy the new dictionary to tempDict
    listTempForAffectedObjects=[]

    #Temporary list only for Affected Objects to append images
    lstFoldersForAffectedObjects=[]

    try:
        print "objClsVars.dictFolders",objExtVars.objClsVars.dictFolders

        #Copy the new dictionary to temp dictionary
        tempDictForAffectedObjects=objExtVars.objClsVars.dictFolders["AffectedObjects"]

        print "Printing Affected Objects===",tempDictForAffectedObjects
        print ""
        print "tempDictForAffectedObjects.keys",tempDictForAffectedObjects.keys()

        #Store the keys in a list
        lstFoldersForAffectedObjects=tempDictForAffectedObjects.keys()
        print ""
        print "printing affected objects keys===",lstFoldersForAffectedObjects
        print ""

        #Loop used for creating the temp dictionary from the new dictionary
        for intIndex in range (0,len(tempDictForAffectedObjects)):
            listTempForAffectedObjects=tempDictForAffectedObjects[lstFoldersForAffectedObjects[intIndex]][0]
            listTempForAffectedObjects.extend(tempDictForAffectedObjects[lstFoldersForAffectedObjects[intIndex]][1])
            print "listTempForAffectedObjects=====",listTempForAffectedObjects
            tempDictForAffectedObjectsModified[lstFoldersForAffectedObjects[intIndex]]=listTempForAffectedObjects
        print "tempDictForAffectedObjectsModified======",tempDictForAffectedObjectsModified

        nodeFoldersAffectedObj=[]

        #Loop to create folders under the first child in the tree control
        for key in lstFoldersForAffectedObjects:
            nodeFoldersAffectedObj.append(self.treeAction.AppendItem(nodeFolders[0],key))

        #Tree Population starts here

        print ""

        #Loop to populate the first child under the tree control
        for intIndex in range (0,len(tempDictForAffectedObjectsModified)):

            #If tree control does not have any children then append Items 
            if self.treeAction.ItemHasChildren(nodeFolders[intIndex+1])==False:  
                intCount=0   

                for intiLoopIndex in range (0,len(tempDictForAffectedObjectsModified[lstFoldersForAffectedObjects[intIndex]])):
                    #print "intIndex=====",intIndex
                    #print "intiLoopIndex=======",intiLoopIndex
                    #print "intCount=====",intCount
                    #print "length of tempDict[lstFoldersNew[intIndex]][intiLoopIndex]=====",len(tempDictForAffectedObjectsModified[lstFoldersForAffectedObjects[intIndex]])

                    #Populate only Instance names, Id's, DocId's etc
                    if intCount < len(tempDictForAffectedObjectsModified[lstFoldersForAffectedObjects[intIndex]]):
                        print "tempDict[lstFoldersNew[intIndex]][intCount])====",tempDictForAffectedObjectsModified[lstFoldersForAffectedObjects[intIndex]][intCount]
                        self.treeAction.AppendItem(nodeFoldersAffectedObj[intIndex],(tempDictForAffectedObjectsModified[lstFoldersForAffectedObjects[intIndex]][intCount]))
                    intCount=intCount+2

        #Tree Population ends

        #End of copying

        #Append images to the folders created under the first child
        self.AppendImages(nodeFoldersAffectedObj,self.treeAction)

        #loop to create the temp dictionary for the last three keys
        for intIndex in range (1,len(objExtVars.objClsVars.dictFolders)):

            listTemp=objExtVars.objClsVars.dictFolders[lstFoldersNew[intIndex]][0]
            listTemp.extend(objExtVars.objClsVars.dictFolders[lstFoldersNew[intIndex]][1])
            #print "listTemp===",listTemp 
            tempDict[lstFoldersNew[intIndex]]= listTemp

        print ""
        print "Copied Dictionary=====",tempDict
        print ""
        #print"tempDict length====", len(tempDict)

        #Modify the list
        lstFoldersNew = ["Attachments","Links","AssociatedObjects"]
        #print "lstFoldersNew=======",lstFoldersNew

        #Loop to populate the folders under the tree control
        for intIndex in range (0,len(tempDict)):

            #If tree control does not have any children then append Items 
            if self.treeAction.ItemHasChildren(nodeFolders[intIndex+1])==False:  
                intCount=0   

                for intiLoopIndex in range (0,len(tempDict[lstFoldersNew[intIndex]])):
                    #print "intIndex=====",intIndex
                    #print "intiLoopIndex=======",intiLoopIndex
                    #print "intCount=====",intCount
                    #print "length of tempDict[lstFoldersNew[intIndex]][intiLoopIndex]=====",len(tempDict[lstFoldersNew[intIndex]])

                    #Populate only Instance names, Id's, DocId's etc
                    if intCount < len(tempDict[lstFoldersNew[intIndex]]):
                        #print "tempDict[lstFoldersNew[intIndex]][intCount])====",tempDict[lstFoldersNew[intIndex]][intCount]
                        self.treeAction.AppendItem(nodeFolders[intIndex+1],(tempDict[lstFoldersNew[intIndex]][intCount]))
                        intCount=intCount+2
                    self.treeAction.ExpandAllChildren(nodeFolders[intIndex])

        # Collapsing all children after populating
        for intIndex in range(0,len(tempDict.keys())):
            self.treeAction.CollapseAllChildren(nodeFolders[intIndex])

        # Setting the root node text to the Action ID entered
        self.treeAction.SetItemText(self.treeAction.GetRootItem(),self.txtActionId.Label)

        # Setting statusbar value
        self.statBar.Label='Click on Next to continue...'

        # Freezing the Action ID text box    
        self.txtActionId.Enable(False)

        # Enabling the 'Next' button
        self.btnNext.Enable(True)

        # Freezing the 'Proceed' Button
        self.btnProceed.Enable(False)

        # Set focus on 'Next' button
        self.btnNext.SetFocus() 

    except:

        lstFoldersNew=[]
        tempDict={}
        listTemp=[]
        tempDictForAffectedObjects={}
        tempDictForAffectedObjectsModified={}
        listTempForAffectedObjects=[]
        lstFoldersForAffectedObjects=[]

Check out the attachment also to see the new tree structure....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.