Skip to content

Instantly share code, notes, and snippets.

def retry_on_connection_error(retries=2, sleep=0.1):
""" Retries on Autoreconnect errors. Wrap functions you want to ensure they
retry if there's a connection failure, giving the chance to mongo to failover.
params:
retries: the number of retries
sleep: seconds to sleep between retries
>>> @retry_on_connection_error(retries=1)
... def super_sensitive_function(data=2)
@alexmic
alexmic / gist:4050955
Created November 10, 2012 12:33
ssh daemon listen to port 443 on EC2
#!/bin/bash -ex
perl -pi -e 's/^#?Port 22$/Port 443/' /etc/ssh/sshd_config
service sshd restart || service ssh restart
from datetime import datetime
import math
def friendly_ago_string(d):
"""
Returns a friendly string like "8 minutes ago" representing the
differences between now and d.
"""
if not d or not isinstance(d, datetime):
return None
@alexmic
alexmic / mtmap_usage.py
Created March 2, 2012 00:30
Using mtmap()
def worker(n):
return n**2
squared = mtmap(worker, [2, 3, 4, 5])
#= [4, 9, 16, 25]
@alexmic
alexmic / mtmap.py
Created March 2, 2012 00:26
Multi-threaded map()
def mtmap(fn, iterable):
""" A multi-threaded map(). Must be used with care - large
iterables might cause a machine to crash from too many
threads. If an exception is thrown in a thread,
the iteration stops and that exception is thrown.
"""
result = {}
threads = []
exception_q = Queue.Queue()
@alexmic
alexmic / gist:1223918
Created September 17, 2011 13:00
FB Login
/**
* @fileOverview Signup page JS code.
* @author <a href="mailto:hi@alexmic.com"> Alex Michael </a>
* @requires PK.js, facebox.js
*/
PK.onPageLoad(function(){
// Once the register page is loaded, make
// a request to FB and fill in user fields.
@alexmic
alexmic / gist:1223906
Last active September 27, 2015 06:08
Python Upload
#!/usr/bin/env python
import string
import os
import uuid
import mimetypes
class Uploader:
def __init__(self, request = None):