Skip to content

Instantly share code, notes, and snippets.

@zed
zed / show_builtins_type.py
Created May 10, 2013 21:47
show `__builtins__` type (module or `.__dict__`)
def f():
print(__name__)
print(type(__builtins__))
f()
if __name__=="__main__":
import show_builtins_type

Create a Postgis timezone DB

Ubuntu's analog of robcowie / postgis_timezone_db.markdown

Install postgis

sudo apt-get install python-software-properties
sudo apt-add-repository ppa:ubuntugis/ppa
sudo apt-get update

sudo apt-get install postgresql-9.1-postgis

@zed
zed / reporttime.py
Last active December 17, 2015 17:59
Compare time performance of recursive vs. generator powerset versions. http://stackoverflow.com/q/16731561/
# -*- coding: utf-8 -*-
import inspect
import sys
import timeit
from functools import partial, wraps
from timeit import default_timer as timer
__version__ = "0.3.0"
#!/usr/bin/env python
"""Merge sort a singly linked linear list."""
import random
from itertools import product
# Linked list is either empty or a value and a link to the next list
empty = None # empty list
class LL(object):
__slots__ = "value", "next"
@zed
zed / bench-all.py
Last active December 18, 2015 13:29
import sys
from subprocess import call
for i in range(11):
print("1e%d:" % i)
sys.stdout.flush()
call([sys.executable, '-m', 'timeit',
'-s', 'import numpy as np; a = np.zeros(10**%d, dtype=bool)' % i,
'a.all()']) # all() should return after the first check (zeros)
#!/usr/bin/env python3
import locale
import os
import sys
envname = "PYTHONIOENCODING"
print("{}:\t{}".format(envname, os.environ.get(envname)))
for set_locale in [False]:
@zed
zed / try_named_pipe.py
Last active September 24, 2016 09:42
#!/usr/bin/env python3
"""Emulate bash:
$ cat /tmp/fifo.tub &
$ gunzip -c /tmp/filedata.dat.gz > /tmp/fifo.tub
http://stackoverflow.com/questions/19859283/python-subprocess-hangs-with-named-pipes
"""
import os
from contextlib import contextmanager
#!/usr/bin/env python
"""Show messages in two new console windows simultaneously.
See http://stackoverflow.com/q/19479504
"""
import sys
import platform
from subprocess import Popen
messages = 'This is Console1', 'This is Console2'
#!/usr/bin/env python
"""Demo for [gobject.timeout_add_seconds fails after system hibernation][1]
[1]: http://stackoverflow.com/q/19882390/
"""
import time
import gtk, glib
def callback():
print(time.asctime())
/* pipe/fork/dup2/execve exercise.
The results are similar to: `sort -u --parallel 2 < input_file`
http://stackoverflow.com/questions/19876980/create-2-child-processes-to-sort-words-of-a-file-using-pipe
Usage:
$ gcc -o sort-parallel-uniq *.c && ./sort-parallel-uniq < input_file
*/