2016-07-01 10 views
0

Ich versuche Print-Kategorie-Baum (Hauptkategorien mit Unterkategorien), aber ich habe ein Problem mit eleganten Lösung, die bis unendlich Tiefe bereit sein wird. IchLoop-Anweisung bereit für Multilevel-Tiefe

In diesem Moment haben:

cat_id = '31' 
cat = prestashop.get('categories', options={'display':'full', 'filter[id]':'[' + cat_id + ']'})['categories']['category']['associations']['categories']['category'] 

''' 
cat returns dictionary: 
cat [ 
    {'id': '32'}, 
    {'id': '37'}, 
    {'id': '48'}, 
    {'id': '52'}, 
    {'id': '54'}, 
    {'id': '57'}, 
    {'id': '58'}, 
    {'id': '59'}, 
    {'id': '1068'} 
] 
''' 

for c in cat: 
    print 'Level 2: ' + c['id'] 
    try: 
     cat = prestashop.get('categories', options={'display':'full', 'filter[id]':'[' + c['id'] + ']'})['categories']['category']['associations']['categories']['category'] 
     for c in cat: 
      print 'Level 3: ' + c['id'] 
      try: 
       cat = prestashop.get('categories', options={'display':'full', 'filter[id]':'[' + c['id'] + ']'})['categories']['category']['associations']['categories']['category'] 
       for c in cat: 
        print 'Level 4: ' + c['id'] 
        try: 
         cat = prestashop.get('categories', options={'display':'full', 'filter[id]':'[' + c['id'] + ']'})['categories']['category']['associations']['categories']['category'] 
         for c in cat: 
          print 'Level 5: ' + c['id'] 
        except: 
         pass 
      except: 
       pass 
    except: 
     pass 

Wie Sie sehen, Wirksamkeit meines Codes hängt von Ebenen, die ich manuell eingeben werden. Es ist eine schlechte Lösung, aber nur eine, die ich in diesem Moment kann und arbeite.

Wie kann ich diesen Code so einstellen, dass er für Unendlich-Level (Level 5,6,7,8,9,10,11,12 ...) bereit ist und wartungsfreier ist?

Ergebnis:

Level 2: 32 
    Level 3: 33 
    Level 3: 1178 
Level 2: 37 
    Level 3: 1201 
    Level 3: 38 
    Level 3: 39 
    Level 3: 40 
     Level 4: 1020 
     Level 4: 41 
     Level 4: 42 
    Level 3: 43 
     Level 4: 1092 
     Level 4: 1093 
     Level 4: 1094 
     Level 4: 1095 
    Level 3: 1024 
     Level 4: 44 
     Level 4: 45 
     Level 4: 1179 
Level 2: 48 
    Level 3: 49 
    Level 3: 50 
    Level 3: 51 
    Level 3: 1128 
    Level 3: 1067 
    Level 3: 1133 
Level 2: 52 
    Level 3: 53 
    Level 3: 1200 
Level 2: 54 
    Level 3: 55 
    Level 3: 56 
    Level 3: 1202 
Level 2: 57 
Level 2: 58 
Level 2: 59 
    Level 3: 60 
    Level 3: 61 
Level 2: 1068 

Antwort

0

Ich bin nicht vertraut mit Prestashop aber haben Sie versucht, eine rekursive Funktion?

def print_levels(cat, level = 2): 
    try: 
     for c in cat: 
      print((" " * level) + "Level {0}: {1}\n".format(level,c['id'])) 
      new_cat = prestashop.get('categories', options={'display':'full', 'filter[id]':'[' + c['id'] + ']'})['categories']['category']['associations']['categories']['category'] 
      if new_cat: 
       print_levels(new_cat, level + 1) 
      else: 
       return 
    except: 
     return 
cat_id = '31' 
cat = prestashop.get('categories', options={'display':'full', 'filter[id]':'[' + cat_id + ']'})['categories']['category']['associations']['categories']['category'] 
print_levels(cat) 
+0

Leider funktioniert es nicht richtig. Es gibt 'Stufe 2: 32 Stufe 3: 33 Stufe 2: 37 Stufe 3: 1201 Stufe 2: 48 Stufe 3: 49 Stufe 2: 52 Stufe 3: 53 Stufe 2: 54 Stufe 3 : 55 Level 2: 57'. Stellen Sie es sich nicht als Presta vor, sondern nur als multidimensionales Array. – user3041764