Skip to content

Instantly share code, notes, and snippets.

View baileywickham's full-sized avatar
👓

Bailey Wickham baileywickham

👓
View GitHub Profile
def get_data(func, message):
while True:
try:
tmp = input(message)
tmp = func(tmp)
except:
print(f"Input not of type: {func.__name__}")
else:
return tmp
@baileywickham
baileywickham / metaclasses.py
Last active September 23, 2020 04:58
Metaclasses in python
# Create classes based on csv file data
with open("file.csv", 'r') as f:
header = f.readline().split(',')
metaclass = type('csv', (object,), {h:None for h in header})
for line in f.readlines():
m = metaclass()
line = line.split(',')
for i in range(len(header)):
setattr(m, header[i], line[i])
yield m # Return as generator
@baileywickham
baileywickham / Dockerfile
Created June 20, 2020 19:47
Docker for ssh
# Stolen from the docker website
FROM ubuntu
RUN apt-get update && apt-get install -qq openssh-server python
RUN mkdir /var/run/sshd
RUN echo 'root:testpass' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
select dayofweek(checkin), count(code), sum(datediff(checkout, checkin)*rate)
from reservations
join rooms on rooms.roomcode=reservations.room
group by dayofweek(checkin)
order by dayofweek(checkin)
@baileywickham
baileywickham / Dockerfile
Last active September 29, 2020 05:25
A Dockerfile for PEP622, pattern matching.
# A Dockerfile for PEP622, pattern matching.
# https://www.python.org/dev/peps/pep-0622/
FROM alpine:3.12
# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH
# http://bugs.python.org/issue19846
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
ENV LANG C.UTF-8
@baileywickham
baileywickham / Maybe.py
Created November 3, 2020 03:38
Maybe monad in python
from operator import neg
class Maybe(object):
def __init__(self, v, failed=False):
self.v = v
self.failed = failed
def bind(self, f):
if self.failed:
return self
try:
@baileywickham
baileywickham / romans.py
Created November 4, 2020 06:36
Roman numeral converter
rmn = {1000 : 'M',
900 : 'CM',
500 : 'D',
400 : 'CD',
100 : 'C',
90 : 'XC',
50 : 'L',
40 : 'XL',
10 : 'X',
9 : 'IX',
import sys
def sieve(N):
P = [1 if x % 2 == 1 or x == 2 else 0 for x in range(N+1)]
m = 3
n = m**2
while n <= N:
if P[m] == 1:
while n <= N:
P[n] = 0
# Fixed Point Map:
# fpmap(function, fixed_point..., list)
def fpmap(*argv):
if len(argv) < 2:
# bad map paramaters
raise TypeError("fpmap must have at least two arguments")
# function to call
f = argv[0]
# List to pass to map
@baileywickham
baileywickham / sitemap.py
Last active May 6, 2021 05:59
wasup kelso
# https://architizer.com/sitemap-firms.xml
# Step 1: Download the above link to the file 'sitemap-firms.xml' and put it in the same directory as the python script.
# Step 2: Run the python script. It's sorta hacked together so it only half works, but it should do the basics. You can filter
# the list more if you want.
import xml.etree.ElementTree as ET
import requests
import re
email_regex = 'mailto:\S+@\S+\.\S+'