Example for Episode 17
This commit is contained in:
parent
3af7ffc5ef
commit
a696a3c077
7 changed files with 192 additions and 34 deletions
|
@ -13,11 +13,11 @@ import textwrap
|
||||||
print '================================ CODING 101 ================================'
|
print '================================ CODING 101 ================================'
|
||||||
|
|
||||||
# Open the file
|
# Open the file
|
||||||
episodeFile = open('coding101.txt', 'r')
|
episode_file = open('coding101.txt', 'r')
|
||||||
# Read the lines into a list
|
# Read the lines into a list
|
||||||
linesFromFile = episodeFile.readlines()
|
lines_from_files = episode_file.readlines()
|
||||||
# Close the file
|
# Close the file
|
||||||
episodeFile.close()
|
episode_file.close()
|
||||||
|
|
||||||
# Let's go. The program will execute until run = False
|
# Let's go. The program will execute until run = False
|
||||||
run = True
|
run = True
|
||||||
|
@ -30,7 +30,7 @@ while run:
|
||||||
|
|
||||||
# Loop through the episodes list.
|
# Loop through the episodes list.
|
||||||
count = 0
|
count = 0
|
||||||
for line in linesFromFile:
|
for line in lines_from_files:
|
||||||
# Increment the episode number
|
# Increment the episode number
|
||||||
count += 1
|
count += 1
|
||||||
# Split the line using tab as the delimiter
|
# Split the line using tab as the delimiter
|
||||||
|
@ -62,7 +62,7 @@ while run:
|
||||||
print
|
print
|
||||||
print '============================================================================'
|
print '============================================================================'
|
||||||
# Get and split the line for the episode, the list is zero-based
|
# 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 the episode title and aired date
|
||||||
print 'Episode #' + choice + ": \"" + episode[1] + '" aired on ' + episode[0]
|
print 'Episode #' + choice + ": \"" + episode[1] + '" aired on ' + episode[0]
|
||||||
print
|
print
|
||||||
|
|
|
@ -15,7 +15,7 @@ import urllib2
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# Your YouTube API key from https://developers.google.com/youtube/registering_an_application
|
# Your YouTube API key from https://developers.google.com/youtube/registering_an_application
|
||||||
youTubeApiKey = ''
|
youtube_api_key = ''
|
||||||
|
|
||||||
# Print opening title
|
# Print opening title
|
||||||
print '============================= CODING YOUTUBE 101 ============================='
|
print '============================= CODING YOUTUBE 101 ============================='
|
||||||
|
@ -23,19 +23,19 @@ print '============================= CODING YOUTUBE 101 ========================
|
||||||
print
|
print
|
||||||
|
|
||||||
# Ask for the API key if none is specified above
|
# Ask for the API key if none is specified above
|
||||||
while not youTubeApiKey.isalnum():
|
while not youtube_api_key.isalnum():
|
||||||
youTubeApiKey = raw_input('Enter your YouTube API Key: ')
|
youtube_api_key = raw_input('Enter your YouTube API Key: ')
|
||||||
# Did you press enter?
|
# Did you press enter?
|
||||||
if not youTubeApiKey:
|
if not youtube_api_key:
|
||||||
# Stop the program execution. Bye-Bye!
|
# Stop the program execution. Bye-Bye!
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
# Search for Coding 101 videos using the channel ID and our API key
|
# Search for Coding 101 videos using the channel ID and our API key
|
||||||
youTubeResponse = urllib2.urlopen(
|
youtube_response = urllib2.urlopen(
|
||||||
'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCSxIcr2rZZcoU7rSGXaEQag&maxResults=20&order=date&key=' + youTubeApiKey)
|
'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
|
# Load the list of videos returned by YouTube
|
||||||
videos = json.load(youTubeResponse)
|
videos = json.load(youtube_response)
|
||||||
|
|
||||||
# Declare the episodes list
|
# Declare the episodes list
|
||||||
episodes = []
|
episodes = []
|
||||||
|
@ -64,10 +64,10 @@ while run:
|
||||||
count = 0
|
count = 0
|
||||||
for episode in episodes:
|
for episode in episodes:
|
||||||
# Split the episode info using tab as the delimiter
|
# Split the episode info using tab as the delimiter
|
||||||
episodeInfo = episode.split('\t')
|
episode_info = episode.split('\t')
|
||||||
# Print the loop count & title
|
# Print the loop count & title
|
||||||
# The count is converted into a str, and right justified
|
# 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
|
# Increment the loop count, unless we're on the last episode
|
||||||
if (count + 1) < len(episodes):
|
if (count + 1) < len(episodes):
|
||||||
# Same as: count = count + 1
|
# Same as: count = count + 1
|
||||||
|
@ -95,15 +95,15 @@ while run:
|
||||||
print
|
print
|
||||||
print '=============================================================================='
|
print '=============================================================================='
|
||||||
# Split the selected episode info using tab as the delimiter
|
# 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 the episode title
|
||||||
print episodeInfo[0]
|
print episode_info[0]
|
||||||
print
|
print
|
||||||
# Print the description, using textwrap to make it look pretty
|
# 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
|
||||||
# Print the URL
|
# Print the URL
|
||||||
print episodeInfo[2]
|
print episode_info[2]
|
||||||
print '=============================================================================='
|
print '=============================================================================='
|
||||||
print
|
print
|
||||||
raw_input('Press ENTER to continue...')
|
raw_input('Press ENTER to continue...')
|
|
@ -17,30 +17,30 @@ max = 100
|
||||||
fileName = 'random.txt'
|
fileName = 'random.txt'
|
||||||
|
|
||||||
# Initialize the list of random numbers
|
# Initialize the list of random numbers
|
||||||
randomNumbers = []
|
random_numbers = []
|
||||||
|
|
||||||
print
|
print
|
||||||
print 'Generating ' + str(max) + ' random numbers...'
|
print 'Generating ' + str(max) + ' random numbers...'
|
||||||
print
|
print
|
||||||
|
|
||||||
# Initialize the print counter
|
# Initialize the print counter
|
||||||
printCounter = 0
|
print_counter = 0
|
||||||
# Loop until we have 100 random numbers in our list
|
# 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
|
# Get a random number between 0 and 100
|
||||||
randint = randrange(max)
|
rand_int = randrange(max)
|
||||||
# Append the random number to our list
|
# 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
|
# Print the random number, converted to a string and right justified
|
||||||
# See: http://www.tutorialspoint.com/python/string_rjust.htm
|
# See: http://www.tutorialspoint.com/python/string_rjust.htm
|
||||||
# The comma at the end prevents a new line from being printed
|
# 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
|
# 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 the print counter is 10, print a new line, and reset the counter
|
||||||
if printCounter == 10:
|
if print_counter == 10:
|
||||||
print
|
print
|
||||||
printCounter = 0
|
print_counter = 0
|
||||||
|
|
||||||
print
|
print
|
||||||
raw_input('Press ENTER to continue...')
|
raw_input('Press ENTER to continue...')
|
||||||
|
@ -50,21 +50,21 @@ print 'Sorting...'
|
||||||
print
|
print
|
||||||
|
|
||||||
# Sort the numbers
|
# Sort the numbers
|
||||||
randomNumbers.sort()
|
random_numbers.sort()
|
||||||
|
|
||||||
# Open the file for writing, it will be closed automatically when done
|
# Open the file for writing, it will be closed automatically when done
|
||||||
with open(fileName, 'w') as f:
|
with open(fileName, 'w') as f:
|
||||||
printCounter = 0
|
print_counter = 0
|
||||||
# Loop through our random number list.
|
# 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
|
# Write the number to the file, followed by a new line
|
||||||
f.write(str(number) + '\n')
|
f.write(str(number) + '\n')
|
||||||
# Also print the number on screen
|
# Also print the number on screen
|
||||||
print ' ' + str(number).rjust(2),
|
print ' ' + str(number).rjust(2),
|
||||||
printCounter += 1
|
print_counter += 1
|
||||||
if printCounter == 10:
|
if print_counter == 10:
|
||||||
print
|
print
|
||||||
printCounter = 0
|
print_counter = 0
|
||||||
|
|
||||||
print
|
print
|
||||||
print 'The numbers have been written to "' + fileName + '"'
|
print 'The numbers have been written to "' + fileName + '"'
|
||||||
|
|
2
episode17/.gitignore
vendored
Normal file
2
episode17/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.idea
|
||||||
|
random.txt
|
3
episode17/README.txt
Normal file
3
episode17/README.txt
Normal file
|
@ -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.
|
142
episode17/rsvp.py
Normal file
142
episode17/rsvp.py
Normal file
|
@ -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)
|
11
episode17/rsvp.txt
Normal file
11
episode17/rsvp.txt
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue