From c32117a7b5d061ce7ed6ca92cd4a53a34eda08ed Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 13 Aug 2015 11:21:53 -0400 Subject: [PATCH] add datum variable to remove redundant code renames datum to dName also fix date parsing (was using book, not datum) --- test.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/test.py b/test.py index 23671c2..b2d1d81 100644 --- a/test.py +++ b/test.py @@ -33,37 +33,38 @@ def updateData(): for book in collection.iter(ns + "entry"): bookout = {} - for datum in fields.keys(): - if book.find(ns + datum) != None: - t = int(fields[datum]["type"]) + for dName in fields.keys(): + datum = book.find(ns + dName) + if datum != None: + t = int(fields[dName]["type"]) #simple text, paragraph, multiple choice, image if t == 1 or t == 2 or t == 3 or t == 10: #check if multiple entries are allowed #if so, put entries in a list - if (int(fields[datum]["flags"]) & 0x01 & 0xff) != 0: - bookout[datum] = [] - for sub in book.find(ns + datum): - bookout[datum].append(sub.text) + if (int(fields[dName]["flags"]) & 0x01 & 0xff) != 0: + bookout[dName] = [] + for sub in datum: + bookout[dName].append(sub.text) else: - bookout[datum] = book.find(ns + datum).text + bookout[dName] = datum.text #date elif t == 12: - bookout[datum] = "" - if book.find(ns + "year") != None: - bookout[datum] += book.find(ns + "year").text - if book.find(ns + "month") != None: - bookout[datum] += "-" + book.find(ns + "month").text - if book.find(ns + "day") != None: - bookout[datum] += "-" + book.find(ns + "day").text + bookout[dName] = "" + if datum.find(ns + "year") != None: + bookout[dName] += datum.find(ns + "year").text + if datum.find(ns + "month") != None: + bookout[dName] += "-" + datum.find(ns + "month").text + if datum.find(ns + "day") != None: + bookout[dName] += "-" + datum.find(ns + "day").text elif t == 6: #Number - bookout[datum] = book.find(ns + datum).text + bookout[dName] = datum.text else: - print(str(t) + ": " + datum) - bookout[datum] = "" + print("Did not parse:" + dName + ", type:" + str(t)) + bookout[dName] = "" else: - bookout[datum] = "" + bookout[dName] = "" out.append(bookout.copy()) data = out.copy() return "success"