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..1daf0a8 100644 --- a/episode14/coding101.py +++ b/episode14/coding101.py @@ -10,14 +10,14 @@ import textwrap # Print opening title -print "================================ CODING 101 ================================" +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 @@ -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. + for line in lines_from_files: + # 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. - episode = linesFromFile[selection - 1].split('\t') - # Print the episode, title - print "Episode #" + choice + ": \"" + episode[1] + '" aired on ' + episode[0] + print '============================================================================' + # Get and split the line for the episode, the list is zero-based + 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 - # 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..485dee6 --- /dev/null +++ b/episode15/.gitignore @@ -0,0 +1 @@ +.idea 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..b2ca367 --- /dev/null +++ b/episode15/codingYouTube101.py @@ -0,0 +1,109 @@ +#!/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 +youtube_api_key = '' + +# Print opening title +print '============================= CODING YOUTUBE 101 =============================' +# Print an empty line +print + +# Ask for the API key if none is specified above +while not youtube_api_key.isalnum(): + youtube_api_key = raw_input('Enter your YouTube API Key: ') + # Did you press enter? + 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 +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(youtube_response) + +# 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']) + +# Reverse sort the episodes list +episodes.reverse() + +# 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: + # Split the episode info using tab as the delimiter + 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), episode_info[0]) + # Increment the loop count, unless we're on the last episode + if (count + 1) < len(episodes): + # Same as: count = count + 1 + count += 1 + + print + + # Ask for the episode number + choice = raw_input('Enter 0-' + 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 0 and the total number of episodes + # 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 + episode_info = episodes[selection].split('\t') + # Print the episode title + print episode_info[0] + print + # Print the description, using textwrap to make it look pretty + print textwrap.fill(episode_info[1], width=76) + print + # Print the URL + print episode_info[2] + print '==============================================================================' + print + raw_input('Press ENTER to continue...') \ No newline at end of file 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/randomizer.py b/episode16/randomizer.py new file mode 100644 index 0000000..2133a77 --- /dev/null +++ b/episode16/randomizer.py @@ -0,0 +1,73 @@ +#!/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 +# See: http://www.tutorialspoint.com/python/number_randrange.htm +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 +random_numbers = [] + +print +print 'Generating ' + str(max) + ' random numbers...' +print + +# Initialize the print counter +print_counter = 0 +# Loop until we have 100 random numbers in our list +while len(random_numbers) < max: + # Get a random number between 0 and 100 + rand_int = randrange(max) + # Append the random number to our list + 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(rand_int).rjust(2), + # Increment the print counter, same as: printCounter = printerCounter + 1 + print_counter += 1 + # If the print counter is 10, print a new line, and reset the counter + if print_counter == 10: + print + print_counter = 0 + +print +raw_input('Press ENTER to continue...') + +print +print 'Sorting...' +print + +# Sort the numbers +random_numbers.sort() + +# Open the file for writing, it will be closed automatically when done +with open(fileName, 'w') as f: + print_counter = 0 + # Loop through our random number list. + 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), + print_counter += 1 + if print_counter == 10: + print + print_counter = 0 + +print +print 'The numbers have been written to "' + fileName + '"' +print + +raw_input('Press ENTER to quit...') \ No newline at end of file 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