From 38880352a545939c7ba0010eb52806910c243885 Mon Sep 17 00:00:00 2001 From: meshmote Date: Tue, 21 Oct 2014 21:05:28 -0700 Subject: [PATCH 01/21] started dict labs --- Students/RPerkins/session04/dict_setlab.py | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Students/RPerkins/session04/dict_setlab.py diff --git a/Students/RPerkins/session04/dict_setlab.py b/Students/RPerkins/session04/dict_setlab.py new file mode 100644 index 00000000..dcb1d0a9 --- /dev/null +++ b/Students/RPerkins/session04/dict_setlab.py @@ -0,0 +1,24 @@ +__author__ = 'Robert W. Perkins' + + +#d = {'name': 'Chris', 'city': 'Seattle', 'cake': 'Chocolate'} +#print d +#d.pop('cake') +#print d +#d['fruit'] = 'Mango' +#print d +#print d.keys() +#print d.values() +#print 'cake' in d +#print 'Mango' in d.values() + +int_list = [] +hex_list = [] +for i in range(16): + int_list.append(i) + hex_list.append(hex(i)) + +h_ex={} +for k, l in zip(int_list, hex_list): + h_ex[k] = l +print h_ex \ No newline at end of file From 66a38f4676bf7cbb3e774cf70fe91a8b7eaf1c09 Mon Sep 17 00:00:00 2001 From: meshmote Date: Wed, 22 Oct 2014 18:39:59 -0700 Subject: [PATCH 02/21] Added, debugged dict problem code --- Students/RPerkins/session04/dict_setlab.py | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Students/RPerkins/session04/dict_setlab.py b/Students/RPerkins/session04/dict_setlab.py index dcb1d0a9..855c350a 100644 --- a/Students/RPerkins/session04/dict_setlab.py +++ b/Students/RPerkins/session04/dict_setlab.py @@ -1,16 +1,16 @@ __author__ = 'Robert W. Perkins' -#d = {'name': 'Chris', 'city': 'Seattle', 'cake': 'Chocolate'} -#print d -#d.pop('cake') -#print d -#d['fruit'] = 'Mango' -#print d -#print d.keys() -#print d.values() -#print 'cake' in d -#print 'Mango' in d.values() +d = {'name': 'Chris', 'city': 'Seattle', 'cake': 'Chocolate'} +print d +d.pop('cake') +print d +d['fruit'] = 'Mango' +print d +print d.keys() +print d.values() +print 'cake' in d +print 'Mango' in d.values() int_list = [] hex_list = [] @@ -21,4 +21,9 @@ h_ex={} for k, l in zip(int_list, hex_list): h_ex[k] = l -print h_ex \ No newline at end of file +print h_ex + +d_prime = {} +for k, v in d.items(): + d_prime[k] = v.count('t') +print d_prime \ No newline at end of file From 0524242c911804260396d02349bfa06e70421808 Mon Sep 17 00:00:00 2001 From: meshmote Date: Wed, 22 Oct 2014 18:53:29 -0700 Subject: [PATCH 03/21] Added, debugged first set problem --- Students/RPerkins/session04/dict_setlab.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Students/RPerkins/session04/dict_setlab.py b/Students/RPerkins/session04/dict_setlab.py index 855c350a..6328a16f 100644 --- a/Students/RPerkins/session04/dict_setlab.py +++ b/Students/RPerkins/session04/dict_setlab.py @@ -26,4 +26,18 @@ d_prime = {} for k, v in d.items(): d_prime[k] = v.count('t') -print d_prime \ No newline at end of file +print d_prime + +s1 = set() +s2 = set() +s3 = set() +for k in range(20): + if k % 2 == 0: + s1.update([k]) + if k % 3 == 0: + s2.update([k]) + if k % 4 == 0: + s3.update([k]) +print s1 +print s2 +print s3 \ No newline at end of file From 875428a8a16d1e0274764e4f77ea889b8b3405eb Mon Sep 17 00:00:00 2001 From: meshmote Date: Wed, 22 Oct 2014 19:03:23 -0700 Subject: [PATCH 04/21] Added, debuged final set problem --- Students/RPerkins/session04/dict_setlab.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Students/RPerkins/session04/dict_setlab.py b/Students/RPerkins/session04/dict_setlab.py index 6328a16f..eefb49be 100644 --- a/Students/RPerkins/session04/dict_setlab.py +++ b/Students/RPerkins/session04/dict_setlab.py @@ -28,16 +28,24 @@ d_prime[k] = v.count('t') print d_prime -s1 = set() s2 = set() s3 = set() +s4 = set() for k in range(20): if k % 2 == 0: - s1.update([k]) - if k % 3 == 0: s2.update([k]) - if k % 4 == 0: + if k % 3 == 0: s3.update([k]) -print s1 + if k % 4 == 0: + s4.update([k]) print s2 -print s3 \ No newline at end of file +print s3 +print s4 +print s3.issubset(s2) +print s4.issubset(s2) + +p_set = {'P', 'y', 't', 'h', 'o', 'n'} +p_set.update(['i']) +m_set = frozenset(('m', 'a', 'r', 'a', 't', 'h', 'o', 'n')) +print p_set.union(m_set) +print p_set.intersection(m_set) From 629280de64fd9e97687baec95014b6c4db9d7d2a Mon Sep 17 00:00:00 2001 From: meshmote Date: Thu, 23 Oct 2014 19:55:29 -0700 Subject: [PATCH 05/21] Added, debugged safe_input version --- Students/RPerkins/session04/mailroom.py | 111 ++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Students/RPerkins/session04/mailroom.py diff --git a/Students/RPerkins/session04/mailroom.py b/Students/RPerkins/session04/mailroom.py new file mode 100644 index 00000000..e7498078 --- /dev/null +++ b/Students/RPerkins/session04/mailroom.py @@ -0,0 +1,111 @@ +__author__ = 'Robert W. Perkins' + + +def mk_dbase(): + """Create data structure for donor list""" + ndbase = [[], [], [], [], []] + # donor name, sum of donations, donation 1, donation 2, ... + ndbase[0] = ['Jeff McCarthy', 4000, 2500, 1000, 500] + ndbase[1] = ['Tabitha Simmons', 2450, 450, 2000] + ndbase[2] = ['Angela Cartwright', 5500, 5500] + ndbase[3] = ['Billy Murray', 3700, 3450, 250] + ndbase[4] = ['Alexa Dalton', 6940, 240, 1200, 5500] + return ndbase + + +def get_input(): + """Ask user whether to send thank you note or create report and return answer""" + answer = None + while not ((answer == '1') or (answer == '2') or (answer == "q")): + new_answer = raw_input("Enter '1' to Send a Thank-You Note, Enter '2' to Create a Report, Enter 'q' to quit-->") + answer = str(new_answer) + return answer + + +def safe_input(): + try: + new_d = raw_input("Enter donation amount (must be numeric)-->") + except EOFError: + return None + except KeyboardInterrupt: + return None + return new_d + + +def in_dbase(i_name, tar_dbase): + """ Check if name is in dbase and return boolean """ + for i in range(len(tar_dbase)): + if i_name in tar_dbase[i]: + return True + return False + + +def print_email(p_name, p_donation): + """ Print thank you not for donation from p_name """ + print 'Dear %s, Thanks so much for your generous donation of $%s. It is greatly appreciated!' % (p_name, p_donation) + + +def app_record(app_name, app_dbase): + """ Append an existing donor record """ + for i in range(len(app_dbase)): + if app_name in app_dbase[i]: + app_donation = safe_input() + app_dbase[i].append(app_donation) + app_dbase[i][1] += int(app_donation) + print_email(app_name, app_donation) + break + + +def add_record(add_name, add_dbase): + """ Add new donor to database """ + add_donation = safe_input() + new = [add_name, int(add_donation), add_donation] + add_dbase.append(new) + print_email(add_name, add_donation) + + +def thank_you(dbase): + """ Find or create a donor, add new donation, and return a thank you note""" + name = 'list' + while name == 'list': + new_name = raw_input("Enter full name of donor-->") + name = str(new_name) + if not (name == 'list'): + break + else: + for i in range(len(dbase)): + print dbase[i][0] + if in_dbase(name, dbase): + app_record(name, dbase) + else: + add_record(name, dbase) + + +def sum_element(key_dbase): + """set key for sorting on sum element of data structure""" + return key_dbase[1] + + +def mk_report(rep_dbase): + """ Create a sorted list of donors""" + print 'Donor Name\t\t\tTotal Donations\t# of Donations\t\tAverage Donation' + rep_dbase.sort(key=sum_element) + for j in range(len(rep_dbase)): + donor_slice = rep_dbase[j][2:] + num_donations = (len(donor_slice)) + avg_donation = int(rep_dbase[j][1])/num_donations + print '%s\t\t\t%s\t\t\t\t%s\t\t\t\t\t\t%s' % (rep_dbase[j][0], rep_dbase[j][1], num_donations, avg_donation) + + +if __name__ == '__main__': + donor = mk_dbase() + answer = None + while not (answer == "q"): + answer = get_input() + if answer == "q": + break + elif answer == "1": + thank_you(donor) + else: + mk_report(donor) + print "Exiting" From 55ff3e1256a4f143afbfd2634f5f3b70cbb7829c Mon Sep 17 00:00:00 2001 From: meshmote Date: Thu, 23 Oct 2014 20:15:53 -0700 Subject: [PATCH 06/21] Started file input function --- Students/RPerkins/session04/kata14.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Students/RPerkins/session04/kata14.py diff --git a/Students/RPerkins/session04/kata14.py b/Students/RPerkins/session04/kata14.py new file mode 100644 index 00000000..3042168a --- /dev/null +++ b/Students/RPerkins/session04/kata14.py @@ -0,0 +1,11 @@ +__author__ = 'Robert W. Perkins' + + +def get_book(): + f = open('secrets.txt') + secret_data = f.read() + f.close() + + +if __name__ == '__main__': + get_book() \ No newline at end of file From 22aaa7ca4a8493fef91986877023980400c38549 Mon Sep 17 00:00:00 2001 From: meshmote Date: Thu, 23 Oct 2014 20:33:50 -0700 Subject: [PATCH 07/21] Built, tested get_book function --- Students/RPerkins/session04/kata14.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Students/RPerkins/session04/kata14.py b/Students/RPerkins/session04/kata14.py index 3042168a..7bd88926 100644 --- a/Students/RPerkins/session04/kata14.py +++ b/Students/RPerkins/session04/kata14.py @@ -1,11 +1,13 @@ __author__ = 'Robert W. Perkins' -def get_book(): - f = open('secrets.txt') - secret_data = f.read() +def get_book(target): + f = open(target) + book_data = f.read() f.close() + return book_data if __name__ == '__main__': - get_book() \ No newline at end of file + source_text = '/intropython/data/sherlock_small.txt' + print get_book(source_text) \ No newline at end of file From 93fada23a5476b8e32688bfef61ee105c67f0e43 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sat, 25 Oct 2014 14:33:29 -0700 Subject: [PATCH 08/21] Added, debugged preprocessing functions on input text, started dictionary constructor --- Students/RPerkins/session04/kata14.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Students/RPerkins/session04/kata14.py b/Students/RPerkins/session04/kata14.py index 7bd88926..54f20fd2 100644 --- a/Students/RPerkins/session04/kata14.py +++ b/Students/RPerkins/session04/kata14.py @@ -2,12 +2,36 @@ def get_book(target): + """ Open target file and read contents into book_data""" f = open(target) book_data = f.read() f.close() return book_data +def strip_lines(in_text): + """ Replace newlines with spaces""" + return in_text.replace('\n', ' ') + + +def mk_wordlist(in_list): + """Split input string at ' ' and return word list""" + return in_list.split(' ') + + +def create_dict(orig_text): + """ Create trigram dictionary from orig_text""" + trigram = {} + word_list = mk_wordlist(strip_lines(orig_text)) + for word in word_list: + trigram_key = word_list[word] + " " + (word_list[word + 1]) + trigram[trigram_key] = word_list[word + 2] + + print word_list + print trigram + + if __name__ == '__main__': source_text = '/intropython/data/sherlock_small.txt' - print get_book(source_text) \ No newline at end of file + d = get_book(source_text) + create_dict(d) From 47115e3ae6177d6188e150b578c283b9b02a3a15 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sat, 25 Oct 2014 15:25:02 -0700 Subject: [PATCH 09/21] Dictionary constructor now working and fixed formatting of trigram_key --- Students/RPerkins/session04/kata14.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Students/RPerkins/session04/kata14.py b/Students/RPerkins/session04/kata14.py index 54f20fd2..00af76fb 100644 --- a/Students/RPerkins/session04/kata14.py +++ b/Students/RPerkins/session04/kata14.py @@ -9,7 +9,7 @@ def get_book(target): return book_data -def strip_lines(in_text): +def strip_newlines(in_text): """ Replace newlines with spaces""" return in_text.replace('\n', ' ') @@ -22,12 +22,19 @@ def mk_wordlist(in_list): def create_dict(orig_text): """ Create trigram dictionary from orig_text""" trigram = {} - word_list = mk_wordlist(strip_lines(orig_text)) - for word in word_list: - trigram_key = word_list[word] + " " + (word_list[word + 1]) - trigram[trigram_key] = word_list[word + 2] + word_list = mk_wordlist(strip_newlines(orig_text)) + for idx, word in enumerate(word_list): + if idx > (len(word_list) - 3): + break + else: + trigram_key = '%s %s' % (word_list[idx], word_list[idx + 1]) + print trigram_key + print idx + if trigram_key in trigram: + trigram[trigram_key].append(word_list[idx + 2]) + else: + trigram[trigram_key] = [word_list[idx + 2]] - print word_list print trigram From a6940d4e333f3cfe93d91efa4d46bb1e7387c1d2 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sat, 25 Oct 2014 16:23:50 -0700 Subject: [PATCH 10/21] Added random selection of initial key and get_newword function to select a random word from the list at a given key --- Students/RPerkins/session04/kata14.py | 41 +++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/Students/RPerkins/session04/kata14.py b/Students/RPerkins/session04/kata14.py index 00af76fb..0dd35e53 100644 --- a/Students/RPerkins/session04/kata14.py +++ b/Students/RPerkins/session04/kata14.py @@ -1,5 +1,5 @@ __author__ = 'Robert W. Perkins' - +import random def get_book(target): """ Open target file and read contents into book_data""" @@ -15,12 +15,12 @@ def strip_newlines(in_text): def mk_wordlist(in_list): - """Split input string at ' ' and return word list""" + """Split input string at spaces and return word list""" return in_list.split(' ') def create_dict(orig_text): - """ Create trigram dictionary from orig_text""" + """ Create trigram dictionary""" trigram = {} word_list = mk_wordlist(strip_newlines(orig_text)) for idx, word in enumerate(word_list): @@ -28,17 +28,42 @@ def create_dict(orig_text): break else: trigram_key = '%s %s' % (word_list[idx], word_list[idx + 1]) - print trigram_key - print idx + #print trigram_key + #print idx if trigram_key in trigram: trigram[trigram_key].append(word_list[idx + 2]) else: trigram[trigram_key] = [word_list[idx + 2]] + #print trigram + return trigram + + +def get_newword(word_key, word_dict): + """Return a random word from the list at the provided key""" + new_wordlist = word_dict.get(word_key) + return random.choice(new_wordlist) + - print trigram +def create_newbook(trigram_dict, word_limit, w_line): + """Create random output of num_words words, using trigram_dict keys to generate new words""" + + start_key = random.choice(list(trigram_dict.keys())) + new_word = get_newword(start_key, trigram_dict) + + print start_key + print type(start_key) + print new_word + print type(new_word) + return None if __name__ == '__main__': + # num_words gives the number of words to be generated + # words_line gives the number of words per line + num_words = 200 + words_line = 20 source_text = '/intropython/data/sherlock_small.txt' - d = get_book(source_text) - create_dict(d) + + new_inbook = get_book(source_text) + new_dict = create_dict(new_inbook) + new_outbook = create_newbook(new_dict, num_words, words_line) \ No newline at end of file From a3c72956f87dd10f6c2513147adf57ea9d6b6195 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sat, 25 Oct 2014 17:45:43 -0700 Subject: [PATCH 11/21] Restored for loops to add in newlines and punctuation at end --- Students/RPerkins/session04/kata14.py | 44 +++++++++++++++++++++------ 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/Students/RPerkins/session04/kata14.py b/Students/RPerkins/session04/kata14.py index 0dd35e53..6c1a82f1 100644 --- a/Students/RPerkins/session04/kata14.py +++ b/Students/RPerkins/session04/kata14.py @@ -1,6 +1,7 @@ __author__ = 'Robert W. Perkins' import random + def get_book(target): """ Open target file and read contents into book_data""" f = open(target) @@ -38,6 +39,11 @@ def create_dict(orig_text): return trigram +def get_randomkey(t_gram): + """Return a random key""" + return random.choice(list(t_gram.keys())) + + def get_newword(word_key, word_dict): """Return a random word from the list at the provided key""" new_wordlist = word_dict.get(word_key) @@ -46,15 +52,32 @@ def get_newword(word_key, word_dict): def create_newbook(trigram_dict, word_limit, w_line): """Create random output of num_words words, using trigram_dict keys to generate new words""" - - start_key = random.choice(list(trigram_dict.keys())) + start_key = get_randomkey(trigram_dict) new_word = get_newword(start_key, trigram_dict) - print start_key - print type(start_key) - print new_word - print type(new_word) - return None + #print start_key + out_list = start_key.split(' ') + out_list.append(new_word) + #print out_list + left_frame = 0 + + for i in range(word_limit): + for j in range(w_line): + next_key = '%s %s' % (out_list[left_frame+1], out_list[left_frame + 2]) + #print next_key in trigram_dict + while not next_key in trigram_dict: + next_key = get_randomkey(trigram_dict) + next_word = get_newword(next_key, trigram_dict) + out_list.append(next_word) + left_frame += 1 + out_list.append('\n') + out_list.append('.') + out_string = ' '.join(out_list) + return out_string + + #print new_word + #print type(new_word) + #print out_list if __name__ == '__main__': @@ -62,8 +85,11 @@ def create_newbook(trigram_dict, word_limit, w_line): # words_line gives the number of words per line num_words = 200 words_line = 20 - source_text = '/intropython/data/sherlock_small.txt' + #source_text = '/intropython/data/sherlock_small.txt' + source_text = '/intropython/data/sherlock.txt' new_inbook = get_book(source_text) new_dict = create_dict(new_inbook) - new_outbook = create_newbook(new_dict, num_words, words_line) \ No newline at end of file + #print new_dict + new_outbook = create_newbook(new_dict, num_words, words_line) + print new_outbook \ No newline at end of file From 7f7f7e3590951a529c27ec82eeb116c56d25a9d8 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sat, 25 Oct 2014 18:12:24 -0700 Subject: [PATCH 12/21] Added ellipses at beginning and end of generated text, removed debug code --- Students/RPerkins/session04/kata14.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Students/RPerkins/session04/kata14.py b/Students/RPerkins/session04/kata14.py index 6c1a82f1..68f19861 100644 --- a/Students/RPerkins/session04/kata14.py +++ b/Students/RPerkins/session04/kata14.py @@ -29,13 +29,10 @@ def create_dict(orig_text): break else: trigram_key = '%s %s' % (word_list[idx], word_list[idx + 1]) - #print trigram_key - #print idx if trigram_key in trigram: trigram[trigram_key].append(word_list[idx + 2]) else: trigram[trigram_key] = [word_list[idx + 2]] - #print trigram return trigram @@ -54,31 +51,24 @@ def create_newbook(trigram_dict, word_limit, w_line): """Create random output of num_words words, using trigram_dict keys to generate new words""" start_key = get_randomkey(trigram_dict) new_word = get_newword(start_key, trigram_dict) - - #print start_key out_list = start_key.split(' ') + out_list.insert(0, '...') out_list.append(new_word) - #print out_list left_frame = 0 for i in range(word_limit): for j in range(w_line): next_key = '%s %s' % (out_list[left_frame+1], out_list[left_frame + 2]) - #print next_key in trigram_dict while not next_key in trigram_dict: next_key = get_randomkey(trigram_dict) next_word = get_newword(next_key, trigram_dict) out_list.append(next_word) left_frame += 1 out_list.append('\n') - out_list.append('.') + out_list.append('...') out_string = ' '.join(out_list) return out_string - #print new_word - #print type(new_word) - #print out_list - if __name__ == '__main__': # num_words gives the number of words to be generated @@ -90,6 +80,5 @@ def create_newbook(trigram_dict, word_limit, w_line): new_inbook = get_book(source_text) new_dict = create_dict(new_inbook) - #print new_dict new_outbook = create_newbook(new_dict, num_words, words_line) print new_outbook \ No newline at end of file From a16bd1491422a263ca33879cbc09acbe17ec7fcf Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 14:23:27 -0700 Subject: [PATCH 13/21] Added, debugged pathlib version --- Students/RPerkins/session04/print_filepath.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Students/RPerkins/session04/print_filepath.py diff --git a/Students/RPerkins/session04/print_filepath.py b/Students/RPerkins/session04/print_filepath.py new file mode 100644 index 00000000..9da9aa36 --- /dev/null +++ b/Students/RPerkins/session04/print_filepath.py @@ -0,0 +1,8 @@ +__author__ = 'Robert W. Perkins' + +import pathlib + + +pth = pathlib.Path('./') +for f in pth.iterdir(): + print '%s\%s' % (pth.absolute(), f) From 009e1522c3622a8e5c9f5820afc0b97b24c4a30a Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 14:24:41 -0700 Subject: [PATCH 14/21] Removed extra lines and whitespace --- Students/RPerkins/session04/print_filepath.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Students/RPerkins/session04/print_filepath.py b/Students/RPerkins/session04/print_filepath.py index 9da9aa36..22128e94 100644 --- a/Students/RPerkins/session04/print_filepath.py +++ b/Students/RPerkins/session04/print_filepath.py @@ -2,7 +2,6 @@ import pathlib - pth = pathlib.Path('./') for f in pth.iterdir(): print '%s\%s' % (pth.absolute(), f) From 026a4c7e97190025a6f71fe0168c3b6e9d4adc0d Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 15:15:15 -0700 Subject: [PATCH 15/21] Added copy progam, source file, and target directory --- Students/RPerkins/session04/test/testcopy.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Students/RPerkins/session04/test/testcopy.txt diff --git a/Students/RPerkins/session04/test/testcopy.txt b/Students/RPerkins/session04/test/testcopy.txt new file mode 100644 index 00000000..e6d2cb24 --- /dev/null +++ b/Students/RPerkins/session04/test/testcopy.txt @@ -0,0 +1 @@ +This is the test file content \ No newline at end of file From 2824ccbbba8985bf2a637fad82e947f24d3a4c06 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 15:16:35 -0700 Subject: [PATCH 16/21] Added copier and source file --- Students/RPerkins/session04/file_copier.py | 7 +++++++ Students/RPerkins/session04/test.txt | 1 + 2 files changed, 8 insertions(+) create mode 100644 Students/RPerkins/session04/file_copier.py create mode 100644 Students/RPerkins/session04/test.txt diff --git a/Students/RPerkins/session04/file_copier.py b/Students/RPerkins/session04/file_copier.py new file mode 100644 index 00000000..85656b2a --- /dev/null +++ b/Students/RPerkins/session04/file_copier.py @@ -0,0 +1,7 @@ +__author__ = 'Robert W. Perkins' + +from_file = './test.txt' +to_file = './test/testcopy.txt' + +indata = open(from_file).read() +open(to_file, 'w').write(indata) diff --git a/Students/RPerkins/session04/test.txt b/Students/RPerkins/session04/test.txt new file mode 100644 index 00000000..e6d2cb24 --- /dev/null +++ b/Students/RPerkins/session04/test.txt @@ -0,0 +1 @@ +This is the test file content \ No newline at end of file From 56d73297dd02a7455b66edc001f267ee28926d27 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 16:23:17 -0700 Subject: [PATCH 17/21] Converted database structure to dict and got all dependent functions working --- Students/RPerkins/session04/mailroom.py | 80 ++++++++++++------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/Students/RPerkins/session04/mailroom.py b/Students/RPerkins/session04/mailroom.py index e7498078..0a819b33 100644 --- a/Students/RPerkins/session04/mailroom.py +++ b/Students/RPerkins/session04/mailroom.py @@ -3,13 +3,14 @@ def mk_dbase(): """Create data structure for donor list""" - ndbase = [[], [], [], [], []] # donor name, sum of donations, donation 1, donation 2, ... - ndbase[0] = ['Jeff McCarthy', 4000, 2500, 1000, 500] - ndbase[1] = ['Tabitha Simmons', 2450, 450, 2000] - ndbase[2] = ['Angela Cartwright', 5500, 5500] - ndbase[3] = ['Billy Murray', 3700, 3450, 250] - ndbase[4] = ['Alexa Dalton', 6940, 240, 1200, 5500] + ndbase = { + 'Jeff McCarthy': [4000, 2500, 1000, 500], + 'Tabitha Simmons': [2450, 450, 2000], + 'Angela Cartwright': [5500, 5500], + 'Billy Murray': [3700, 3450, 250], + 'Alexa Dalton': [6940, 240, 1200, 5500] + } return ndbase @@ -29,15 +30,15 @@ def safe_input(): return None except KeyboardInterrupt: return None - return new_d + return int(new_d) -def in_dbase(i_name, tar_dbase): - """ Check if name is in dbase and return boolean """ - for i in range(len(tar_dbase)): - if i_name in tar_dbase[i]: - return True - return False +#def in_dbase(i_name, tar_dbase): + #""" Check if name is in dbase and return boolean """ + #for i in range(len(tar_dbase)): + #if i_name in tar_dbase[i]: + #return True + #return False def print_email(p_name, p_donation): @@ -45,26 +46,23 @@ def print_email(p_name, p_donation): print 'Dear %s, Thanks so much for your generous donation of $%s. It is greatly appreciated!' % (p_name, p_donation) -def app_record(app_name, app_dbase): +def app_record(app_name, app_dict): """ Append an existing donor record """ - for i in range(len(app_dbase)): - if app_name in app_dbase[i]: - app_donation = safe_input() - app_dbase[i].append(app_donation) - app_dbase[i][1] += int(app_donation) - print_email(app_name, app_donation) - break + app_donation = safe_input() + app_dict[app_name].append(app_donation) + app_dict[app_name][0] += app_donation + #print app_dict + print_email(app_name, app_donation) -def add_record(add_name, add_dbase): +def add_record(add_name, add_dict): """ Add new donor to database """ add_donation = safe_input() - new = [add_name, int(add_donation), add_donation] - add_dbase.append(new) + add_dict[add_name] = [add_donation, add_donation] print_email(add_name, add_donation) -def thank_you(dbase): +def thank_you(donor_dict): """ Find or create a donor, add new donation, and return a thank you note""" name = 'list' while name == 'list': @@ -73,28 +71,30 @@ def thank_you(dbase): if not (name == 'list'): break else: - for i in range(len(dbase)): - print dbase[i][0] - if in_dbase(name, dbase): - app_record(name, dbase) + for item in donor_dict: + print item + + if name in donor_dict: + app_record(name, donor_dict) else: - add_record(name, dbase) + add_record(name, donor_dict) -def sum_element(key_dbase): - """set key for sorting on sum element of data structure""" - return key_dbase[1] +#def sum_element(key_dbase): + #"""set key for sorting on sum element of data structure""" + #return key_dbase[1] -def mk_report(rep_dbase): +def mk_report(rep_dict): """ Create a sorted list of donors""" print 'Donor Name\t\t\tTotal Donations\t# of Donations\t\tAverage Donation' - rep_dbase.sort(key=sum_element) - for j in range(len(rep_dbase)): - donor_slice = rep_dbase[j][2:] - num_donations = (len(donor_slice)) - avg_donation = int(rep_dbase[j][1])/num_donations - print '%s\t\t\t%s\t\t\t\t%s\t\t\t\t\t\t%s' % (rep_dbase[j][0], rep_dbase[j][1], num_donations, avg_donation) + #rep_dbase.sort(key=sum_element) + for j, k in rep_dict.items(): + num_donations = len(k)-1 + #print num_donations + #print len(k) + avg_donation = k[0]/(len(k)-1) + print '%s\t\t\t%s\t\t\t\t%s\t\t\t\t\t\t%s' % (j, k[0], num_donations, avg_donation) if __name__ == '__main__': From 3de8ac80c1319910658173f1a68bad78f05ea9c9 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 16:38:49 -0700 Subject: [PATCH 18/21] Added, debugged email text file writing function --- Students/RPerkins/session04/mailroom.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Students/RPerkins/session04/mailroom.py b/Students/RPerkins/session04/mailroom.py index 0a819b33..08cba004 100644 --- a/Students/RPerkins/session04/mailroom.py +++ b/Students/RPerkins/session04/mailroom.py @@ -33,13 +33,6 @@ def safe_input(): return int(new_d) -#def in_dbase(i_name, tar_dbase): - #""" Check if name is in dbase and return boolean """ - #for i in range(len(tar_dbase)): - #if i_name in tar_dbase[i]: - #return True - #return False - def print_email(p_name, p_donation): """ Print thank you not for donation from p_name """ @@ -80,11 +73,20 @@ def thank_you(donor_dict): add_record(name, donor_dict) +def write_efile(name, donation_list): + """write a donor email to disk named "name".txt""" + to_file = './%s.txt' % name + outdata = 'Dear %s, Thanks so much for your recent generous donation of $%s. ' \ + 'It is greatly appreciated!' % (name, donation_list[-1]) + open(to_file, 'w').write(outdata) + + #def sum_element(key_dbase): #"""set key for sorting on sum element of data structure""" #return key_dbase[1] + def mk_report(rep_dict): """ Create a sorted list of donors""" print 'Donor Name\t\t\tTotal Donations\t# of Donations\t\tAverage Donation' @@ -95,6 +97,8 @@ def mk_report(rep_dict): #print len(k) avg_donation = k[0]/(len(k)-1) print '%s\t\t\t%s\t\t\t\t%s\t\t\t\t\t\t%s' % (j, k[0], num_donations, avg_donation) + write_efile(j,k) + if __name__ == '__main__': From 85a45f672528f8b901f9c341f5216063a8d097e7 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 16:57:02 -0700 Subject: [PATCH 19/21] Added, debugged dict function for user choice --- Students/RPerkins/session04/mailroom.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Students/RPerkins/session04/mailroom.py b/Students/RPerkins/session04/mailroom.py index 08cba004..2c003029 100644 --- a/Students/RPerkins/session04/mailroom.py +++ b/Students/RPerkins/session04/mailroom.py @@ -3,7 +3,7 @@ def mk_dbase(): """Create data structure for donor list""" - # donor name, sum of donations, donation 1, donation 2, ... + # donor name = key: sum of donations, donation 1, donation 2, ... ndbase = { 'Jeff McCarthy': [4000, 2500, 1000, 500], 'Tabitha Simmons': [2450, 450, 2000], @@ -16,11 +16,16 @@ def mk_dbase(): def get_input(): """Ask user whether to send thank you note or create report and return answer""" - answer = None - while not ((answer == '1') or (answer == '2') or (answer == "q")): - new_answer = raw_input("Enter '1' to Send a Thank-You Note, Enter '2' to Create a Report, Enter 'q' to quit-->") - answer = str(new_answer) - return answer + + choices = { + '1': 'Enter "1" to Send a Thank-You Note', + '2': 'Enter "2" to Create a Report', + 'q': 'Enter "q" to quit-->' + } + in_put = None + while not in_put in choices: + in_put = raw_input('%s, %s, %s' % (choices['1'], choices['2'], choices['q'])) + return in_put def safe_input(): From 11930a3599cd8a6c21290d700a906ff3e942cc54 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 17:38:09 -0700 Subject: [PATCH 20/21] Added, debugged use of dict and format to make thank you template. --- Students/RPerkins/session04/mailroom.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Students/RPerkins/session04/mailroom.py b/Students/RPerkins/session04/mailroom.py index 2c003029..bb14eca1 100644 --- a/Students/RPerkins/session04/mailroom.py +++ b/Students/RPerkins/session04/mailroom.py @@ -38,10 +38,15 @@ def safe_input(): return int(new_d) - def print_email(p_name, p_donation): - """ Print thank you not for donation from p_name """ - print 'Dear %s, Thanks so much for your generous donation of $%s. It is greatly appreciated!' % (p_name, p_donation) + """ Print thank you note for donation from p_name """ + #print 'Dear %s, Thanks so much for your generous donation of $%s. ' \ + #'It is greatly appreciated!' % (p_name, p_donation) + ltr_temp = {'Template1': 'Dear {name}, Thanks so much for your generous donation of ${donation}. ' + 'It is greatly appreciated!' + } + + print ltr_temp['Template1'].format(name=p_name, donation=p_donation) def app_record(app_name, app_dict): @@ -49,7 +54,6 @@ def app_record(app_name, app_dict): app_donation = safe_input() app_dict[app_name].append(app_donation) app_dict[app_name][0] += app_donation - #print app_dict print_email(app_name, app_donation) @@ -91,21 +95,17 @@ def write_efile(name, donation_list): #return key_dbase[1] - def mk_report(rep_dict): """ Create a sorted list of donors""" print 'Donor Name\t\t\tTotal Donations\t# of Donations\t\tAverage Donation' #rep_dbase.sort(key=sum_element) for j, k in rep_dict.items(): num_donations = len(k)-1 - #print num_donations - #print len(k) avg_donation = k[0]/(len(k)-1) print '%s\t\t\t%s\t\t\t\t%s\t\t\t\t\t\t%s' % (j, k[0], num_donations, avg_donation) write_efile(j,k) - if __name__ == '__main__': donor = mk_dbase() answer = None From 644610b4ec7afff079c3194054b21218641a6186 Mon Sep 17 00:00:00 2001 From: meshmote Date: Sun, 26 Oct 2014 17:45:38 -0700 Subject: [PATCH 21/21] Removed debugging lines --- Students/RPerkins/session04/mailroom.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Students/RPerkins/session04/mailroom.py b/Students/RPerkins/session04/mailroom.py index bb14eca1..9a8e5092 100644 --- a/Students/RPerkins/session04/mailroom.py +++ b/Students/RPerkins/session04/mailroom.py @@ -40,8 +40,6 @@ def safe_input(): def print_email(p_name, p_donation): """ Print thank you note for donation from p_name """ - #print 'Dear %s, Thanks so much for your generous donation of $%s. ' \ - #'It is greatly appreciated!' % (p_name, p_donation) ltr_temp = {'Template1': 'Dear {name}, Thanks so much for your generous donation of ${donation}. ' 'It is greatly appreciated!' }