Skip to content

Instantly share code, notes, and snippets.

>>> import urlparse
>>> urlparse.parse_qs("mp3=http%3A//hwzhiyin.com/uploadfile/1369168835.mp3&loop=1&autoplay=1")
{'mp3': ['http://hwzhiyin.com/uploadfile/1369168835.mp3'], 'loop': ['1'], 'autoplay': ['1']}
def _mturk_flatten(obj):
if isinstance(obj, basestring):
return {"": obj}
elif isinstance(obj, collections.Mapping):
iterable = obj.items()
elif isinstance(obj, collections.Iterable):
iterable = enumerate(obj)
else:
return {"": obj}
class Base(object):
def __init__(self, pen, home):
super(Base, self).__init__()
class HomeMixin(Base):
def __init__(self, pen, home):
super(HomeMixin, self).__init__(pen, home)
self.home = home
class PenMixin(Base):
class HomeMixin(object):
def __init__(self, home, *args, **kwargs):
super(HomeMixin, self).__init__(*args, **kwargs)
self.home = home
class PenMixin(object):
def __init__(self, pen, *args, **kwargs):
super(PenMixin, self).__init__(*args, **kwargs)
self.pen = pen
class Event(object):
def __init__(self):
self.subscribers = []
def subscribe(self, callback):
self.subscribers.append(callback)
def notify(self, *args, **kwargs):
for subscriber in self.subscribers:
def crazy_computation(expression, values):
"""
given expression = ["a", "+", "b", "-", "c"] and values = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]},
return [-2, -1, 0]
"""
# taking care of the cases when one of the two first operands is a list
if not isinstance(expression[0], basestring):
intermediate = crazy_computation(expression[0], values)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import re
import datetime
import subprocess
import sys
query_date = datetime.datetime.today() - datetime.timedelta(days=3)
query_date = query_date.strftime("%d-%b")
import itertools
from PIL import Image
img = Image.new("RGB", (10, 10))
pixels = img.load()
for i, j in itertools.product(xrange(10), xrange(10)):
if (i + j) % 2 == 0:
pixels[i, j] = (0, 0, 0)
else:
while True:
v = input()
if v.lower() == "none":
v = None
break
try:
v = int(v)
except ValueError:
class Number(object):
def __init__(self, value):
self.value = value
def __add__(self, other):
return Average(self.value, 1) + other
def __radd__(self, other):
return Average(self.value, 1) + other