Skip to content

Instantly share code, notes, and snippets.

View japsu's full-sized avatar

Santtu Pajukanta japsu

View GitHub Profile
@japsu
japsu / mkxtemp.py
Created February 28, 2012 09:01
mkxtemp - a temporary file context manager for use by external processes that need temp files
#!/usr/bin/env python
# Mkxtemp - a temporary file context manager for use by external processes
# Placed in the Public Domain by Santtu Pajukanta <santtu@pajukanta.fi> 2012
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
import os
import unittest
@japsu
japsu / defaultdict2.py
Created March 1, 2012 07:57
defaultdict2 - a defaultdict whose default factory is called with the key
from collections import defaultdict2
from unittest import TestCase
class defaultdict2(defaultdict):
def __missing__(self, key):
value = self.default_factory(key)
self[key] = value
return value
class TestDefaultdict2(TestCase):
@japsu
japsu / gist:2146116
Created March 21, 2012 10:47
Python stdout weirdness
japsu@eris ~ $ cat foo.py
#!/usr/bin/env python
# encoding: utf-8
import sys
sys.stdout.write(u"åöä\n")
japsu@eris ~ $ python foo.py
åöä
japsu@eris ~ $ python foo.py | cat
Traceback (most recent call last):
@japsu
japsu / python_to_java.py
Created April 25, 2012 07:14
Convert python_style_func_names to javaStyleFuncNames
#!/usr/bin/env python
def python_to_java(s):
t = list(s)
try:
i = 0
while True:
if t[i] == '_':
del t[i]
t[i] = t[i].upper()
@japsu
japsu / EXAMPLE
Created April 27, 2012 09:52
Phone number formatting and a minimal-boilerplate testing library
Traceback (most recent call last):
File "finphone.py", line 73, in <module>
assert verify.check(), verify.report()
AssertionError: FAILURE (passed 10, failed 15, total 25)
FAIL #1: split_area_code
INPUT: '035551234'
OUTPUT: ['', '035551234']
EXPECTED: ('03', '5551234')
@japsu
japsu / errorwrap.py
Created May 2, 2012 09:46
Wrap exceptions in a custom exception class
class errors_wrapped(object):
"""
A function decorator to wrap all exceptions in an exception class of
your choosing.
Usage:
@errors_wrapped(MyExceptionClass)
def my_function(...
MyExceptionClass should accept this:
To Zanarkand
Final Fantasy X
säv. Nobuo Uematsu
sov. Masashi Hamauzu
Aerith's Theme
Final Fantasy 7
säv. Nobuo Uematsu
sov. Shiro Hamaguchi
@japsu
japsu / fixavi.c
Created June 20, 2012 11:55
fix a broken Sony AnyCast DVPRO/PCM AVI file
/*
* fixavi.c - fix a broken Sony AnyCast DVPRO/PCM AVI file
* Written by Matti "Lumpio-" Virkkunen
*
* Compile with: cc -o fixavi fixavi.c (or just make fixavi)
*/
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
@japsu
japsu / gunicorn_django
Created July 6, 2012 07:27 — forked from xspager/gunicorn
Gunicorn_django init.d script (redhat/centos)
#!/bin/sh
### BEGIN INIT INFO
# Provides: gunicorn_django
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the gunicorn server
# Description: starts gunicorn using start-stop-daemon
KÄYTÖSSÄ
========
astarte - Samsung Galaxy Y Pro -kännykkä
celestis - Samsung Galaxy Tab 8.9
opux - verkkotulostin
enyo - imicuksen seuraaja, tiedostopalvelin isän nurkissa
eris - virtuaalikone primellä
helios - kotipalvelin
iteron - kytkin