Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
from __future__ import print_function
import functools
import sys
try: from collections import Counter
except ImportError:
pass # ignore
from collections import defaultdict
@zed
zed / .gitignore
Created November 20, 2011 14:20
WebSocket Echo :python:twisted:txws:jquery:
/twistd.pid
/_trial_temp/
@zed
zed / format_map.py
Created November 21, 2011 23:31
''.format_map() in Python 2.x
"""''.format_map() in Python 2.x"""
try:
''.format_map({})
except AttributeError: # Python < 3.2
import string
def format_map(format_string, mapping, _format=string.Formatter().vformat):
return _format(format_string, None, mapping)
del string
@zed
zed / casefold.py
Created January 26, 2012 02:16
full unicode casefolding in Python (case-insensitive search for keywords)
import regex # pip install regex
keywords = ['post', 'another']
p = regex.compile(ur'(?fi)\L<keywords>', keywords=keywords)
p.findall(u'POST, Post, post, poſt, post, and poſt')
# -> [u'POST', u'Post', u'post', u'po\u017ft', u'po\ufb06', u'po\ufb05']
@zed
zed / fib-fork.py
Created February 6, 2012 09:36
Recursive Fibonacci using Fork
#!/usr/bin/env python
"""
http://stackoverflow.com/questions/9156823/recursive-fibonacci-using-fork-in-c
"""
import os
import sys
def exitcode(pid):
retpid, status = os.waitpid(pid, 0)
assert retpid == pid and os.WIFEXITED(status)
@zed
zed / cacert.pem
Created February 16, 2012 04:19
https POST using socket, httplib, urllib2 on Python 2.x, 3.x :python:ssl:
##
## ca-bundle.crt -- Bundle of CA Root Certificates
##
## Certificate data from Mozilla as of: Wed Jan 18 00:04:16 2012
##
## This is a bundle of X.509 certificates of public Certificate Authorities
## (CA). These were automatically extracted from Mozilla's root certificates
## file (certdata.txt). This file can be found in the mozilla source tree:
## http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1
##
@zed
zed / countdown.py
Created March 1, 2012 18:15
kill gedit in 10 seconds with a countdown using Tkinter :python2.x:python3.x:ttk:
import sys
import time
version = sys.hexversion
if 0x03000000 <= version < 0x03010000 :
import tkinter
import ttk
elif version >= 0x03010000:
import tkinter
import tkinter.ttk as ttk
#file: next_permutation.pyx
from libcpp cimport bool
cimport numpy as np
ctypedef np.int_t dtype_t
cdef extern from "<algorithm>" namespace "std":
bool cpp_next_permutation "std::next_permutation" (dtype_t* first, dtype_t* last)
@zed
zed / cydot.pyx
Created March 16, 2012 18:25
Naive O(N**3) 2D np.dot() multithreaded implementation (CPython extension in Cython)
#cython: boundscheck=False, wraparound=False
import numpy as np
cimport numpy as np
from cython.parallel cimport prange
def dot(np.ndarray[np.float32_t, ndim=2] a not None,
np.ndarray[np.float32_t, ndim=2] b not None,
np.ndarray[np.float32_t, ndim=2] out=None):
"""Naive O(N**3) 2D np.dot() implementation."""
@zed
zed / longest_match_datrie.py
Created July 12, 2012 19:54
longest prefix match
import string
import datrie # pip install datrie
def longest_match_datrie(search, hosts):
trie = datrie.new(alphabet=string.printable)
for url in hosts:
trie[url.decode('ascii')] = 1
matches = trie.keys(search.decode('ascii'))
return max(matches, key=len) if matches else ''