From 14fc8c4a6f075d5a217f1a0d2db373a249f657e1 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 2 May 2014 03:04:25 -0700 Subject: [PATCH 1/9] Example code for Episode 15 --- episode14/README.txt | 3 + episode14/coding101.py | 30 +++++----- episode15/.gitignore | 1 + episode15/README.txt | 3 + episode15/codingYouTube101.py | 104 ++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 episode14/README.txt create mode 100644 episode15/.gitignore create mode 100644 episode15/README.txt create mode 100644 episode15/codingYouTube101.py diff --git a/episode14/README.txt b/episode14/README.txt new file mode 100644 index 0000000..6d1f9de --- /dev/null +++ b/episode14/README.txt @@ -0,0 +1,3 @@ +Example for TWiT Coding 101 - Episode 14 + +Displays a list of the Coding 101 episodes (from a file), upon selection of a specific episode displays its title, air date and description. \ No newline at end of file diff --git a/episode14/coding101.py b/episode14/coding101.py index c05e628..c5a9a5c 100644 --- a/episode14/coding101.py +++ b/episode14/coding101.py @@ -10,10 +10,10 @@ import textwrap # Print opening title -print "================================ CODING 101 ================================" +print '================================ CODING 101 ================================' # Open the file -episodeFile = open("coding101.txt", "r") +episodeFile = open('coding101.txt', 'r') # Read the lines into a list linesFromFile = episodeFile.readlines() # Close the file @@ -25,25 +25,25 @@ while run: # Print an empty line print # Print the episode list - print "Choose an episode:" + print 'Choose an episode:' print # Loop through the episodes list. count = 0 for line in linesFromFile: - # Increment the episode number. + # Increment the episode number count += 1 # Split the line using tab as the delimiter # The format is: DateEpisode TitleDescription - episode = line.split("\t") + episode = line.split('\t') # Print the episode number & title. # The episode number is converted into a str, and right justified - print "\t{0}. {1}".format(str(count).rjust(2), episode[1]) + print '\t{0}. {1}'.format(str(count).rjust(2), episode[1]) print # Ask for the episode number - choice = raw_input("Enter 0-" + str(count) + " (or ENTER to quit): ") + choice = raw_input('Enter 1-' + str(count) + ' (or ENTER to quit): ') # Did you press enter? if not choice: @@ -52,7 +52,7 @@ while run: # Validate the selected episode number. It must be a number... elif choice.isdigit(): - # Convert the selection to an integer. + # Convert the selection to an integer selection = int(choice) # It must also be between 1 and the total number of episodes @@ -60,14 +60,14 @@ while run: if 1 <= selection <= count: print print - print "============================================================================" - # Get and split the line for the episode, the list is zero-based. + print '============================================================================' + # Get and split the line for the episode, the list is zero-based episode = linesFromFile[selection - 1].split('\t') - # Print the episode, title - print "Episode #" + choice + ": \"" + episode[1] + '" aired on ' + episode[0] + # Print the episode title and aired date + print 'Episode #' + choice + ": \"" + episode[1] + '" aired on ' + episode[0] print - # Print the description, using textwrap to make it look pretty. + # Print the description, using textwrap to make it look pretty print textwrap.fill(episode[2], width=76) - print "============================================================================" + print '============================================================================' print - raw_input("Press ENTER to continue...") + raw_input('Press ENTER to continue...') diff --git a/episode15/.gitignore b/episode15/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/episode15/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/episode15/README.txt b/episode15/README.txt new file mode 100644 index 0000000..b942784 --- /dev/null +++ b/episode15/README.txt @@ -0,0 +1,3 @@ +Example for TWiT Coding 101 - Episode 15 + +Displays a list of the Coding 101 episodes (fetched from YouTube), upon selection of a specific episode displays its title, description and URL. \ No newline at end of file diff --git a/episode15/codingYouTube101.py b/episode15/codingYouTube101.py new file mode 100644 index 0000000..8ddb739 --- /dev/null +++ b/episode15/codingYouTube101.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python + +# Coding YouTube 101 +# +# Written by Erik C. Thauvin (erik@thauvin.net) +# http://erik.thauvin.net/ +# http://github.com/ethauvin/coding101/ +# May 1, 2014 + +# To warp long lines +import textwrap +# For making http requests +import urllib2 +# For decoding YouTube's API responses +import json + +# Your YouTube API key from https://developers.google.com/youtube/registering_an_application +youTubeApiKey = '' + +# Print opening title +print '============================= CODING YOUTUBE 101 =============================' +# Print an empty line +print + +# Ask for the API key if none is specified above +while not youTubeApiKey.isalnum(): + youTubeApiKey = raw_input('Enter your YouTube API Key: ') + # Did you press enter? + if not youTubeApiKey: + # Stop the program execution. Bye-Bye! + exit(0) + +# Search for Coding 101 videos using the channel ID and our API key +youTubeResponse = urllib2.urlopen( + 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCSxIcr2rZZcoU7rSGXaEQag&maxResults=20&order=date&key=' + youTubeApiKey) + +# Load the list of videos returned by YouTube +videos = json.load(youTubeResponse) + +# Declare the episodes list +episodes = [] + +# Loop through the videos to fill in the episodes list +for video in videos['items']: + # Ensure that it is a video + if video['id']['kind'] == 'youtube#video': + # Store the title, description and link of the video in our list, separated by tabs + # The format is: TitleDescriptionURL + episodes.append( + video['snippet']['title'] + '\t' + video['snippet']['description'] + '\t' + + 'http://youtube.com/watch?v=' + video['id']['videoId']) + +# Let's go. The program will execute until run = False +run = True +while run: + print + print 'Choose an episode:' + print + + # Loop through and print the episodes list. + count = 0 + for episode in episodes: + # Increment the loop count, same as: count = count + 1 + count += 1 + # Split the episode info using tab as the delimiter + episodeInfo = episode.split('\t') + # Print the loop count & title + # The count is converted into a str, and right justified + print ' {0}. {1}'.format(str(count).rjust(2), episodeInfo[0]) + + print + + # Ask for the episode number + choice = raw_input('Enter 1-' + str(count) + ' (or ENTER to quit): ') + + # Did you press enter? + if not choice: + # Stop the program execution. Bye-Bye! + run = False + + # Validate the selected episode number. It must be a number... + elif choice.isdigit(): + # Convert the selection to an integer + selection = int(choice) + + # It must also be between 1 and the total number of episodes + # Could be written as: selection >= 1 and selection <= count + if 1 <= selection <= count: + print + print + print '==============================================================================' + # Split the selected episode info using tab as the delimiter, the list is zero-based + episodeInfo = episodes[selection - 1].split('\t') + # Print the episode title + print episodeInfo[0] + print + # Print the description, using textwrap to make it look pretty + print textwrap.fill(episodeInfo[1], width=76) + print + # Print the URL + print episodeInfo[2] + print '==============================================================================' + print + raw_input('Press ENTER to continue...') From 67c81ce499db646d3510ef7fe4e85e51f2050622 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 2 May 2014 14:50:42 -0700 Subject: [PATCH 2/9] Reversed the episodes list order to match the loop count with the actual episode number. --- episode15/codingYouTube101.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/episode15/codingYouTube101.py b/episode15/codingYouTube101.py index 8ddb739..8510bc7 100644 --- a/episode15/codingYouTube101.py +++ b/episode15/codingYouTube101.py @@ -50,6 +50,9 @@ for video in videos['items']: video['snippet']['title'] + '\t' + video['snippet']['description'] + '\t' + 'http://youtube.com/watch?v=' + video['id']['videoId']) +# Reverse sort the episodes list +episodes.reverse() + # Let's go. The program will execute until run = False run = True while run: @@ -60,18 +63,18 @@ while run: # Loop through and print the episodes list. count = 0 for episode in episodes: - # Increment the loop count, same as: count = count + 1 - count += 1 # Split the episode info using tab as the delimiter episodeInfo = episode.split('\t') # Print the loop count & title # The count is converted into a str, and right justified print ' {0}. {1}'.format(str(count).rjust(2), episodeInfo[0]) + # Increment the loop count, same as: count = count + 1 + count += 1 print # Ask for the episode number - choice = raw_input('Enter 1-' + str(count) + ' (or ENTER to quit): ') + choice = raw_input('Enter 0-' + str(count) + ' (or ENTER to quit): ') # Did you press enter? if not choice: @@ -84,13 +87,13 @@ while run: selection = int(choice) # It must also be between 1 and the total number of episodes - # Could be written as: selection >= 1 and selection <= count - if 1 <= selection <= count: + # Could be written as: selection >= 0 and selection <= count + if 0 <= selection <= count: print print print '==============================================================================' - # Split the selected episode info using tab as the delimiter, the list is zero-based - episodeInfo = episodes[selection - 1].split('\t') + # Split the selected episode info using tab as the delimiter + episodeInfo = episodes[selection].split('\t') # Print the episode title print episodeInfo[0] print From 0b8f4c755c894f4dd8b035c5e4a618c271831120 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 2 May 2014 15:05:46 -0700 Subject: [PATCH 3/9] Reversed the episodes list order to match the loop count with the actual episode number. --- episode15/codingYouTube101.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/episode15/codingYouTube101.py b/episode15/codingYouTube101.py index 8510bc7..28526b5 100644 --- a/episode15/codingYouTube101.py +++ b/episode15/codingYouTube101.py @@ -68,8 +68,10 @@ while run: # Print the loop count & title # The count is converted into a str, and right justified print ' {0}. {1}'.format(str(count).rjust(2), episodeInfo[0]) - # Increment the loop count, same as: count = count + 1 - count += 1 + # Increment the loop count, unless we're on the last episode + if (count + 1) < len(episodes): + # Same as: count = count + 1 + count += 1 print From fe8107cfc30f64cfbb3099432e0a459419d09b44 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 2 May 2014 17:52:27 -0700 Subject: [PATCH 4/9] Fixed comments. --- episode15/codingYouTube101.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/episode15/codingYouTube101.py b/episode15/codingYouTube101.py index 28526b5..1572dd5 100644 --- a/episode15/codingYouTube101.py +++ b/episode15/codingYouTube101.py @@ -88,7 +88,7 @@ while run: # Convert the selection to an integer selection = int(choice) - # It must also be between 1 and the total number of episodes + # It must also be between 0 and the total number of episodes # Could be written as: selection >= 0 and selection <= count if 0 <= selection <= count: print From 7802098b92f760ed6b8d60f00552de684cba77b4 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 9 May 2014 11:10:41 -0700 Subject: [PATCH 5/9] Example for Episode 16 --- episode15/.gitignore | 2 +- episode16/.gitignore | 2 ++ episode16/README.txt | 3 ++ episode16/radomizer.py | 71 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 episode16/.gitignore create mode 100644 episode16/README.txt create mode 100644 episode16/radomizer.py diff --git a/episode15/.gitignore b/episode15/.gitignore index 723ef36..485dee6 100644 --- a/episode15/.gitignore +++ b/episode15/.gitignore @@ -1 +1 @@ -.idea \ No newline at end of file +.idea diff --git a/episode16/.gitignore b/episode16/.gitignore new file mode 100644 index 0000000..e49c53b --- /dev/null +++ b/episode16/.gitignore @@ -0,0 +1,2 @@ +.idea +random.txt diff --git a/episode16/README.txt b/episode16/README.txt new file mode 100644 index 0000000..e811346 --- /dev/null +++ b/episode16/README.txt @@ -0,0 +1,3 @@ +Example for TWiT Coding 101 - Episode 16 + +Generates 100 random numbers, sorts them and writes them to a file. \ No newline at end of file diff --git a/episode16/radomizer.py b/episode16/radomizer.py new file mode 100644 index 0000000..d0f7779 --- /dev/null +++ b/episode16/radomizer.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +# Coding 101: Randomizer +# +# Written by Erik C. Thauvin (erik@thauvin.net) +# http://erik.thauvin.net/ +# http://github.com/ethauvin/coding101/ +# May 9, 2014 + +# Import the randrange function +from random import randrange + +# The maximum number of random numbers to generate +max = 100 +# The name of the file to write the number to +fileName = 'random.txt' + +# Initialize the list of random numbers +randomNumbers = [] + +print +print 'Generating ' + str(max) + ' random numbers...' +print + +# Initialize the print counter +printCounter = 0 +# Loop until we have 100 random numbers in our list +while len(randomNumbers) < max: + # Get a random number between 0 and 100 + randint = randrange(max) + # Append the random number to our list + randomNumbers.append(randint) + # Print the random number, converted to a string and right justified + # The comma at the end prevents a new line from being printed + print ' ' + str(randint).rjust(2), + # Increment the print counter, same as: printCounter = printerCounter + 1 + printCounter += 1 + # If the print counter is 10, print a new line, and reset the counter + if printCounter == 10: + print + printCounter = 0 + +print +raw_input('Press ENTER to continue...') + +print +print 'Sorting...' +print + +# Sort the numbers +randomNumbers.sort() + +# Open the file for writing, it will be closed automatically when done +with open(fileName, 'w') as f: + printCounter = 0 + # Loop through our random number list. + for number in randomNumbers: + # Write the number to the file, followed by a new line + f.write(str(number) + '\n') + # Also print the number on screen + print ' ' + str(number).rjust(2), + printCounter += 1 + if printCounter == 10: + print + printCounter = 0 + +print +print 'The numbers have been written to "' + fileName + '"' +print + +raw_input('Press ENTER to quit...') \ No newline at end of file From 1f34d05c32703e8c69aa08bafa6a748953a238b5 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 9 May 2014 11:15:06 -0700 Subject: [PATCH 6/9] mend --- episode16/{radomizer.py => randomizer.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename episode16/{radomizer.py => randomizer.py} (100%) diff --git a/episode16/radomizer.py b/episode16/randomizer.py similarity index 100% rename from episode16/radomizer.py rename to episode16/randomizer.py From 93e5f73021a4794d5c6f18be8cda346bbbe17b71 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 9 May 2014 11:15:06 -0700 Subject: [PATCH 7/9] Example for Episode 16 --- episode16/{radomizer.py => randomizer.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename episode16/{radomizer.py => randomizer.py} (100%) diff --git a/episode16/radomizer.py b/episode16/randomizer.py similarity index 100% rename from episode16/radomizer.py rename to episode16/randomizer.py From 3af7ffc5ef1eea46066e8d489446f8cfd944a38e Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 9 May 2014 11:52:26 -0700 Subject: [PATCH 8/9] Added tutorial references. --- episode16/randomizer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/episode16/randomizer.py b/episode16/randomizer.py index d0f7779..d30f960 100644 --- a/episode16/randomizer.py +++ b/episode16/randomizer.py @@ -8,6 +8,7 @@ # May 9, 2014 # Import the randrange function +# See: http://www.tutorialspoint.com/python/number_randrange.htm from random import randrange # The maximum number of random numbers to generate @@ -31,6 +32,7 @@ while len(randomNumbers) < max: # Append the random number to our list randomNumbers.append(randint) # Print the random number, converted to a string and right justified + # See: http://www.tutorialspoint.com/python/string_rjust.htm # The comma at the end prevents a new line from being printed print ' ' + str(randint).rjust(2), # Increment the print counter, same as: printCounter = printerCounter + 1 From a696a3c077921bc15a796635b256bb248ead709d Mon Sep 17 00:00:00 2001 From: erik Date: Mon, 19 May 2014 02:17:44 -0700 Subject: [PATCH 9/9] Example for Episode 17 --- episode14/coding101.py | 10 +-- episode15/codingYouTube101.py | 28 +++---- episode16/randomizer.py | 30 +++---- episode17/.gitignore | 2 + episode17/README.txt | 3 + episode17/rsvp.py | 142 ++++++++++++++++++++++++++++++++++ episode17/rsvp.txt | 11 +++ 7 files changed, 192 insertions(+), 34 deletions(-) create mode 100644 episode17/.gitignore create mode 100644 episode17/README.txt create mode 100644 episode17/rsvp.py create mode 100644 episode17/rsvp.txt diff --git a/episode14/coding101.py b/episode14/coding101.py index c5a9a5c..1daf0a8 100644 --- a/episode14/coding101.py +++ b/episode14/coding101.py @@ -13,11 +13,11 @@ import textwrap print '================================ CODING 101 ================================' # Open the file -episodeFile = open('coding101.txt', 'r') +episode_file = open('coding101.txt', 'r') # Read the lines into a list -linesFromFile = episodeFile.readlines() +lines_from_files = episode_file.readlines() # Close the file -episodeFile.close() +episode_file.close() # Let's go. The program will execute until run = False run = True @@ -30,7 +30,7 @@ while run: # Loop through the episodes list. count = 0 - for line in linesFromFile: + for line in lines_from_files: # Increment the episode number count += 1 # Split the line using tab as the delimiter @@ -62,7 +62,7 @@ while run: print print '============================================================================' # Get and split the line for the episode, the list is zero-based - episode = linesFromFile[selection - 1].split('\t') + episode = lines_from_files[selection - 1].split('\t') # Print the episode title and aired date print 'Episode #' + choice + ": \"" + episode[1] + '" aired on ' + episode[0] print diff --git a/episode15/codingYouTube101.py b/episode15/codingYouTube101.py index 1572dd5..b2ca367 100644 --- a/episode15/codingYouTube101.py +++ b/episode15/codingYouTube101.py @@ -15,7 +15,7 @@ import urllib2 import json # Your YouTube API key from https://developers.google.com/youtube/registering_an_application -youTubeApiKey = '' +youtube_api_key = '' # Print opening title print '============================= CODING YOUTUBE 101 =============================' @@ -23,19 +23,19 @@ print '============================= CODING YOUTUBE 101 ======================== print # Ask for the API key if none is specified above -while not youTubeApiKey.isalnum(): - youTubeApiKey = raw_input('Enter your YouTube API Key: ') +while not youtube_api_key.isalnum(): + youtube_api_key = raw_input('Enter your YouTube API Key: ') # Did you press enter? - if not youTubeApiKey: + if not youtube_api_key: # Stop the program execution. Bye-Bye! exit(0) # Search for Coding 101 videos using the channel ID and our API key -youTubeResponse = urllib2.urlopen( - 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCSxIcr2rZZcoU7rSGXaEQag&maxResults=20&order=date&key=' + youTubeApiKey) +youtube_response = urllib2.urlopen( + 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCSxIcr2rZZcoU7rSGXaEQag&maxResults=20&order=date&key=' + youtube_api_key) # Load the list of videos returned by YouTube -videos = json.load(youTubeResponse) +videos = json.load(youtube_response) # Declare the episodes list episodes = [] @@ -64,10 +64,10 @@ while run: count = 0 for episode in episodes: # Split the episode info using tab as the delimiter - episodeInfo = episode.split('\t') + episode_info = episode.split('\t') # Print the loop count & title # The count is converted into a str, and right justified - print ' {0}. {1}'.format(str(count).rjust(2), episodeInfo[0]) + print ' {0}. {1}'.format(str(count).rjust(2), episode_info[0]) # Increment the loop count, unless we're on the last episode if (count + 1) < len(episodes): # Same as: count = count + 1 @@ -95,15 +95,15 @@ while run: print print '==============================================================================' # Split the selected episode info using tab as the delimiter - episodeInfo = episodes[selection].split('\t') + episode_info = episodes[selection].split('\t') # Print the episode title - print episodeInfo[0] + print episode_info[0] print # Print the description, using textwrap to make it look pretty - print textwrap.fill(episodeInfo[1], width=76) + print textwrap.fill(episode_info[1], width=76) print # Print the URL - print episodeInfo[2] + print episode_info[2] print '==============================================================================' print - raw_input('Press ENTER to continue...') + raw_input('Press ENTER to continue...') \ No newline at end of file diff --git a/episode16/randomizer.py b/episode16/randomizer.py index d30f960..2133a77 100644 --- a/episode16/randomizer.py +++ b/episode16/randomizer.py @@ -17,30 +17,30 @@ max = 100 fileName = 'random.txt' # Initialize the list of random numbers -randomNumbers = [] +random_numbers = [] print print 'Generating ' + str(max) + ' random numbers...' print # Initialize the print counter -printCounter = 0 +print_counter = 0 # Loop until we have 100 random numbers in our list -while len(randomNumbers) < max: +while len(random_numbers) < max: # Get a random number between 0 and 100 - randint = randrange(max) + rand_int = randrange(max) # Append the random number to our list - randomNumbers.append(randint) + random_numbers.append(rand_int) # Print the random number, converted to a string and right justified # See: http://www.tutorialspoint.com/python/string_rjust.htm # The comma at the end prevents a new line from being printed - print ' ' + str(randint).rjust(2), + print ' ' + str(rand_int).rjust(2), # Increment the print counter, same as: printCounter = printerCounter + 1 - printCounter += 1 + print_counter += 1 # If the print counter is 10, print a new line, and reset the counter - if printCounter == 10: + if print_counter == 10: print - printCounter = 0 + print_counter = 0 print raw_input('Press ENTER to continue...') @@ -50,21 +50,21 @@ print 'Sorting...' print # Sort the numbers -randomNumbers.sort() +random_numbers.sort() # Open the file for writing, it will be closed automatically when done with open(fileName, 'w') as f: - printCounter = 0 + print_counter = 0 # Loop through our random number list. - for number in randomNumbers: + for number in random_numbers: # Write the number to the file, followed by a new line f.write(str(number) + '\n') # Also print the number on screen print ' ' + str(number).rjust(2), - printCounter += 1 - if printCounter == 10: + print_counter += 1 + if print_counter == 10: print - printCounter = 0 + print_counter = 0 print print 'The numbers have been written to "' + fileName + '"' diff --git a/episode17/.gitignore b/episode17/.gitignore new file mode 100644 index 0000000..e49c53b --- /dev/null +++ b/episode17/.gitignore @@ -0,0 +1,2 @@ +.idea +random.txt diff --git a/episode17/README.txt b/episode17/README.txt new file mode 100644 index 0000000..33eb5b0 --- /dev/null +++ b/episode17/README.txt @@ -0,0 +1,3 @@ +Example for TWiT Coding 101 - Episode 17 + +Displays Shannon's wedding RSVP list with options to sort (all, yes, no, not replied) and save. \ No newline at end of file diff --git a/episode17/rsvp.py b/episode17/rsvp.py new file mode 100644 index 0000000..498f716 --- /dev/null +++ b/episode17/rsvp.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python + +# Coding 101: RSVP +# +# Written by Erik C. Thauvin (erik@thauvin.net) +# http://erik.thauvin.net/ +# http://github.com/ethauvin/coding101/ +# May 18, 2014 + +# The name and extension of the file containing the RSVP list +file_name = 'rsvp' +file_ext = '.txt' + +# The RSVP statuses +status_yes = 'Yes' +status_no = 'No' +status_nr = 'Not Replied' + +# Print the banner +print '=================================== RSVP =====================================' + +# Initialize the RSVP list +rsvp_list = [] +# Read the file, line by line +for line in open(file_name + file_ext): + # Append the split line as a tuple to our list + # The file format is: name,status + # e.g.: Shannon Morse,Yes + # See: http://www.tutorialspoint.com/python/string_split.htm + # http://www.tutorialspoint.com/python/string_strip.htm + # http://www.tutorialspoint.com/python/python_tuples.htm + rsvp_list.append(tuple(line.strip().split(',', 1))) + +# Sort the list by names +rsvp_list.sort(key=lambda rsvp: rsvp[0]) + + +# +# Function to save the list based on the specified status +# +def save_list(status): + # Create a new file using the status + # e.g: rsvp-yes.txt, rsvp-no.txt, rsvp-not-replied.txt + # See: http://www.tutorialspoint.com/python/string_replace.htm + # http://www.tutorialspoint.com/python/string_lower.htm + new_file = file_name + '-' + status.replace(' ', '-').lower() + file_ext + # Open the file for writing, it will be closed automatically when done + with open(new_file, 'w') as f: + # Loop through the RSVP list + for rsvp in rsvp_list: + # Determine if the line should be saved, by comparing its status + # The statuses are all lower-cased, so the comparison is case-insensitive + is_save = False + if status.lower() == rsvp[1].lower() == status_yes.lower(): + is_save = True + elif status.lower() == rsvp[1].lower() == status_no.lower(): + is_save = True + elif status.lower() == rsvp[1].lower() == status_nr.lower(): + is_save = True + + if is_save: + # Write the name, followed by a new line + f.write(rsvp[0] + '\n') + print + print 'The list have been written to "' + new_file + '"' + print + + +# +# Function to display the list based on the specified status, if any +# +def display_list(status=None): + # Print the columns header + print + print ' # Name RSVP' + print '==============================================================================' + # The displayed line count + count = 1 + for rsvp in rsvp_list: + # Determine if the line should be displayed by comparing its status + is_print = False + if status is None: + is_print = True + elif status.lower() == rsvp[1].lower() == status_yes.lower(): + is_print = True + elif status.lower() == rsvp[1].lower() == status_no.lower(): + is_print = True + elif status.lower() == rsvp[1].lower() == status_nr.lower(): + is_print = True + + if is_print: + # Print the count, name and status using right and left justification + # See: http://www.tutorialspoint.com/python/string_rjust.htm + # http://www.tutorialspoint.com/python/string_ljust.htm + # http://www.tutorialspoint.com/python/string_title.htm + print '{0}. {1} {2}'.format(str(count).rjust(2), rsvp[0].title().ljust(58), rsvp[1].title().rjust(15)) + # Increment the count, same as: count = count + 1 + count += 1 + print + + # Present the option to save the list to a file, is the status is specified + if status is not None: + save = raw_input('Type \'s\' to save or ENTER to continue: ') + if save == 's': + save_list(status) + else: + raw_input('Press ENTER to continue...') + +# +# Let's go. The program will execute until run = False +# +run = True +while run: + # Print the options + print + print 'Choose an option:' + print + print '\t1. RSVP: All' + print '\t2. RSVP: ' + status_yes + print '\t3. RSVP: ' + status_no + print '\t4. RSVP: ' + status_nr + print + choice = raw_input('Enter option (or ENTER to quit): ') + + # Did you press enter? + if not choice: + # Stop the program execution. Bye-Bye! + run = False + + # Validate the selected option. It must be a number... + elif choice.isdigit(): + # Convert the selected option to an int + selection = int(choice) + # Display the list based on the selected status + if selection == 1: + display_list() + elif selection == 2: + display_list(status_yes) + elif selection == 3: + display_list(status_no) + elif selection == 4: + display_list(status_nr) \ No newline at end of file diff --git a/episode17/rsvp.txt b/episode17/rsvp.txt new file mode 100644 index 0000000..e6edb0a --- /dev/null +++ b/episode17/rsvp.txt @@ -0,0 +1,11 @@ +Shannon Morse,Yes +Keith Coffman,Yes +Aunt Morse,No +Cranky Hippo,Yes +Daddy Morse,Not Replied +Mommy Coffman,Yes +Mommy Morse,Yes +Nana Morse,Yes +Officiant Tom,Yes +Papa Coffman,Yes +Sister Kate,No \ No newline at end of file