Skip to content

Instantly share code, notes, and snippets.

@peterldowns
peterldowns / Coroutines.py
Created December 29, 2011 21:14
Coroutine implementations and wrappers
import multiprocessing
import Queue, threading
import sys, time
class CoroutineProcess(multiprocessing.Process):
""" Will run a coroutine in its own process, using the
multiprocessing library. The coroutine thread runs as
a daemon, and is closed automatically when it is no longer
needed. Because it exposes send and close methods, a CoroutineProcess
wrapped coroutine can be dropped in for a regular coroutine."""
@peterldowns
peterldowns / state_abbrevs.js
Created May 7, 2012 19:04
JS dicts for US state abbreviations
var abbrev_to_name = {'AK': 'Alaska', 'AL' : 'Alabama', 'AR' : 'Arkansas', 'AZ' : 'Arizona', 'CA' : 'California', 'CO' : 'Colorado', 'CT' : 'Connecticut', 'DE' : 'Delaware', 'DC' : 'District of Columbia', 'FL' : 'Florida', 'GA' : 'Georgia', 'HI' : 'Hawaii', 'IA' : 'Iowa', 'ID' : 'Idaho', 'IL' : 'Illinois', 'IN' : 'Indiana', 'KS' : 'Kansas', 'KY' : 'Kentucky', 'LA' : 'Louisiana', 'MA' : 'Massachusetts', 'MD' : 'Maryland', 'ME' : 'Maine', 'MI' : 'Michigan', 'MN' : 'Minnesota', 'MS' : 'Mississippi', 'MO' : 'Missouri', 'MT' : 'Montana', 'NC' : 'North Carolina', 'ND' : 'North Dakota', 'NE' : 'Nebraska', 'NH' : 'New Hampshire', 'NJ' : 'New Jersey', 'NM' : 'New Mexico', 'NV' : 'Nevada', 'NY' : 'New York', 'OH' : 'Ohio', 'OK' : 'Oklahoma', 'OR' : 'Oregon', 'PA' : 'Pennsylvania', 'RI' : 'Rhode Island', 'SC' : 'South Carolina', 'SD' : 'South Dakota', 'TN' : 'Tennessee', 'TX' : 'Texas', 'UT' : 'Utah', 'VA' : 'Virginia', 'VT' : 'Vermont', 'WA' : 'Washington', 'WI' : 'Wisconsin', 'WV' : 'West Virginia', 'WY' : 'Wyoming'};
@peterldowns
peterldowns / p28.py
Created May 13, 2012 20:46
Project Euler Problem #28
spiral_side = 1001
num_corners = spiral_side/2
total = 1
corners = (1, 1, 1, 1)
additions = (2, 4, 6, 8)
for i in xrange(num_corners):
corners = map(sum, zip(corners, additions))
additions = map(lambda x: x+8, additions)
@peterldowns
peterldowns / string_multiply.py
Created November 21, 2012 02:18
Efficient string multiplication in Python
import timeit
test_str = "hello world|"
test_times = 1000
def test_str_add():
result = ""
for i in xrange(test_times-1):
result += test_str
return result
@peterldowns
peterldowns / memo_fib.py
Created January 4, 2013 09:21
Simple memoized fibonacci number generator. Works because Python evaluates default argument variables at the time of definition, not call.
def recursive_fib(n, cache={}):
if n <= 1:
return 1
cache[n] = cache.get(n) or n * recursive_fib(n - 1, cache)
return cache[n]
@peterldowns
peterldowns / style.less
Created February 1, 2013 06:19
Demo of responsive template with mixins.
// Rename:
// .locu-mobile-menu => .mobile-mixin
// .locu-web-menu => .web-mixin
// Then, use the mixins like so
@media screen and (max-width: 480px) {
.mobile-mixin;
}
// Keep this if you want the mobile preview to show your small-screen style
@peterldowns
peterldowns / gmail.py
Created May 14, 2013 04:04
Short function for sending messages using a Gmail account.
def gmail(recipients, subject, message, sender_username, sender_password):
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
if not (isinstance(recipients, list) or isinstance(recipients, tuple)):
recipients = [recipients]
number = int(raw_input('Enter a number between 20 and 0: '))
# Only one of the following statements will print. If a condition
# (like "number < 20") is matched, then only that part of the block
# will run. That's what elif does. Else, if.
if number < 20:
print '%d is less than 20' % number
elif number < 10:
print '%d is less than 10' % number
elif number < 5:
@peterldowns
peterldowns / git_aliases.sh
Created April 14, 2014 06:24
Git Aliases
alias g="git"
alias ga="git add"
alias gb="git branch"
alias gba="git branch -a"
alias gbd="git branch -d"
alias gbdr="git push origin --delete"
alias gc="git commit"
alias gca="git commit -a"
alias gch="git checkout"
alias gl="git log --pretty=format:'%Cred%h%Creset %s %Cgreen(%cr)%Creset %Cblue[%an]%Creset' --date=relative"
@peterldowns
peterldowns / mock_object.py
Created July 9, 2014 01:01
Simple way of creating Mock objects from nested dictionaries.
import mock
def mock_object(data=None):
result = mock.MagicMock()
for key, value in data.iteritems():
if isinstance(value, dict):
result.configure_mock(**{
key: mock_object(value),
})
else:
result.configure_mock(**{