Skip to content

Instantly share code, notes, and snippets.

@andres-erbsen
andres-erbsen / enchtml.py
Created October 23, 2011 18:24
Python to javascript encryption - HTML is encrypted using AES and put in a JS string, decrypted on page load after getting password from user
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from sys import stdin, stdout, stderr
from optparse import OptionParser
from os import urandom
from hashlib import sha256
from Crypto.Cipher import AES
from base64 import b64encode
@andres-erbsen
andres-erbsen / vikerarchive.py
Created October 23, 2011 18:35
Vikerraadio arhiivist sarja allalaadmine - download entire series from vikerraadio archive
from datetime import datetime
import xml.dom.minidom
import re
import urllib
archive_url = 'http://vikerraadio.err.ee/kuularhiiv?saade=25&kid=123'
kid = re.findall('kid=\d+',archive_url)[0].replace('kid=','')
rss_base_url = 'http://vikerraadio.err.ee/gfx/rss2.php?id='
rss_url = rss_base_url + kid
@andres-erbsen
andres-erbsen / kysija.py
Created October 23, 2011 18:54
'Flash cards' implementation, old and possibly buggy in some rarely used code paths, but it works. fileformat is 'answer $ question # comment'
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import os
import codecs
from sys import argv, hexversion
from random import randint
from time import sleep
@andres-erbsen
andres-erbsen / gist:1307715
Created October 23, 2011 18:57
Bash API for snurl.com URL shortening service (old)
#!/bin/bash
# SNURL_KEY=your-SNURL_KEY-here
# SNURL_USER=username
if [ "x${SNURL_KEY}" == "x" ]
then
echo "You need set set sn.im API key, preferably by appending 'export SNURL_KEY=mykey' to .bashrc file. To get one you have to register at sn.im"
exit 1
fi
@andres-erbsen
andres-erbsen / gist:1307726
Created October 23, 2011 19:04
Python API for ekool.eu, (supports accessing homework and grades, tested 2011-06-12)
#!/usr/bin/python
# -*- encoding: utf-8 -*-
### Realeased under LGPL-3
### Copyright Andres Erbsen <andres.erbsen@gmail.com> 2011
import urllib2, urllib
import time
import re
import pickle
@andres-erbsen
andres-erbsen / gist:1307745
Created October 23, 2011 19:15
Bash script to install firefox extension xpi (for all users by default)
#!/bin/bash
EXTENSIONSDIR="/usr/lib/firefox-addons/extensions"
TMP="/tmp/firefox-extension-install-temp"
[ x"$1" == "x" ] || [ ! -f $1 ] && echo "Specify valid .xpi file as argument." && exit 1
sudo rm -rf $TMP 2>/dev/null
mkdir -p "$TMP"
trap "sudo rm -rf $TMP 2>/dev/null" 1 2 3 15
@andres-erbsen
andres-erbsen / itershuffle.py
Created October 23, 2011 19:22
Shuffle for python iterators. This works by holding `bufsize` items back and yielding them sometime later. This is NOT 100% random, proved or anything.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
def itershuffle(iterable,bufsize=1000):
"""Shuffle an iterator. This works by holding `bufsize` items back
and yielding them sometime later. This is NOT 100% random, proved or anything."""
iterable = iter(iterable)
buf = []
@andres-erbsen
andres-erbsen / cached.py
Created October 23, 2011 19:28
Caching decorator for python, like functools.lru_cache in Python 3.2+ without LRU. Written for easy debugging of dynamic programming algorthms. Also includes that caches to a pickle file, but this is only useful for very timeconsuming calls like web reque
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import wraps
try:
import cPickle as pickle
except:
import pickle
def cached(fn):
cache = {}
@andres-erbsen
andres-erbsen / gist:1307797
Created October 23, 2011 19:45
Use a directory as a task queue. Process files in they were added and keep track of progress.
import os, glob
import cPickle as pickle
class QueueDir:
def __init__(self,dir,pattern='*',statefilename='.queuedir.pc'):
self.statefilename = statefilename
self.pattern = pattern
self.dir = dir
self.file_in_progress_mtime = None
@andres-erbsen
andres-erbsen / fifteenpuzzle.c
Created November 15, 2011 20:45
Solve "fifteen puzzle" of size M*N in O((M+N)*M*N) time and determine solveability in O(M*N) time. Originally started for an EIO task.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <malloc.h>
// input file is named pusle.sis and on the first row there should be
// dimensions of the puzzle, then all tiles should follow (separated by whitespace)
// Example:
// 2 3
// 1 2 3