-
Notifications
You must be signed in to change notification settings - Fork 77
Added session04 homework #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3888035
66a38f4
0524242
875428a
629280d
55ff3e1
22aaa7c
93fada2
47115e3
a6940d4
a3c7295
7f7f7e3
a16bd14
009e152
026a4c7
2824ccb
56d7329
3de8ac8
85a45f6
11930a3
644610b
47df8af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| __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 | ||
|
|
||
| d_prime = {} | ||
| for k, v in d.items(): | ||
| d_prime[k] = v.count('t') | ||
| print d_prime | ||
|
|
||
| s2 = set() | ||
| s3 = set() | ||
| s4 = set() | ||
| for k in range(20): | ||
| if k % 2 == 0: | ||
| s2.update([k]) | ||
| if k % 3 == 0: | ||
| s3.update([k]) | ||
| if k % 4 == 0: | ||
| s4.update([k]) | ||
| print s2 | ||
| 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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| __author__ = 'Robert W. Perkins' | ||
| import random | ||
|
|
||
|
|
||
| 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_newlines(in_text): | ||
| """ Replace newlines with spaces""" | ||
| return in_text.replace('\n', ' ') | ||
|
|
||
|
|
||
| def mk_wordlist(in_list): | ||
| """Split input string at spaces and return word list""" | ||
| return in_list.split(' ') | ||
|
|
||
|
|
||
| def create_dict(orig_text): | ||
| """ Create trigram dictionary""" | ||
| trigram = {} | ||
| 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]) | ||
| if trigram_key in trigram: | ||
| trigram[trigram_key].append(word_list[idx + 2]) | ||
| else: | ||
| trigram[trigram_key] = [word_list[idx + 2]] | ||
| 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) | ||
| return random.choice(new_wordlist) | ||
|
|
||
|
|
||
| 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) | ||
| out_list = start_key.split(' ') | ||
| out_list.insert(0, '...') | ||
| out_list.append(new_word) | ||
| 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]) | ||
| 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 | ||
|
|
||
|
|
||
| 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' | ||
| 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) | ||
| print new_outbook |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| __author__ = 'Robert W. Perkins' | ||
|
|
||
|
|
||
| def mk_dbase(): | ||
| """Create data structure for donor list""" | ||
| # donor name = key: sum of donations, donation 1, donation 2, ... | ||
| 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 | ||
|
|
||
|
|
||
| def get_input(): | ||
| """Ask user whether to send thank you note or create report and 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(): | ||
| try: | ||
| new_d = raw_input("Enter donation amount (must be numeric)-->") | ||
| except EOFError: | ||
| return None | ||
| except KeyboardInterrupt: | ||
| return None | ||
| return int(new_d) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will happen if the input is not an integer? |
||
|
|
||
|
|
||
| def print_email(p_name, p_donation): | ||
| """ Print thank you note for donation from p_name """ | ||
| ltr_temp = {'Template1': 'Dear {name}, Thanks so much for your generous donation of ${donation}. ' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why put this in a dict? are you anticipating being able to chose more than one template? |
||
| 'It is greatly appreciated!' | ||
| } | ||
|
|
||
| print ltr_temp['Template1'].format(name=p_name, donation=p_donation) | ||
|
|
||
|
|
||
| def app_record(app_name, app_dict): | ||
| """ Append an existing donor record """ | ||
| app_donation = safe_input() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will happen if safe_input returns None? |
||
| app_dict[app_name].append(app_donation) | ||
| app_dict[app_name][0] += app_donation | ||
| print_email(app_name, app_donation) | ||
|
|
||
|
|
||
| def add_record(add_name, add_dict): | ||
| """ Add new donor to database """ | ||
| add_donation = safe_input() | ||
| add_dict[add_name] = [add_donation, add_donation] | ||
| print_email(add_name, add_donation) | ||
|
|
||
|
|
||
| def thank_you(donor_dict): | ||
| """ 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 item in donor_dict: | ||
| print item | ||
|
|
||
| if name in donor_dict: | ||
| app_record(name, donor_dict) | ||
| else: | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the "./" is assumed anyway. |
||
| 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' | ||
| #rep_dbase.sort(key=sum_element) | ||
| for j, k in rep_dict.items(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how could you sort this? |
||
| num_donations = len(k)-1 | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tabs are tricky -- different terminals, etc will expand them differently. better to use spaces -- and you can use the formatters to align it all for you. |
||
| write_efile(j,k) | ||
|
|
||
|
|
||
| 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" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| __author__ = 'Robert W. Perkins' | ||
|
|
||
| import pathlib | ||
|
|
||
| pth = pathlib.Path('./') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! pathlib is pretty cool, eh? |
||
| for f in pth.iterdir(): | ||
| print '%s\%s' % (pth.absolute(), f) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| This is the test file content |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| This is the test file content | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably a good idea for you test file to be more than one line, just in case... |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while it makes some sense to store a calculated value, it does mean you need to be careful to keep them in sync. You also might want to use a slightly more complex data structure, so that it's clear that the total is a differnt thing:
maybe:
ndbase = {
'Jeff McCarthy': (4000, [2500, 1000, 500]),