Skip to content

Instantly share code, notes, and snippets.

@olooney
olooney / extract_urls_from_page.py
Created November 28, 2012 05:17
A simple wrapper around BeautifulSoup to quickly find the URLs of all resources referenced by an HTML page.
'''A simple wrapper around BeautifulSoup to quickly find the URLs of all resources referenced by an HTML page.'''
import urllib2
import urlparse
import BeautifulSoup
def make_soup(url):
page = urllib2.urlopen(url)
contents = page.read()
soup = BeautifulSoup.BeautifulSoup(contents)
import poplib, email, getpass
M = poplib.POP3_SSL('pop.gmail.com', 995)
username = raw_input("gmail username: ")
M.user(username)
M.pass_(getpass.getpass('%s@gmail.com password: ' % username))
status, emails, octets = M.list()
if status.startswith('+OK'):
print 'new emails:', len(emails)
"""Solves the 8-queens problem (and its N-queens generalization.)"""
# It is clear that since no two queens can occupy the same column, and there are
# as many columns as queens to place, then each column must contain one and only one
# queen. The only question is, on which row to place each queen?
# A "left-board" is a way to represent a partial solution to the n-queens problem.
# It is simply a list of integers, each indicating the row occupied by the queen
# in each column. Because it is a partial solution, it can have fewer than 8 elements,
# or even be empty. For example, [0, 2] indicates a that the queen in the left-most
def collapse_intervals(intervals):
'''intervals is an iterable of 2-tuples representing the start and end of
closed intervals. returns a list of 2-tuples after collapsing any
overlapping intervals. For example:
collapse_intervals([(1,5), (9,10), (4,7)]) -> [(1,7), (9,10)]
The algorithm doesn't care if intervals are out of order, or if two points
of the same interval are out of order. It also doesn't require the points
@olooney
olooney / gist:5231480
Created March 24, 2013 11:19
ssh-copy-id lite
cat ~/.ssh/id_rsa.pub | ssh $USER@$SERVER "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys"
# http://en.wikipedia.org/wiki/RSA_(algorithm)
p = 61
q = 53
n = p*q
totient = (p-1)*(q-1)
# here, e is 2^4 + 1; in real world applications, e is often 2^16 + 1
e = 17
"""
Example using Python's itertools and generators to implement the classic FizzBuzz problem using pythonic iterators.
Output looks like this:
$ python fizz_buzz_with_iterator.py
1
2
Fizz
@olooney
olooney / gist.py
Created December 28, 2013 02:27
simple gist API wrapper with requests module
#! /usr/bin/env python
"""
Simple wrapper around Github's gist HTTP API.
"""
import argparse
import logging
import requests
import json
from getpass import getuser, getpass
@olooney
olooney / .gitignore
Last active September 14, 2023 09:36
worked examples of argparse and python logging
logs/
arima.simulation <- function(linear=1, exponential=0, seasonal=0, season=25, random=1, ahead=10) {
sim.data <- (1:100 * linear/100) + exponential*exp( (1:100/100))/exp(1) + seasonal*sin(1:100 * 2 * pi / season ) + random*rnorm(100);
sims <- ts(sim.data, frequency=25)
sim.model <- auto.arima(sims);
sim.past.future <- c(as.vector(fitted(sim.model)),as.vector(forecast(sim.model, h=ahead)$mean));
ylimits = c( min(sim.past.future), max(sim.past.future))
plot(sim.data, pch=20, col=rgb(0,0,1, alpha=0.5), ylim=ylimits, xlim=c(0,length(sim.past.future)));
lines(seq_along(sim.past.future), sim.past.future, col=rgb(0,1,0, alpha=0.75));
return(sim.model);