Skip to content

Instantly share code, notes, and snippets.

@showell
showell / compose_funcs.py
Created November 22, 2012 20:40
silly way to compose functions in Python
def sumxyz(x):
def sumyz(y):
def sumz(z):
return x + y + z
return sumz
return sumyz
print sumxyz(5)(4)(3)
@showell
showell / file_system.py
Created November 11, 2012 02:38
python: file system abstracted as dictionary
import glob
import os
class Directory:
def __init__(self, path = '/'):
self.path = path
def __getitem__(self, key):
fn = os.path.join(self.path, key)
if os.path.exists(fn):
@showell
showell / python_seq.html
Created November 10, 2012 21:34
Python Sequences Chart
<table border=1 cellpadding=5>
<tr>
<th>&nbsp;</th>
<th>Lists</th>
<th>Iterators</th>
</tr>
<tr>
<th>Expressive</th>
<td>
@showell
showell / basketball.py
Created October 17, 2012 05:36
procedural/functional python
def num_ways_to_score(target_score, scores):
ways_to_score = 0
counts = [0] * len(scores)
while True:
score = sum([scores[i] * count for i, count in enumerate(counts)])
if score == target_score:
ways_to_score += 1
print counts
i = 0
if score > target_score:
@showell
showell / xunit.py
Created October 10, 2012 04:20
xunit is cruft in python
TESTS = []
def suite():
for test in TESTS:
print 'running %s' % test.__name__
test()
def test(f):
TESTS.append(f)
return f
@showell
showell / class_without_class.py
Created September 14, 2012 22:32
silly example that shows how python classes are really just sugar for the simple cases
def Blank():
# this is a hacky way of returning an empty
# object that you can set attributes on, without
# having to create a class
return lambda: None
def klass(factory):
def init(*args):
self = Blank()
@showell
showell / mit_license.html
Created August 26, 2012 17:59
MIT license vs. Public Domain
Paul Miller asked on Twitter: "Why use MIT license instead of public domain? The only difference is copyright notice inclusion requirement. Is it really needed?" I only know Paul from a few online interactions. His profile says that he is from the Ukraine. I am from the U.S.
I mostly agree with the spirit of Paul's question. The license doesn't really grant you many rights that aren't already covered by most modern democracies, whether it's written law or legal precedent. It's only a minor nuisance to include copyright notices from a purely pragmatic standpoint, but it's irksome nonetheless. When I include the MIT license with software that I wrote, I am first annoyed that the lawyers won. More importantly, I feel that including the license reframes the debate about free software in the wrong direction. In my opinion, software should be naturally free, and you would only ever include licenses with non-free software.
I would support a law that said all software was implicitly covered by the MIT licen
@showell
showell / wildcards.py
Created May 12, 2012 20:00
wildcards from URL paths
def wildcards(path):
parts = path.split('/')
parts = filter(bool, parts)
def generate(parts):
if len(parts) == 0:
yield []
return
head = parts[0]
rest = parts[1:]
for rest in generate(rest):
@showell
showell / big_files.py
Created May 3, 2012 15:41
big_files.py
import random
import time
import os
class SmartDir:
def __init__(self, path):
self.path = path
self.buckets = 50
def clear(self):
@showell
showell / gist:2173618
Created March 23, 2012 18:37
fake web server
# This is the world's most boring and predictable web server. Its purpose in life is
# to facilitate testing of upstream load balancers. The web server is light, so you can
# deploy a bunch of them. If you hit any instance of this web server in a browser
# (presumably via a load balancer, then the page will always tell you who's listening.)
# There's also a simple back door to kill it. It doesn't serve any actual content.
import time
import sys
import re
import os