Skip to content

Instantly share code, notes, and snippets.

View duydo's full-sized avatar

Duy Do duydo

View GitHub Profile
import simplejson
from boto.s3.connection import S3Connection
from boto.s3.key import Key
class S3KeyStore(object):
def __init__(self, access_key, secret_key, bucket):
self.conn = S3Connection(access_key, secret_key)
self.bucket = self.conn.create_bucket(bucket)
def get(self, key):
name: ${HOSTNAME}
# The cluster should be named with the name of the AD domain
cluster:
name: bldrprod-0.14.0
routing:
allocation:
# As small as possible, very large performance hits during recovery
concurrent_recoveries: 1
__author__ = 'duydo'
from base64 import encodestring
from json import loads as json_encode
from requests import get
class SearchAPI(object):
MAX_RESULTS = 10
@duydo
duydo / gist:455ff86fcc9c74349307
Created September 16, 2014 10:36 — forked from cuimuxi/gist:3719516
Gevent Crawler
import gevent
from gevent import monkey, queue
monkey.patch_all()
import urllib2
from time import sleep
import traceback
import logging
from __future__ import with_statement
from kombu import BrokerConnection
from collections import defaultdict
import gevent
from gevent import monkey
monkey.patch_all()
class WorkerHub():
"""
WorkerHub controls the local mailboxes that the @worker decorator assigns.
@duydo
duydo / crawler.py
Last active August 29, 2015 14:06 — forked from jmoiron/crawler.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Simple async crawler/callback queue based on gevent."""
import traceback
import logging
import httplib2
import gevent
@duydo
duydo / tree.md
Last active August 29, 2015 14:06 — forked from hrldcpr/tree.md

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@duydo
duydo / pagination.md
Last active August 29, 2015 14:06 — forked from mislav/pagination.md

Pagination 101

Article by Faruk Ateş, [originally on KuraFire.net][original] which is currently down

One of the most commonly overlooked and under-refined elements of a website is its pagination controls. In many cases, these are treated as an afterthought. I rarely come across a website that has decent pagination, and it always makes me wonder why so few manage to get it right. After all, I'd say that pagination is pretty easy to get right. Alas, that doesn't seem the case, so after encouragement from Chris Messina on Flickr I decided to write my Pagination 101, hopefully it'll give you some clues as to what makes good pagination.

Before going into analyzing good and bad pagination, I want to explain just what I consider to be pagination: Pagination is any kind of control system that lets the user browse through pages of search results, archives, or any other kind of continued content. Search results are the o

# Don't use this.
import zmq
import os
class Worker:
def __init__(self):
print "parent: %d, pid: %d" % (os.getppid(), os.getpid())
self.pid = os.getppid()
self.context = zmq.Context()
self.sub = self.context.socket(zmq.SUB)
@duydo
duydo / timeout.py
Last active August 29, 2015 14:06 — forked from felipecruz/timeout.py
import signal
def signal_handler(signum, frame):
raise Exception("Timed out!")
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(10) # Ten seconds
try:
long_function_call()