Skip to content

Instantly share code, notes, and snippets.

View tarekziade's full-sized avatar

Tarek Ziade tarekziade

View GitHub Profile
@tarekziade
tarekziade / piproxy.py
Created May 2, 2012 09:12
Proxy to simulate --allow-hosts with PIP- adapted from a wsgi proxy found out there
# adapted from https://code.google.com/p/wsgi-proxy
from httplib import HTTPConnection
from urlparse import urlparse
import copy
import logging
import mimetypes
import os
_hoppish = {
@tarekziade
tarekziade / marketplace_installation.rst
Created September 24, 2012 10:09
Installing Marketplace in Ubuntu 12.04
@tarekziade
tarekziade / settings_local_mkt.py
Created October 2, 2012 11:01
settings file for mkt
from mkt.settings import *
DEBUG = True
TEMPLATE_DEBUG = DEBUG
DEBUG_PROPAGATE_EXCEPTIONS = DEBUG
LESS_PREPROCESS = LESS_LIVE_REFRESH = True
LESS_BIN = '/usr/local/bin/lessc'
UGLIFY_BIN = 'uglifyjs'
SERVE_TMP_PATH = True
CELERY_ALWAYS_EAGER = True
@tarekziade
tarekziade / hard_error.rst
Created October 17, 2012 20:09
Hard to find error in Python 2

So you have some data mapping you format in a string:

>>> data = {'a': 'é', 'b': 's'}
>>> '%(b)s %(a)s' % data
's \xc3\xa9'

Nice. Python is simply formating the string and everything works.

Now what happens if one of the keys is unicode:

@tarekziade
tarekziade / verify.py
Created November 19, 2012 14:23
Verifying a gpg signed package from PyPI
# you need to install python-gpgme
import StringIO
import urllib
import gpgme
import shutil, tempfile
import os
from contextlib import closing, contextmanager
tarball = 'http://pypi.python.org/packages/source/s/signtest/signtest-1.0.tar.gz'
import os
from multiprocessing.pool import Pool, TimeoutError
import signal
import functools
import time
class TimeoutFunc(object):
def __init__(self, func, duration, ctimeout):
self.func = func
@tarekziade
tarekziade / distribution.py
Last active June 4, 2016 05:39
Consistent Distribution of users across servers
""" Consistent load-balancing.
We have a few servers and we want a load-balancer to
distribute incoming requests across them in a deterministic
and consistent way - without keeping any counter to make the
decision.
Removing a backend server should not impact users on other
servers.
@tarekziade
tarekziade / detect.rst
Created October 14, 2016 14:02
Detect and kill an idling loads ec2 instance

On the EC2 instance A few variable to set:

Run a docker with the docker sock linked and a few variables set:

docker run -t -v /var/run/docker.sock:/run/docker.sock -v $(which docker):/bin/docker  -i ubuntu /bin/bash
apt-get update && apt-get install -y python-pip && pip install docker-py==1.6.0

export CID=`cat /proc/self/cgroup | grep -o  -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"`
@tarekziade
tarekziade / async_workers.py
Created January 9, 2017 21:03
Async Workers using Threads
import requests
import asyncio
import sys
from functools import partial
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
loop = asyncio.get_event_loop()
@tarekziade
tarekziade / api.py
Last active August 1, 2018 16:57
connexion as an extension
import pathlib
from connexion.api import Api, Resolver
import werkzeug.exceptions
def swagger_blueprint(specification, specification_dir=None, validate_responses=False,
strict_validation=False, resolver_error=None,
base_path='', debug=False):
resolver = Resolver()
swagger_json = False