Skip to content

Instantly share code, notes, and snippets.

@wolf0403
wolf0403 / fibs.py
Created July 6, 2014 05:15
Different ways of fib() in Python
N=30
PRINT = False
def f1(n):
if n < 2: return n
return f1(n-1) + f1(n-2)
print f1(N) if PRINT
def f2(n, mem={}):
if n < 2: return n
def runserver(port=5000, profile_log=None):
"""Runs a development server."""
from gevent.wsgi import WSGIServer
from werkzeug.serving import run_with_reloader
from werkzeug.debug import DebuggedApplication
from werkzeug.contrib.profiler import ProfilerMiddleware
port = int(port)
if profile_log:
@wolf0403
wolf0403 / arg_sink.py
Created May 21, 2014 09:25
Arg Sink decorator
# When calling functions, remove unused arguments
# Useful for framework callbacks
def args_sink(f):
import inspect
dst_args = inspect.getargspec(f)
args = set(dst_args.args)
@wraps(f)
def wrapper(*a, **kw):
nkw = deepcopy(kw)
@wolf0403
wolf0403 / admin.py
Last active August 29, 2015 14:01
Flask / Request reverse proxy, with `admin' (upload) interface.
#!/usr/bin/env python
from __future__ import unicode_literals, print_function
import os
import subprocess as sp
from subprocess import check_output
from flask import Flask, Blueprint, current_app, \
request, redirect, url_for
@wolf0403
wolf0403 / sort_by_du_h.py
Created March 20, 2014 00:31
sort "du -h" output.
def cmpsz(s1, s2):
tosz = lambda s: ('KMG'.find(s[-1]), float(s[:-1]) if s[-1] in 'KMG' else float(s))
u1, s1 = tosz(s1)
u2, s2 = tosz(s2)
scmp = cmp(u1, u2)
return scmp if scmp != 0 else cmp(s1, s2)
@wolf0403
wolf0403 / access_log_parser.py
Created March 13, 2014 03:27
Python re for Nginx access_log
# Modified by adding names from https://github.com/richardasaurus/nginx-access-log-parser/blob/master/main.py
pat = (r''
'(?P<ip>\d+.\d+.\d+.\d+)\s-\s-\s' #IP address
'\[(?P<time>.+)\]\s' #datetime
'"(?P<method>GET|POST)\s(?P<uri>.+)\s(?P<ver>\w+/.+)"\s(?P<status>\d+)\s' #requested file
'(?P<content_length>\d+)\s"(?P<referrer>.+)"\s' #referrer
'"(?P<user_agent>.+)"' #user agent
)
r = re.match(pat, logline)
r.groupdict()
@wolf0403
wolf0403 / subcribe.conf
Created February 21, 2014 01:45
Email subscription form "backend"
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
listen 80 default_server;
server_name _;
root /home/ubuntu/landing-page;
access_log /var/log/nginx/access_80.log;
error_log /var/log/nginx/error_80.log;
@wolf0403
wolf0403 / workers.py
Created December 30, 2013 14:28
Worker model with Redis and Gevent
#!/usr/bin/env python
from __future__ import print_function
import gevent
import gevent.monkey
gevent.monkey.patch_all()
import gevent.hub
import redis
@wolf0403
wolf0403 / Makefile
Last active January 1, 2016 04:48
How to define static member functions of class templates correctly.
a.out: a.o b.o def.o
%.o: %.cpp
clang++ -c -o $@ $<
@wolf0403
wolf0403 / node-phantom-reverseproxy.js
Created July 22, 2013 05:21
Deajaxify page for Google crawler with Node.js / Phantom.js
var http = require('http'),
phantom=require('node-phantom'), // https://github.com/alexscheelmeyer/node-phantom
urlparse = require('url');
function proxy_url(url, res) {
phantom.create(function(err,ph) {
return ph.createPage(function(err,page) {