Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
matteobertozzi / sqljoin.py
Created November 3, 2011 20:17
Sql Join
#!/usr/bin/env python
# http://en.wikipedia.org/wiki/Join_(SQL)
def crossJoin(table_a, table_b):
"""
Cross join returns the Cartesian product of rows from tables in the join.
In other words, it will produce rows which combine each row from the first
table with each row from the second table.
"""
results = JoinTable(table_a, table_b)
@matteobertozzi
matteobertozzi / mem-info.py
Created November 4, 2011 23:38
Linux Process Memory Info /proc/$PID/status
#!/usr/bin/env python
def _procMemoryGrep(filename, vmkeys):
size_scale = {'kb': 1024.0, 'mb':1048576.0, 'gb':1073741824.0}
values = {}
fd = open(filename)
try:
for line in fd:
for vmkey in vmkeys:
@matteobertozzi
matteobertozzi / higher.py
Created November 5, 2011 10:10
Get 'n' higher values from 'items'
#!/usr/bin/env python
def bsearch(data, key, cmpfunc):
end = len(data)
start = 0
while start < end:
m = (start + end) / 2
c = cmpfunc(data[m], key)
@matteobertozzi
matteobertozzi / ticket-lock.c
Created December 17, 2011 13:52
Ticket Lock (__sync_fetch_and_add)
#define spinlock_t union ticket_lock
#define spin_init ticket_init
#define spin_destroy ticket_destroy
#define spin_lock ticket_acquire
#define spin_unlock ticket_release
union ticket_lock {
volatile unsigned int data;
struct {
volatile unsigned short next_ticket;
@matteobertozzi
matteobertozzi / encoding.py
Created December 26, 2011 19:09
Zig-Zag/VInt Encoding
#!/usr/bin/env python
def encodeZigZag32(n): return (n << 1) ^ (n >> 31)
def encodeZigZag64(n): return (n << 1) ^ (n >> 63)
def decodeZigZag32(n): return (n >> 1) ^ -(n & 1)
def decodeZigZag64(n): return (n >> 1) ^ -(n & 1)
def encodeVInt(n):
data = ''
while n >= 0x80:
@matteobertozzi
matteobertozzi / gist:1799010
Created February 11, 2012 12:07
StreamRequestHandler that allows you to handle line requests
from SocketServer import TCPServer, StreamRequestHandler
class LineService(StreamRequestHandler):
"""
Simple StreamRequestHandler that allows you to handle line requests
Example:
telnet localhost 8080
> mycommand
> quit
"""
@matteobertozzi
matteobertozzi / gist:1799024
Created February 11, 2012 12:09
StreamRequestHandler that allows you to handle json requests
from SocketServer import TCPServer, StreamRequestHandler
class JSonService(StreamRequestHandler):
"""
Simple StreamRequestHandler that allows you to handle json requests
Example:
telnet localhost 8080
> {"action": [1, 2, 3]}
> ["do this", "do that"]
"""
@matteobertozzi
matteobertozzi / gist:1934974
Created February 28, 2012 20:41
Consistent Hashing
def _hashKey(key):
return long(md5(key).hexdigest(), 16)
def _nodeKey(node, replica):
return _hashKey('%s:%d' % (str(node), replica))
class ConsistentRing(object):
"""
Consistent Hashing:
@matteobertozzi
matteobertozzi / zcl-stream-ng.c
Created March 21, 2012 22:10
zcl stream next-gen, zero copy read/write api...
/*
* - stream interface
* [filter stream]
* - buffered stream
* - encoded stram
* - buffer stream
* - chunkq stream
*
* Copyright 2011-2012 Matteo Bertozzi
*
@matteobertozzi
matteobertozzi / tokenize.py
Created April 26, 2012 19:35
Simple Tokenizer
from cStringIO import StringIO
SYMBOLS_QUOTE = ('"', "'")
TOKEN_STRING = 0
TOKEN_NUMBER = 1
TOKEN_KEYWORD = 2
TOKEN_PARENTHESES_OPEN = 3
TOKEN_PARENTHESES_CLOSE = 4
TOKEN_COMMA = 5