Skip to content

Instantly share code, notes, and snippets.

@AO8
AO8 / password.py
Created October 17, 2017 16:05
Python function that creates an alphanumeric password using the random and string modules.
import random
import string
def create_password():
"""Creates a random 25-character alphanumeric password"""
chars = string.ascii_letters + string.digits
return "".join(random.choice(chars) for i in range(25))
print("Your new password is:", create_password())
@AO8
AO8 / tweeting_watchdog.py
Last active October 17, 2017 17:48
Turn your Raspberry Pi into a tweeting watchdog. Project uses a Pi, camera board, and the Twython package to post a timestamped photo to Twitter every 60 seconds.
# Get started with the Twitter API at https://projects.raspberrypi.org/en/projects/getting-started-with-the-twitter-api
# Get started with PiCamera at https://projects.raspberrypi.org/en/projects/getting-started-with-picamera
# Twitter updates (API, updates, following) at https://support.twitter.com/articles/15364
# Recommend setting up a separate private Twitter account so that you don't inundate followers with photos
from picamera import PiCamera, Color
from time import sleep
from datetime import datetime
from twython import Twython
from twitter_auth import (
@AO8
AO8 / shakespeare_insults.csv
Last active December 6, 2017 17:03
"You sir, are a bawdy, milk-livered canker-blossom!" Randomly create Shakespearean insults just like this with this simple insult generator using Python and Tkinter. Corresponding CSV file included, modified from PDF at https://www.theatrefolk.com/free-resources.
artless base-court apple-john
bawdy bat-fowling baggage
beslubbering beef-witted barnacle
bootless beetle-headed bladder
churlish boil-brained boar-pig
cockered clapper-clawed bugbear
clouted clay-brained bum-bailey
craven common-kissing canker-blossom
currish crook-pated clack-dish
dankish dismal-dreaming clotpole
@AO8
AO8 / stack_reverse.py
Last active January 8, 2018 19:26
Use a stack to reverse a list in Python.
# with inspiration from chapter 21 of The Self-Taught Programmer by Corey Althoff
class Stack:
"""a stack is a LIFO data structure where the last item put in is the first item taken out"""
def __init__(self):
self.items = []
def push(self, item):
"""adds a new item to the stack"""
return self.items.append(item)
@AO8
AO8 / ninety_nine_bottles.py
Last active January 9, 2018 19:58
99 bottles of beer on the wall with Python.
def ninety_nine_bottles():
for n in range(99, 0, -1):
b = "bottles" if n > 1 else "bottle"
verse = f"{n} {b} of beer on the wall, \
\n{n} {b} of beer, \
\nYou take one down and pass it around,"
if n > 2:
@AO8
AO8 / ninety_nine_bottles_2.py
Created January 12, 2018 00:24
Concise version of 99 bottles of beer on the wall with Python.
# just for fun
# correct plural and singular usage of 'bottle' throughout
for n in range(99,0,-1):
b = 'bottles' if n > 1 else b[:-1]
h = 'of beer'
w = 'on the wall'
v = f'{n} {b} {h} {w}, {n} {b} {h}, take one down, pass it around,'
@AO8
AO8 / dow_tracker.py
Last active January 16, 2018 22:59
Simple Dow Jones tracker illustrating how to scrape a website and write the result to a txt file using Python 3 and BeautifulSoup.
# By AO8
# January 13, 2018
import urllib.request
import re
from bs4 import BeautifulSoup
from datetime import datetime as dt
dow_jones_page = "https://www.bloomberg.com/quote/INDU:IND"
html = urllib.request.urlopen(dow_jones_page)
@AO8
AO8 / dow_jones_csv_tracker.py
Last active January 16, 2018 23:00
Another simple Dow Jones tracker that scrapes a website and writes data to a CSV file using Python 3 and Beautiful Soup.
import urllib.request
import csv
import re
from bs4 import BeautifulSoup
from datetime import datetime as dt
dow_jones_page = "https://www.bloomberg.com/quote/INDU:IND"
html = urllib.request.urlopen(dow_jones_page)
soup = BeautifulSoup(html, "html.parser")
@AO8
AO8 / stack_reverse_2.py
Last active January 17, 2018 19:32
Use a stack to reverse a list of orders in Python.
class Stack:
"""a stack is a LIFO data structure where the last item put in is the first item taken out"""
def __init__(self):
self.items = []
def push(self, item):
"""adds a new item to the stack"""
return self.items.append(item)
def pop(self):
@AO8
AO8 / reverse_string.py
Last active January 21, 2018 22:43
Five ways to reverse a string in Python
# 1. Use a simple print command
print("python"[::-1])
# 2. Use join() and reversed()
print("".join(reversed("python")))
# 3. Use a function, for loop, join(), and reversed()
def reverse_string(string):
st = []
for i in string: