Skip to content

Instantly share code, notes, and snippets.

View allanlei's full-sized avatar
:shipit:
What's up?

Allan Lei allanlei

:shipit:
What's up?
View GitHub Profile
@allanlei
allanlei / diff.py
Created April 14, 2014 06:21
dict diff
# -*- coding: utf-8 -*-
MISSING_KEY_VALUE = None
def diff(d2, d1={}, missing=MISSING_KEY_VALUE):
for key, val in d2.items():
if key not in d1:
yield key, (missing, val)
@allanlei
allanlei / gist:300602157a54153b754b
Last active August 29, 2015 14:01
docker + etcd - Command to read all keys from a directory of etcd and set as env in a docker run command
docker run $(etcdctl ls $ETCD_ENV | while read e; do etcdctl get $e > /dev/null 2>&1 && echo --env $(echo $e | rev | cut -d/ -f1 | rev)=$(etcdctl get $e); done) busybox env
@allanlei
allanlei / python-site
Created May 26, 2014 04:53
Sample python backend for nginx
server {
listen 80 default_server deferred;
listen 443 default_server deferred ssl;
listen [::]:80 ipv6only=on default_server deferred;
listen [::]:443 ipv6only=on default_server deferred ssl;
server_name example.com www.example.com testing.example.com;
root /path/to/static/files
# Include SSL stuff
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import urlparse
import click
@click.command()
@click.argument('iam_role')
@click.option('--metadata-url', default='http://169.254.169.254/latest/meta-data/iam/security-credentials/')
@allanlei
allanlei / Dockerfile
Created June 10, 2014 10:42
Command line boto docker container
FROM dockerfile/python
RUN pip install boto
import StringIO
import subprocess
import os
import time
from datetime import datetime
from PIL import Image
# Motion detection settings:
# Threshold (how much a pixel has to change by to be marked as "changed")
# Sensitivity (how many changed pixels before capturing an image)
@allanlei
allanlei / motion.py
Created June 12, 2014 01:53
Motion detection using OpenCV with Differential Images technique. Source http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/
import cv2
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
cam = cv2.VideoCapture(0)
winName = "Movement Indicator"
@allanlei
allanlei / forward.py
Last active August 29, 2015 14:02
Twisted portforwarding. Source: http://therning.org/magnus/archives/30
from twisted.internet import reactor
from twisted.protocols import portforward
def server_dataReceived(self, data):
print 'Server received data:', data
portforward.Proxy.dataReceived(self, data)
portforward.ProxyServer.dataReceived = server_dataReceived
def client_dataReceived(self, data):
print 'Client received data:', data
@allanlei
allanlei / procfile.py
Created August 27, 2014 09:29
Parse Procfile
def procfile(filepath):
with open(filepath, 'rb') as fp:
for process_line in fp.readlines():
process_line = process_line.strip()
if not process_line or process_line.startswith('#'):
continue
process_name, command = process_line.split(':', 1)
yield process_name.strip(), command.strip()

Keybase proof

I hereby claim:

  • I am allanlei on github.
  • I am allanlei (https://keybase.io/allanlei) on keybase.
  • I have a public key whose fingerprint is 851F D7DF 24A1 0109 D014 7301 8240 ED3E 5606 E5BA

To claim this, I am signing this object: