Python лучший способ обработки иерархических данных, отличных от вложенных, если и для циклов - PullRequest
0 голосов
/ 03 декабря 2018

Попытка записать иерархические данные, полученные через API, и записать их в файл CSV, и в результате получилось глубоко вложенное решение цикла if-for.

Данные организованы следующим образом:

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

Не гарантируется, что у каждого элемента есть дочерние элементы.Текущий код выглядит следующим образом:

      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)

Предположение, что должно быть более элегантное решение.

Вот типичный пример вывода

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