Skip to content

Instantly share code, notes, and snippets.

@zed
zed / .gitignore
Created January 17, 2011 16:08
What is faster x**.5 or math.sqrt(x) in Python?
.tox/
/MANIFEST
@zed
zed / get-all-post.sh
Created September 29, 2011 18:27
Get all posts from del.icio.us
#!/bin/sh
#
# Get all posts from del.icio.us
# See http://www.delicious.com/help/api
#
USER=$1
OUT=posts-${USER}.xml
[ -z "$USER" ] && echo "USAGE: $0 USERNAME" && exit 2
@zed
zed / urllib2_ssl.py
Created November 8, 2011 05:09
SSL client/server certificates verification for `urllib2`. :python:ssl:
"""SSL client/server certificates verification for `urllib2`.
It works on Python 2.6, 2.7, 3.1, 3.2
It also works on Python 2.4, 2.5 if `ssl` is installed (``pip install ssl``)
Example::
>>> import urllib2, urllib2_ssl
>>> opener = urllib2.build_opener(urllib2_ssl.HTTPSHandler(
... key_file='clientkey.pem',
@zed
zed / test.sh
Created November 13, 2011 09:37
getuid() vs. geteuid() in Python
# checkout pyintaller
##svn co http://svn.pyinstaller.org/trunk pyinstaller && cd pyinstaller
# make Python script that prints uid, euid
printf '#!/usr/bin/env python\nimport os\nprint(os.getuid(),os.geteuid())\n' >print-uid.py
# create stand-alone executable
python pyinstaller.py -F print-uid.py
# change ownership to root
@pamelafox
pamelafox / selenium_page.py
Created November 30, 2011 22:48
Selenium Utility Funcs
from selenium.common.exceptions import NoSuchElementException, TimeoutException, WebDriverException
class SeleniumPage(object):
driver = None
waiter = None
def open_page(self, url):
self.driver.get(url)
@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."""
@jboner
jboner / latency.txt
Last active January 20, 2026 16:47
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD