Python Better Way to Process Hierarchal Data Other Than Nested if and for Loops

84 Views Asked by At

Trying to write hierachal data pulled through an API and write to a CSV file and ended up with a deeply nested if-for loop solution.

Data is organized like this:

    Program
    +--Project
       +--Feature
          +--Team Feature
             +--User Story

Not guaranteed that every element has children. Current code looks like:

      for pgm in response:
            if pgm.FormattedID[:3] == "PGM":
                outrow = ["PGM", pgm.FormattedID, "","",  "", "" ] + \
                    [returnAttrib(pgm, field, default="") for field in gFields]
                outfile.writerow(outrow)

                if hasattr(pgm, "Children"):
                    for prj in pgm.Children:
                        outrow = ["PRJ", pgm.FormattedID, prj.FormattedID, "", "", "" ] + \
                            [returnAttrib(prj, field, default="") for field in gFields]
                        outfile.writerow(outrow)

                        if hasattr(prj, "Children"):
                            for fea in prj.Children:
                                outrow = ["FEA", pgm.FormattedID, prj.FormattedID, fea.FormattedID, "", "" ] + \
                                    [returnAttrib(fea, field, default="") for field in gFields]
                                outfile.writerow(outrow)

                                if hasattr(fea, "Children"):
                                    for tf in fea.Children:
                                        outrow = ["TF", pgm.FormattedID, prj.FormattedID, fea.FormattedID, tf.FormattedID, "" ] + \
                                            [returnAttrib(tf, field, default="") for field in gFields]
                                        outfile.writerow(outrow)

                                        if hasattr(tf, "UserStories"):
                                            for us in tf.UserStories:
                                                outrow = ["US", pgm.FormattedID, prj.FormattedID, fea.FormattedID, tf.FormattedID,  us.FormattedID  ] + \
                                                    [returnAttrib(us, field, default="") for field in gFields]
                                                outfile.writerow(outrow)

Guessing there should be a more elegant solution.

Here's a representative example of the output

Type   PGM    PRJ     FEA.ID    TF.ID    US.ID      Title                       StoryPoints
PGM    PGM362                                       Remodel House
PRJ    PGM362 PRJ3735                               Living Areas Remodel    
FEA    PGM362 PRJ3735 FEA14867                      Kitchen Remodel 
TF     PGM362 PRJ3735 FEA15147  TF34748             Appliance Upgrades  
US     PGM362 PRJ3735 FEA19127  TF48721  US437377   Upgrade electrical system   13
US     PGM362 PRJ3735 FEA19127  TF48721  US437378   Oven replacements            3
US     PGM362 PRJ3735 FEA19127  TF48721  US437380   Refrigerator replacement     1
TF     PGM362 PRJ3735 FEA15147  TF34755     Cabinet & Countertop 
TF     PGM362 PRJ3735 FEA15147  TF34756     Flooring    
FEA    PGM362 PRJ3735 FEA14888                      Living Room Remodel 
TF     PGM362 PRJ3735 FEA14888  TF34812             Windows 
US     PGM362 PRJ3735 FEA14888  TF34812  US437448   Blind clean and repair       5
US     PGM362 PRJ3735 FEA14888  TF34812  US437454   Drape replacement            3
PRJ    PGM362 PRJ3779                               Bathrooms Remodel   
0

There are 0 best solutions below