Skip to content

Instantly share code, notes, and snippets.

@temoto
temoto / test_pub_sub_double_bind.py
Created March 20, 2012 12:30
ZeroMQ test PUB-SUB multiple bind to ipc socket
#!/usr/bin/env python
# coding: utf-8
import os
import time
import zmq
name = "main"
#endpoint = "ipc://test-pub-sub-double-bind.ipc"
endpoint = "tcp://127.0.0.1:5004"
@temoto
temoto / zmq-to-file.py
Created March 21, 2012 20:31
ZeroMQ -> file writer with optional forwarder device
ZeroMQ -> file writer
@temoto
temoto / 01.sql
Created April 6, 2012 13:50
plpgsql for loop -> single select?
-- Types: ticket_query, ticket_result
-- Functions: ticket_search_1, ticket_unique_sellers
drop type ticket_query cascade;
create type ticket_query as (
cities_from text[],
cities_to text[],
date_direct date,
date_back_min date,
date_back_max date,
@temoto
temoto / xmonad.hs
Created June 9, 2012 16:04
my xmonad config
$ xmonad --recompile
Error detected while loading xmonad configuration file: /home/temoto/.xmonad/xmonad.hs
xmonad.hs:81:54:
Couldn't match expected type `Data.Monoid.Endo WindowSet'
with actual type `()'
Expected type: X (Data.Monoid.Endo WindowSet)
Actual type: X ()
In the first argument of `liftX', namely
`(withDisplay $ \ d -> io (lowerWindow d w))'
@temoto
temoto / main.go
Created June 26, 2012 06:04
Eve Online Market Data Relay
package main
import (
cache "code.google.com/p/vitess/go/cache"
"fmt"
zmq "github.com/alecthomas/gozmq"
"hash"
"hash/fnv"
"os"
"time"
@temoto
temoto / pg-transaction.py
Created June 26, 2012 15:55
[Python] psycopg2 'execute block of code in transaction' context manager
@contextmanager
def transaction(pool='common_write'):
"""Context manager.
Executes block inside DB transaction. Returns cursor.
At the end of the block, the connection is returned to pool.
>>> with transaction() as cursor:
... rows = cursor.execute(...).fetchall()
... process(rows)
... cursor.execute(...)
@temoto
temoto / sqlalchemy_postgresql_union_fix.py
Created July 5, 2012 20:43
SQLAlchemy PostgreSQL UNION with ORDER/LIMIT monkey patch fix
# Problem: sa.union_all(*qs).order_by('y') will generate invalid SQL like this:
# SELECT ... ORDER BY x LIMIT 10 UNION ALL SELECT ... ORDER BY x LIMIT 10 ORDER BY y
# Order in inner queries could be required for LIMIT or DISTINCT ON (field).
#
# Solution: sqlalchemy should put all inner queries that contain order by or limit
# clauses into parenthesis, like this:
# SELECT ... UNION ALL (SELECT ... ORDER BY x LIMIT 10) ORDER BY y
#
# sqlalchemy.sql.compiler.SQLCompiler.visit_compound_select
# Fixed to surround subqueries with parens if they contain ORDER BY or LIMIT or OFFSET.
@temoto
temoto / gist:3397886
Created August 19, 2012 21:36
Linux loop device problem
# mount -t iso9660 -o loop,ro,noexec ~temoto/Downloads/archlinux-2012.06-1-archboot-x86_64.iso /tmp/arch/
mount: /tmp/arch/: mount failed: No such file or directory
$ ls -l ~/Downloads/archlinux-2012.06-1-archboot-x86_64.iso
-rw-r--r-- 1 temoto users 375390208 Aug 20 00:39 /home/temoto/Downloads/archlinux-2012.06-1-archboot-x86_64.iso
$ ls -ld /tmp/arch
drwxr-xr-x 2 root root 4096 Aug 20 01:10 /tmp/arch
@temoto
temoto / test015.py
Created August 26, 2012 14:10
gevent reraise exception
import gevent
#...
def test_reraise_01():
try:
gevent.spawn(lambda: 1/0).join()
assert False, "ZeroDivisionError excepted"
except ZeroDivisionError:
pass
@temoto
temoto / cpu-perf-test.cxx
Created September 3, 2012 20:07
Various elementary CPU operations benchmark
# gcc -O3 -o cpu-perf-test -lrt cpu-perf-test.cxx && (for x in {1..7}; do sleep 0.3s; ./cpu-perf-test; done) && rm cpu-perf-test
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
//const char *op_tag = "id "; long op(long x) { return x; }
const char *op_tag = "*2 "; long op(long x) { return x * 2; }
//const char *op_tag = "<<1"; long op(long x) { return x << 1; }
//const char *op_tag = "*27"; long op(long x) { return x * 27; }