From a696a3c077921bc15a796635b256bb248ead709d Mon Sep 17 00:00:00 2001 From: erik Date: Mon, 19 May 2014 02:17:44 -0700 Subject: [PATCH] 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