我的經驗是與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 還有一兩件事是相關的是,如果我刪除的部分檢查它工作正常的屬性。所以我知道問題在哪裏,但我沒有看到它。在此先感謝您的任何建議。
堆棧跟蹤如何連接到您的腳本? – User
我改變了上面例子中的變量,但棧跟蹤沒有改變。我這樣做,所以它不會被任何搜索引擎機器人連接到我的項目。 – lots0logs
您可能希望將其分解爲更小的函數/方法,以便更易於調試。每個有'for'或'if/else'的地方都適合分居。這也將有助於可讀性。 –