My Experience with Automate The Boring Stuff
For a while now I’ve wanted to gain a better understanding of programing. I really think it will be useful to have a basic understand of how the programs that I work with everyday function. I decided that learning python would be a good place to start since it is beginner friendly and widely used. Hearing good thigs about Automate The Boring Stuff by Al Sweigart I picked up a copy. I’ve taken a few computer science class in college but really don’t have much experience.
Overall I found the book to be a good introduction for people with little to no background in programming. The author really focuses on getting you writing code as quickly as possible and creating scripts that he thinks will be useful to most people in an office setting. However, as a consequence of this the book is really light on programming concepts like functions and object oriented design. Once you learn about basic structures like strings, list, and dictionaries, the second half of the book is really just about learning new python packages. Which for me lost my interest. Also, the final chapter has you working on a bigger program that automatically plays an online flash game. This portion of the book seemed kind of outdated. I ended up trying it for a bit then skipping it once I realized it was actually going to be a lot of effort to get working. Not because the logic was difficult, but because I was going to have to spend hours studying the html code to figure out which lines corresponded with which command.
Now that I have pretty much completed this book the plan is to move on to a new python book which is more than likely going to be Learning Python 3 The Hardway by Zed Shaw.
Below you’ll find some code snippets from some of the projects that I found more interesting along with a short explanation.
Tick Tac Toe: Chapter 05
In this project you use all the basic structures you learn like arrays, strings, and loops to create a basic tic-tac-toe game in the console
#global dictionary variable
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' ',
}
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
#Sets the game to start with 'X' playing first
turn = 'X'
#
for i in range(9):
#prints the board's current state
printBoard(theBoard)
print('Turn for ' + turn + '. Move on which space?')
move = input()
#checks that user entered a valid input
while move not in theBoard.keys():
print('Invalid play try again.')
print('Turn for ' + turn + '. Move on which space?')
move = input()
#checks that the player is not attempting to play a space already taken
while theBoard[move] == 'X' or theBoard[move] == 'O':
print('Sorry that space is taken try again.')
print('Turn for ' + turn + '. Move on which space?')
move = input()
theBoard[move] = turn
if turn == 'X':
turn = 'O'
else:
turn = 'X'
printBoard(theBoard)
print('game over.')
Madlibs with Regex: Chapter 08
This chapter and the project were all about regular expressions, which are super useful for parsing text. In this program you parse a text file that contains a mad lib to find where it calls for an adjective, noun, or verb and then ask the user to input the correct word type.
import os,re
'--------------------------------------------------------------------------------------------------'
textFile = open('C:\\AutomateBoringStuff\\Chapter_08\\mdLibsText.txt')
textContent = textFile.read()
print(textContent)
'--------------------------------------------------------------------------------------------------'
'--------------------------------------------------------------------------------------------------'
checkRegex = re.compile(r'ADJECTIVE|NOUN|VERB')
'--------------------------------------------------------------------------------------------------'
while True:
result = checkRegex.search(textContent)
if result == None:
break
if result.group() == 'ADJECTIVE':
print('Enter an Adjective:')
adjectiveSelection = input()
textContent = checkRegex.sub(adjectiveSelection, textContent, count = 1)
if result.group() == 'NOUN':
print('Enter a Noun.')
nounSelection = input()
textContent = checkRegex.sub(nounSelection, textContent, count = 1)
if result.group() == 'VERB':
print('Enter a Verb.')
verbSelection = input()
textContent = checkRegex.sub(verbSelection, textContent, count = 1)
'--------------------------------------------------------------------------------------------------'
print(textContent)
mlTextResult = open('C:\\AutomateBoringStuff\\Chapter_08\\mlTextResult.txt', 'w')
mlTextResult.write(textContent)
mlTextResult.close()
Printing Weather Information Using Website API: Chapter 14
This one was a cooler project. You learn about APIs. You retrieve weather information using the US governments weather APIs and create a program that tells the user about weather in their area.
#! python3
# quickWeather.py - Prints the weather for a location from the command line.
import json, requests, sys, pprint
APPID = 'fa9663719aafd64bc648ad0b23f671d6'
#Compute location from command line arguments.
if len(sys.argv) > 1:
if len(sys.argv) < 2:
print('Usage: quickWeather.py location')
sys.exit()
location = ''.joing(sys.argv[1:])
else:
location = 'san francisco'
# ToDo: Download the JSON data from OenWeatherMap.org's API
url ='http://api.openweathermap.org/data/2.5/forecast?q=%s&cnt=3&APPID=%s' % (location, APPID)
response = requests.get(url)
response.raise_for_status()
#pprint.pprint(response.text)
# ToDo: Load JSON data into a Python variable.
weatherData = json.loads(response.text)
#Print weather descriptions
w =weatherData['list']
print('Current weather in %s:' % (location))
print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description'])
print()
print('Tomorrow:')
print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description'])
print()
print('Day After Tomorrow:')
print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])
Stock Market Info Program
Finally, using what the book taught about parsing html, converting data to csv files, and spreadsheets, I made a script that gathers data from the internet about Nasdaq 100 stocks and creates a spreadsheet showing their performance. You can check that one out on my GitHub, but below you can see the spreadsheet that the program generates
GitHub: https://github.com/zackthomas1/Nasdaq100/blob/master/Nasdaq100DataDownloader.py