Skip to content

Instantly share code, notes, and snippets.

@larsr
larsr / tspi.py
Created April 13, 2012 13:57
Calling Trousers TSS stack from Python
import ctypes
class Tspi:
def __init__(self):
self.hContext = ctypes.c_uint32(0)
self.tspi = ctypes.CDLL('libtspi.so.1')
e = self.tspi.Tspi_Context_Create(ctypes.pointer(self.hContext))
assert e==0,'Error: %x' % e
def err(self,e):
@larsr
larsr / gist:2708695
Created May 16, 2012 08:27
Two-line shell archive
SEP=03248902398402398402390239402
awk < $0 '/^'$SEP'/{f=substr($0,length("'$SEP'")+2);} f && !/^'$SEP'/{print $0 > f}'; exit $?
03248902398402398402390239402-file_one.txt
this is
file one
03248902398402398402390239402-file_two.txt
and this
is file
number two
@larsr
larsr / chromebook303.dtb
Created December 10, 2012 08:49
dtb for chromebook303 kernel
/dts-v1/;
/ {
description = "Chrome OS kernel image with one or more FDT blobs";
#address-cells = <1>;
images {
kernel@1 {
data = /incbin/("/home/larsr/build/arch/arm/boot/zImage");
type = "kernel_noload";
def quicksort(l):
work = [(0,len(l)-1)]
while work:
(left,right) = work.pop()
if right < left:
continue
i_piv = left
piv = l[i_piv]
l0,r0 = left, right
while left<right:
@larsr
larsr / sudoku-start.py
Last active November 8, 2018 10:40
If you want to write a sudoku solver, you can start with this code. It loads a game from a string, and can pretty-print the "possible values" of the fields.
# sudoku-starter-kit, lars.rasmusson@gmail.com, 2013-04-30
game = '.8...6...|32.9.47..|.4...7.6.|93..1....|......8.9|8..2.....|...4..17.|.7...89..|..4......'
def rc(r,c): return (r-1)*9+(c-1)
def init_sud(sud):
v=[]
for c in sud:
if c in "123456789": v.append([int(c)])
elif c in ".": v.append(range(1,10))
assert(len(v)==81)
return v
@larsr
larsr / synonyms.py
Created May 7, 2013 13:31
Fetch synonyms and antonyms from thesaurus.com
import BeautifulSoup
import urllib2
def word(w):
return BeautifulSoup.BeautifulSoup(urllib2.urlopen("http://thesaurus.com/browse/"+w).read())
def synonym(w):
w = word(w)
return w.find('table', {"class":'the_content'}).find('td',text=lambda t:'Synonym' in t).parent.parent.text
@larsr
larsr / sqr.py
Last active December 17, 2015 10:58
compute the square root of x
# compute the square root of x
def sqr(x):
prec = 1
z = 2
while z < x:
prec = prec + 1
z = z * 4
# Here x <= (2**prec)**2
a = 0
@larsr
larsr / tricube.py
Created October 22, 2013 19:46
"triangle rubik"-cube movements as list permutations.
moves = """
1 14 76
2 15 77
3 16 78
4 17 74
5 18 75
6 19 71
7 20 72
8 21 73
9 22 69
@larsr
larsr / shmem.py
Created September 23, 2014 16:32
import mmap
import posix_ipc
import os
import sys
def create_shared_mem(sm_name, size):
sm = posix_ipc.SharedMemory(sm_name, flags=posix_ipc.O_CREX, size=size)
return mmap.mmap(sm.fd, sm.size)
def open_shared_mem(sm_name):
@larsr
larsr / mupl.py
Last active August 29, 2015 14:09
A python implementation of the MUPL language
class term(object):
def __init__(self,**args):
for k in self.vals:
self.__setattr__(k,args[k])
def __str__(self):
return self.__class__.__name__+"("+(", ".join([k+'='+str(self.__getattribute__(k)) for k in self.vals]))+")"
class const(term): vals = ['val']
class add(term): vals = ['e1', 'e2']
class let(term): vals = ['name','exp', 'body']