From e800c321b8b438a75955ab221a140fc58fedf6f4 Mon Sep 17 00:00:00 2001 From: Brianna Birman Date: Tue, 3 Jul 2012 17:55:30 -0700 Subject: [PATCH] separated out display class; sorted years; added user options for display --- display.py | 121 +++++++++++++++++++++++++++++ main.py | 221 ++++++++++++++++++++++------------------------------- 2 files changed, 212 insertions(+), 130 deletions(-) create mode 100644 display.py diff --git a/display.py b/display.py new file mode 100644 index 0000000..0ec15a7 --- /dev/null +++ b/display.py @@ -0,0 +1,121 @@ +from tkinter import * +from tkinter import ttk + +class Display: + def __init__(self, data, names, years): + self.root = Tk() + self.root.title("Tick-Talk") + + self.optionsframe = ttk.Frame(self.root, width = 1200, height = 100) + self.optionsframe.pack(fill=X) + + self.canvas = Canvas(self.root, width=1200, height=1000) + self.canvas.pack() + + self.drawList = Listbox(self.optionsframe, height=5, selectmode=SINGLE, exportselection=0) + self.yearList = Listbox(self.optionsframe, height=5, selectmode=SINGLE, exportselection=0) + self.timeList = Listbox(self.optionsframe, height=5, selectmode=SINGLE, exportselection=0) + self.label = ttk.Label(self.optionsframe, text=" ") + + self.data = data + self.names = names + self.years = years + + self.drawList.insert(END, "Line count") + self.drawList.insert(END, "Percentage") + + self.timeList.insert(END, "By year") + self.timeList.insert(END, "Total") + + for year in self.years: + self.yearList.insert(END, year) + + self.label.pack(side = RIGHT) + self.drawList.pack(side=LEFT, padx=30) + self.timeList.pack(side=LEFT, padx=30) + + def drawValue(self, drawType, year, name, week): + if drawType == "rawLines": + return .25*self.data[year][name][week] + elif drawType == "percentages": + pass + #if weekTotals[year][week] != 0: + # return (self.data[year][name][week]/weekTotals[year][week]*500) + #return (peopleCounts[name][week]/weekTotals[week]*500) + #else: + + # return 0 + + def nameLabel(self, currentTag): + if len(currentTag) > 0: + toTruncate = currentTag.index("current") + #toTruncate = currentTag.index(" ") + name = currentTag[0:toTruncate] + return name + else: + return "" + + def drawYear(self, year): + YOFFSET = 600 + xcoord = 1080 + bottomHeight = [YOFFSET]*108 + i = 0 + while i < 108: + if i%2 == 0: + bottomHeight[i] = xcoord + xcoord = xcoord - 20 + i = i + 1 + + num = 0 + for name in self.names: + topHeight = [0]*54; + + xcoord = 1080 + j = 53 + while j >= 0: + topHeight[j] = xcoord + topHeight.insert(j+1, self.drawValue("rawLines", year, name, j)) + xcoord = xcoord - 20 + j = j - 1 + + for h in range(108): + if h%2 == 1: + topHeight[h] = bottomHeight[108 - h] - topHeight[h] + + topHeight.append(topHeight[106]) + topHeight.append(bottomHeight[107]) + plotThis = topHeight + bottomHeight + + + color = "lightblue" + if num%10 == 8: + color = "green" + elif num%10 == 7: + color = "hot pink" + elif num%10 == 6: + color = "yellow" + elif num%10 == 5: + color = "gray" + elif num%10 == 4: + color = "pink" + elif num%10 == 3: + color = "purple" + elif num%10 == 2: + color = "orange" + elif num%10 == 1: + color = "blue" + elif num%10 == 0: + color = "red" + + self.canvas.create_polygon(plotThis,fill=color,outline="brown",width=2, smooth="true", tags=name) + num = num + 1 + + k = 0 + while k < 108: + if k%2 == 1: + bottomHeight[k] = topHeight[108 - k] + k = k+1 + + #canvas.bind('', lambda e: label.configure(text = canvas.gettags(CURRENT))) + self.canvas.bind('', lambda e: self.label.configure(text = self.nameLabel(self.canvas.gettags(CURRENT)))) + #canvas.bind('', lambda e: canvas.itemconfig(canvas.find_withtag(canvas.gettags(CURRENT)), fill="blue")) \ No newline at end of file diff --git a/main.py b/main.py index 833dbf8..4f951eb 100644 --- a/main.py +++ b/main.py @@ -12,18 +12,15 @@ import pickle from tkinter import * from tkinter import ttk +from display2 import Display -peopleCounts = {} +data = {} names = [] -weekTotals = [0]*54 +years = [] +weekTotals = {} drawType = "rawLines" -def nameLabel(currentTag): - toTruncate = currentTag.index("current") - name = currentTag[0:toTruncate] - return name - def countLines(email): count = email.count("") + email.count("<=\r\ncli:body>") + email.count("") + email.count("") + email.count("") + email.count("") + email.count("") + email.count("") + email.count("") + email.count("") return count @@ -78,10 +75,14 @@ def parseDate(dateString): return dateTuple def mergeNames(keepName, mergeNames): + for mergeName in mergeNames: - for i in range(54): - peopleCounts[keepName][i] += peopleCounts[mergeName][i] - del peopleCounts[mergeName] + for year in years: + for i in range(54): + data[year][keepName][i] += data[year][mergeName][i] + # peopleCounts[keepName][i] += peopleCounts[mergeName][i] + del data[year][mergeName] + #del peopleCounts[mergeName] names.remove(mergeName) @@ -89,129 +90,89 @@ def mergeTheseNames(): mergeName("Example name", ["Example name duplicate"]) -def drawValue(name, week): - if drawType == "rawLines": - return .25*peopleCounts[name][week] - elif drawType == "percentages": - if weekTotals[week] != 0: - return (peopleCounts[name][week]/weekTotals[week]*500) - else: - return 0 - -def main(): - FILE = open("post3-2.txt", "rb") - raw_emails = pickle.load(FILE) - FILE.close() - + +def areaHovered(canvas, label): + label.configure(text = nameLabel(canvas.gettags(CURRENT))) + #label.configure(text = canvas.gettags(CURRENT)) + #area = canvas.find_withtag(canvas.gettags(CURRENT)) + #canvas.itemconfig(canvas.find_withtag(canvas.gettags(CURRENT)), fill="blue") + - #weekTotals = [0]*54 - for raw_email in raw_emails: - email_message = email.message_from_bytes(raw_email) - name = email.utils.parseaddr(email_message['From'])[0] #0 for name, 1 for email address - if names.count(name) == 0: - names.append(name) - #print(name) +def chooseYear(event): + display.canvas.delete("all") + item = display.yearList.get(ACTIVE) + print("Item: " + str(item)) + if item == 2009: + display.drawYear(2009) + elif item == 2010: + display.drawYear(2010) + elif item == 2011: + display.drawYear(2011) + elif item == 2012: + display.drawYear(2012) - #set up dictionary with each name correlating to a list of the counts of each week (over a year) - #peopleCounts = {} - for name in names: - yearList = [0]*54 - peopleCounts[name] = yearList - - for raw_email in raw_emails: - email_message = email.message_from_bytes(raw_email) - name = email.utils.parseaddr(email_message['From'])[0] #0 for name, 1 for email address - dateTuple = parseDate(str(email_message['Date'])) - weekBucket = dateTuple[1] - lines = 0 - for index in range(len(email_message.get_payload())): - lines = countLines(str(email_message.get_payload()[index])) - peopleCounts[name][weekBucket] += lines - weekTotals[weekBucket] += lines - - mergeTheseNames() - - #for name in names: - # print("--" + name + "--") - # for week in peopleCounts[name]: - # print(week) - - root = Tk() - root.title("Tick-Talk") - YOFFSET = 800 - - mainframe = ttk.Frame(root) - mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) - label = ttk.Label(mainframe, text=" ") - label.grid(column=0, row=0) - - canvas = Canvas(root, width=1200, height=1000) - canvas.grid(column=0, row=1) +def typeSelected(event): + selection = display.timeList.get(ACTIVE) + print("Selection: " + selection) + if selection == "By year": + display.yearList.pack(side=LEFT, padx=30) + display.yearList.bind("", chooseYear) + elif selection == "Total": + display.yearList.pack_forget() + display.draw(display.) + +FILE = open("post3-2.txt", "rb") +raw_emails = pickle.load(FILE) +FILE.close() + +#weekTotals = [0]*54 +for raw_email in raw_emails: + email_message = email.message_from_bytes(raw_email) + name = email.utils.parseaddr(email_message['From'])[0] #0 for name, 1 for email address + year = parseDate(str(email_message['Date']))[0] + if names.count(name) == 0: + names.append(name) + #print(name) + if years.count(year) == 0: + years.append(year) + print("year: #" + str(year) + "#") + +for year in years: + data[year] = {} + for name in names: + data[year][name] = [0]*54 - xcoord = 1080 - bottomHeight = [YOFFSET]*108 - i = 0 - while i < 108: - if i%2 == 0: - bottomHeight[i] = xcoord - xcoord = xcoord - 20 - i = i + 1 - - num = 0 - for name in names: - topHeight = [0]*54; - - xcoord = 1080 - j = 53 - while j >= 0: - topHeight[j] = xcoord - topHeight.insert(j+1, drawValue(name, j)) - xcoord = xcoord - 20 - j = j - 1 - - for h in range(108): - if h%2 == 1: - topHeight[h] = bottomHeight[108 - h] - topHeight[h] - - topHeight.append(topHeight[106]) - topHeight.append(bottomHeight[107]) - plotThis = topHeight + bottomHeight + weekTotals[year] = [0]*54 + + +#set up dictionary with each name correlating to a list of the counts of each week (over a year) +#peopleCounts = {} +#for name in names: + #yearList = [0]*54 + #peopleCounts[name] = yearList + +for raw_email in raw_emails: + email_message = email.message_from_bytes(raw_email) + name = email.utils.parseaddr(email_message['From'])[0] #0 for name, 1 for email address + dateTuple = parseDate(str(email_message['Date'])) + weekBucket = dateTuple[1] + yearBucket = dateTuple[0] + lines = 0 + for index in range(len(email_message.get_payload())): + lines = countLines(str(email_message.get_payload()[index])) + #peopleCounts[name][weekBucket] += lines + #weekTotals[weekBucket] += lines + data[yearBucket][name][weekBucket] += lines + weekTotals[yearBucket][weekBucket] += lines + +mergeTheseNames() + +display = Display(data, names, years) +display.yearList.pack(side=LEFT, padx=30) +display.timeList.bind("", typeSelected) +display.drawYear(1970); - - color = "lightblue" - if num%10 == 8: - color = "green" - elif num%10 == 7: - color = "hot pink" - elif num%10 == 6: - color = "yellow" - elif num%10 == 5: - color = "gray" - elif num%10 == 4: - color = "pink" - elif num%10 == 3: - color = "purple" - elif num%10 == 2: - color = "orange" - elif num%10 == 1: - color = "blue" - elif num%10 == 0: - color = "red" - - canvas.create_polygon(plotThis,fill=color,outline="brown",width=2, smooth="true", tags=name) - num = num + 1 - - k = 0 - while k < 108: - if k%2 == 1: - - bottomHeight[k] = topHeight[108 - k] - k = k+1 - - canvas.bind('', lambda e: label.configure(text = nameLabel(canvas.gettags(CURRENT)))) - mainloop() - -if __name__ == '__main__': - main() +mainloop() +