Skip to content

Instantly share code, notes, and snippets.

@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 = "sender@gmail.com"
receiver = "recipient@domain.com"
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
@AO8
AO8 / month_analyzer3.py
Last active September 26, 2018 17:11
Another approach to an ongoing Python project: reading CSV exported from Calendly to analyze what months students book an advising appointment. Key modules used in this approach include OrderedDict, datetime, and matplotlib.
import csv
from datetime import datetime
from collections import OrderedDict
import matplotlib.pyplot as plt
# set up empty containers
total = 0
months = dict()
# open and read CSV
@AO8
AO8 / cohort_analyzer.py
Last active October 9, 2018 19:58
Using Python to analyze the makeup of a given BAS-SD cohort.
import csv
from statistics import mean
# set up empty dict containers
associate_type = dict() # row 6
associate_from = dict() # row 7
maths = dict() # row 14
advisors = dict() # row 15
# set up empty list containers
@AO8
AO8 / record_video.py
Created September 17, 2018 00:21
Simple Python recipe for recording video with timestamp using PiCamera + Raspberry Pi.
from picamera import PiCamera, Color
from time import sleep
from datetime import datetime as dt
with PiCamera() as camera:
camera.rotation = 180 # omit or use 90, 180, 270 depending on setup
camera.annotate_background = Color("black")
start = dt.now()
camera.start_preview()