Skip to content

Instantly share code, notes, and snippets.

@AO8
AO8 / syllable_counter.py
Last active January 10, 2019 18:26
Use Python 3, NLTK, and the Carnegie Mellon University Pronouncing Dictionary to count the syllables in an English word or phrase.
# NLTK is a suite of libraries for working with human language data: http://www.nltk.org
# NLTK allows us to access the Carnegie Mellon University Prounouncing Dictionary (cmudict)
# cmudict is a corpus that contains almost 125,000 words mapped to their pronunciations.
# This tiny app was inspired by Lee Vaughn's Impractical Python Projects
import sys
from string import punctuation
from nltk.corpus import cmudict
# load corpus and build dictionary
@AO8
AO8 / journal.py
Last active January 11, 2019 22:27
More File I/O practice: a simple MyJournal app with Python 3, taken from Michael Kennedy's Python Jumpstart by Building 10 Apps course.
import os
def load(name):
"""
This method creates and loads a new journal.
:param name: This base name of the journal to load.
:return: A new journal data structure populated with file data.
"""
data = []
filename = get_full_pathname(name)
@AO8
AO8 / deque.py
Last active December 18, 2018 17:19
Deque practice with Python 3.
# Exercise completed as part of Erin Allard's Lynda.com course,
# 'Python Data Structures: Stacks, Queues, and Deques'
class Deque:
"""An ADT that resembles both a Stack (LIFO) and a Queue (FIFO).
Items in a deque can be added to and removed from both the back
and front.
Don't reinvent the wheel. More simply, from collections import deque
https://docs.python.org/3/library/collections.html#collections.deque
@AO8
AO8 / queue.py
Created December 18, 2018 16:53
Queue practice with Python 3.
# Exercise completed as part of Erin Allard's Lynda.com course,
# 'Python Data Structures: Stacks, Queues, and Deques'
class Queue:
"""An ADT that stores items in the order in which they
were added. Items are added to the back and removed
from the front - FIFO.
"""
def __init__(self):
self.items = []
@AO8
AO8 / stack.py
Last active December 18, 2018 16:37
Stack practice with Python 3.
# Exercise completed as part of Erin Allard's Lynda.com course,
# 'Python Data Structures: Stacks, Queues, and Deques'
class Stack:
"""An ADT that stores items in the order in which they were
added. Items are added to and removed from the top of the
stack - LIFO.
"""
def __init__(self):
self.items = []
@AO8
AO8 / get_weather.py
Last active December 14, 2018 21:57
Retrieve temperature and weather conditions with this simple Python weather client using Weather Underground, Requests, and BeautifulSoup.
# Screen scraping information from Weather Underground at https://www.wunderground.com/ is for
# example purposes only. Try the Weather Underground API at https://www.wunderground.com/weather/api
# Project adapted from Michael Kennedy's Python Jumpstart by Building 10 Apps course
# Standard Library
from collections import namedtuple
# Third Party
import requests
from bs4 import BeautifulSoup
@AO8
AO8 / send_gmail.py
Created December 6, 2018 23:04
Sending gmail with Python. This version uses the ssl module and SMTP_SSL() from the smtplib module.
import smtplib
import ssl
from email.mime.text import MIMEText
sender = "[email protected]"
receiver = "[email protected]"
password = input("Enter password: ")
text = """\
Hi!
@AO8
AO8 / get_chuck_norris_joke.py
Last active November 29, 2018 20:00
Fetch a random joke from the Internet Chuck Norris Database (ICNDb) with Python 3 and Requests.
# Learn more about the ICNDb API at: http://www.icndb.com/api/
# Learn more about the Requests library at: http://docs.python-requests.org/en/master/
import requests
def get_joke():
"""fetches and prints a random joke"""
url = "http://api.icndb.com/jokes/random"
resp = requests.get(url)
resp.encoding = "utf-8"
@AO8
AO8 / secret_santa.py
Last active November 20, 2018 22:24
Remove the hassle from making Secret Santa gift giving assignments with Python 3. This short program uses the csv, random, and smtplib modules to read a file, create random assignments, then prepare & send email notifications to each participant.
# To start, set up a Google form that collects
# first name, last name, and email address from participants,
# then export as a CSV file and store it in the same
# directory as this program
# also, enable less secure apps in gmail before running
# https://support.google.com/accounts/answer/6010255?hl=en
import csv
import smtplib
from random import shuffle
@AO8
AO8 / stopwatch.py
Created October 9, 2018 16:12
Handy Python stopwatch recipe for recording the time it takes to perform a task.
# Stopwatch recipe taken from Python Cookbook, 3rd Edition, by David Beazley and Brian K. Jones.
# Page 561: "As a general rule of thumb, the accuracy of timing measurements made with functions
# such as time.time() or time.clock() varies according to the operation system. In contast,
# time.perf_counter() always uses the highest-resolution timer available on the system."
import time
class Timer:
def __init__(self, func=time.perf_counter):
self.elapsed = 0.0