2013-11-23 42 views
0

我的經驗是與HTML/CSS/PHP,但我最近開始使用Python。我發現自己陷入了困境,希望有人能夠發現我做錯了什麼。我正在處理的程序在從xml文檔中檢索一些變量時停滯不前。我已經把範圍縮小到本節:Python3 ElementTree循環在解析時停頓。跟蹤顯示subprocess.CalledProcessError

(彌補了變量)

def add_ingredients_for_selected_recipes(self, root): 
     recipes = ["Beef Stew", "Tuna Casserole", "Spaghetti", "Chocolate Cake"] 
     guest = self.settings.get('guest') 
     allergies = {'nuts': ["guest1", "guest2", "guest3"], 'seafood': ["guest5", "guest6"]} 

     for recipe in recipes: 
      # Add necessary ingredients for user desired recipes to our list 
      if self.settings.get("recipe_" + recipe): 
       self.queue_event('info', 'Selecting ingredients for "%s" recipe.' % recipe) 
       for child in root.iter(recipe): 
        for ing in child.iter('ingredient'): 
         contains = ing.attrib.get('allergy') 
         if contains is None: 
          self.queue_event('debug', 'Adding ingredient:%s' % (ing.text)) 
          self.ingredients.append(ing.text) 
         elif contains in allergies and guest in allergies[contains]: 
          self.queue_event('debug', 'Adding ingredient:%s contains %s' % (ing.text, contains)) 
          self.ingredients.append(ing.text) 
         else: 
          self.queue_event('debug', 'Skipping ingredient:%s contains %s' % (ing.text, contains)) 

以下是我從跟蹤grep'd:https://dl.dropboxusercontent.com/u/60521097/fromtrace 還有一兩件事是相關的是,如果我刪除的部分檢查它工作正常的屬性。所以我知道問題在哪裏,但我沒有看到它。在此先感謝您的任何建議。

+0

堆棧跟蹤如何連接到您的腳本? – User

+0

我改變了上面例子中的變量,但棧跟蹤沒有改變。我這樣做,所以它不會被任何搜索引擎機器人連接到我的項目。 – lots0logs

+0

您可能希望將其分解爲更小的函數/方法,以便更易於調試。每個有'for'或'if/else'的地方都適合分居。這也將有助於可讀性。 –

回答

1

好吧,我想出了問題。我之前定義了哪些「食譜」可作爲每個「賓客」的選項。當主循環(食譜中的食譜:)得到的食譜沒有被定義爲「過敏」中的「過敏」的可用選項時,它顯示假和循環無法繼續。我解決了這個定義的「食譜」變量基礎上選定的「客人」是這樣的:

self.recipes_by_guest = {"guest1": ["Beef Stew", "Tuna Casserole", "Spaghetti"], 
         "guest2": ["Beef Stew", "Spaghetti"],...etc} 
guest = self.settings.get("guest") 
recipes = self.recipes_by_guest[guest] 
allergies = {'nuts': ["guest1", "guest2", "guest3"], 'seafood': ["guest5", "guest6"]} 

for recipe in recipes: etc... 

謝謝大家誰看了一下,並評論建議!

+0

感謝您找到解決方案:) – User