Skip to content

Instantly share code, notes, and snippets.

View bancek's full-sized avatar

Luka Zakrajšek bancek

  • Koofr
  • Ljubljana, Slovenia
View GitHub Profile
@bancek
bancek / fabric_threadsafe_patch.py
Created September 29, 2011 20:22
Fabric threadsafe state patch
import threading
from functools import wraps
from UserDict import UserDict
state = threading.local()
class DictProxy(UserDict, object):
def __init__(self, getter, dict=None, **kwargs):
object.__setattr__(self, 'getter', getter)
@bancek
bancek / fixexes.py
Created October 14, 2011 16:00
Python replace exe files with bat files
import os
from glob import glob
for file in glob('*-script.py'):
exe = file.replace('-script.py', '.exe')
bat = file.replace('-script.py', '.bat')
ax = filter(os.path.exists, [exe, bat])
print file
@bancek
bancek / localtunnel_server.py
Created October 16, 2011 09:15
LocalTunnel server
try:
from twisted.internet import pollreactor
pollreactor.install()
except: pass
from twisted.internet import protocol, reactor, defer, task
from twisted.web import http, proxy, resource, server
from twisted.python import log
import sys
import time
@bancek
bancek / .xbindkeysrc
Created December 29, 2011 22:53
Logitech Nano VX Ubuntu volume keys
#Volume up
"amixer sset Master 5+ unmute"
m:0x0 + b:9
#Volume down
"amixer sset Master 5- unmute"
m:0x0 + b:8
@bancek
bancek / xml_to_json.py
Created September 15, 2012 20:53
XML to JSON
import json
from collections import OrderedDict
from lxml import objectify
def xml_to_py(el):
if len(set([x.tag for x in el.getchildren()])) == 1:
return [xml_to_py(x) for x in el.getchildren()]
if hasattr(el, 'pyval'):
return el.pyval
@bancek
bancek / bashinit
Created October 3, 2012 13:56
Bash init buffer. Usage: bashinit "cd some/path\rdosomething\r"
#!/usr/bin/expect
trap {
set rows [stty rows]
set cols [stty columns]
stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
spawn "/bin/bash"
@bancek
bancek / terminator_layout_builder.py
Created October 5, 2012 06:24
Terminator layout builder
INDENT = ' '
lines = open('data').read().splitlines()
def calc_indent(line):
return len([x for x in line.split(INDENT) if not x])
_last_id = 0
def next_id(name):
@bancek
bancek / moodlepdf.url
Created October 28, 2012 16:31
Moodle PDF
javascript:window.location = document.getElementsByTagName('frame').length?document.getElementsByTagName('frame')[1].src:$('object').attr('data');
@bancek
bancek / ieee754.py
Last active October 13, 2015 11:38
IEEE 754
import struct
from_hex = lambda hx: struct.unpack('>f', struct.pack('>I', hx))[0]
to_hex = lambda dc: '0x' + ''.join(['%.2x' % ord(x) for x in struct.pack('>f', dc)])
chr_to_bin = lambda ch: ('0'*8 + bin(ord(ch))[2:])[-8:]
to_32_bits = lambda fl: '|'.join((lambda bits: [bits[:1], bits[1:9], bits[9:]])(''.join(map(chr_to_bin, struct.pack('>f', fl)))))
to_64_bits = lambda fl: '|'.join((lambda bits: [bits[:1], bits[1:12], bits[12:]])(''.join(map(chr_to_bin, struct.pack('>d', fl)))))
from_bits = lambda bs: from_hex(int(bs.replace('|', ''), 2))
a = 0xfb
@bancek
bancek / gol.py
Created December 8, 2012 16:42
Conway python
import sys
import os
import time
from collections import namedtuple
Cell = namedtuple('Cell', ['x', 'y'])
class World:
def __init__(self, cells):
self.cells = set(cells)