Skip to content

Instantly share code, notes, and snippets.

@kirsle
kirsle / attrdict.py
Created July 18, 2014 21:07
AttrDict - A dict for Python that behaves like JavaScript.
#!/usr/bin/env python
"""A dict-like object that supports JS object syntax.
AttrDict is a dict that can be called using either square brackets, like a
normal dict, or by using attributes--just like in JavaScript.
Accessing the value for an undefined key will return None (Python's equivalent
to undefined).
@utilitarianexe
utilitarianexe / threadify
Last active December 27, 2015 15:19
Python Decorator to turn a normal function into a function that splits its work among threads. First argument of original function is a list that is split into pieces for each thread to run separately.
import threading
from functools import wraps
def threadify(num_threads,is_method=True,outputs_list=False,outputs_bool = False,outputs_gen=False):
''' Apply this decorator to a function. It turns that function into a new one that splits its work up among threads. This only works on special functions though. The first argument must be a list. This list will be split into even pieces with each piece passed to a thread. Your function must return either None,a list, or a dictionary. If it is a list make sure you specify outputs list as true. Lists are not guaranteed to return in order so dicts are best in most cases. The return of each thread will automatically be combined for you. The overhead is quite low'''
def threadify_real(func):
def threaded(*args,**kwargs):
threads = []
@dstroot
dstroot / install-redis.sh
Created May 23, 2012 17:56
Install Redis on Amazon EC2 AMI
#!/bin/bash
# from here: http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
# and here: https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
###############################################
# To use:
# wget https://raw.github.com/gist/2776679/04ca3bbb9f085b192f6aca945120fe12d59f15f9/install-redis.sh
# chmod 777 install-redis.sh
# ./install-redis.sh
###############################################
echo "*****************************************"
@gregburek
gregburek / rateLimitDecorator.py
Created December 7, 2011 01:51
Rate limiting function calls with Python Decorators
import time
def RateLimited(maxPerSecond):
minInterval = 1.0 / float(maxPerSecond)
def decorate(func):
lastTimeCalled = [0.0]
def rateLimitedFunction(*args,**kargs):
elapsed = time.clock() - lastTimeCalled[0]
leftToWait = minInterval - elapsed
if leftToWait>0: