Skip to content

Instantly share code, notes, and snippets.

@yatt
yatt / zipfile_extended_damemoji_for_py26.py
Created March 25, 2011 22:14
ZipInfo holds filename as bytes. this causes mojibake for zip has sjis encoded string. 2nd byte position of dame-moji is normalized from \ to / incorrectly. this module intercept assignment to hold filename as unicode and handle zip better.
# extended zipfile module to handle sjis damemoji well.
from zipfile import *
def cp932_invert(cp932_path):
from string import printable
def fun(i):
pred = i > 0 and cp932_path[i] == '/' and cp932_path[i-1] not in printable
return '\\' if pred else cp932_path[i]
lst = map(fun, range(len(cp932_path)))
uni = ''.join(lst).decode('cp932').replace('\\', '/')
@yatt
yatt / tohoku-univ-radiation-monitoring-info
Created April 10, 2011 10:04
東北大学の「福島第一原子力発電所事故に係る放射線モニタリング情報」http://www.bureau.tohoku.ac.jp/anzen/monitoring/から情報を取得します。
# coding: utf-8
import urllib
import decimal
import re
import time
from BeautifulSoup import BeautifulSoup as BS
class SievertPerHour(object):
def __init__(self, sv):
if isinstance(sv, decimal.Decimal):
@yatt
yatt / tesseract-ocr-class.cs
Created April 12, 2011 12:49
simple c# class for Optical Character Recognition(OCR) using tesseract (http://code.google.com/p/tesseract-ocr/) usage: pass .exe path to constructor
// usage:
//
// TesseractOCR ocr = TesseractOCR(@"C:\bin\tesseract.exe");
// string result = ocr.OCRFromBitmap(bmp);
// textBox1.Text = result;
//
using System;
using System.IO;
using System.Diagnostics;
using System.Drawing;
@yatt
yatt / bitmap-grayscale-converter.cs
Created April 13, 2011 12:12
extension method definition for converting image to grayscale
using System.Drawing;
using System.Drawing.Imaging;
namespace BitmapConversion
{
static class GrayScale
{
public static Bitmap Transform(Bitmap bmp, float[][] matrix)
{
ColorMatrix mat = new ColorMatrix(matrix);
@yatt
yatt / bfinterpreter.py
Created April 15, 2011 03:49
brainf*ck interpreter
from sys import stdin, stdout
# state
class Int(object):
def __init__(self, v=0): self.val = v
def set(self, v): self.val = v; return self.val
def inc(self): self.val += 1; return self.val
def dec(self): self.val -= 1; return self.val
mem = [Int() for i in range(255)]
stk = []
ptr = Int()
@yatt
yatt / one-dimensional-cellular-automaton.py
Created April 15, 2011 13:38
an algorithm implementation of simple dimentional cellular automaton
# coding: utf-8
# one dimensional cell automaton
import random
class Cell1Dim(object):
def __init__(self, rule, initializer):
# buffer
self.fore = initializer
self.back = [0] * len(initializer)
@yatt
yatt / stream-processing.c
Created May 7, 2011 05:03
stream processing with pthread
// http://archive.linux.or.jp/JM/html/glibc-linuxthreads/man3/pthread_cond_wait.3.html
// http://d.hatena.ne.jp/mononoco/20080522/1211452506
// http://codezine.jp/article/detail/1894?p=2
#include<stdio.h>
#include<pthread.h>
typedef unsigned char byte_t;
typedef struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
@yatt
yatt / gcj2011-qualification-round-sources.py
Created May 9, 2011 03:31
get google code jam 2011 qualification round sources
# coding: utf-8
import os
import urllib
import time
import json
import StringIO
import zipfile
# google code jam 2011 qualification round
@yatt
yatt / .vimrc
Created May 10, 2011 09:49
zencoding-vim user setting for google ajax library api
let g:user_zen_settings = {
\ 'html' : {
\ 'indentation' : ' ',
\ 'snippets' : {
\ 'gapi:jquery' : '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>',
\ 'gapi:jqueryui' : '<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>',
\ }
\ }
\}
@yatt
yatt / typecheck.py
Created June 20, 2011 16:38
django parameter type decorator
#!/usr/bin/python
# coding: utf-8
# ref: http://stackoverflow.com/questions/582056/getting-list-of-parameters-inside-python-function
import inspect
def base(name, ptype, required):
def deco(fun): def gun(*args, **kwargs): frame = inspect.currentframe() _, _, _, values = inspect.getargvalues(frame) try: if required: values[name] = ptype(values[name])
except Exception, e:
raise e
return fun(*args, **kwargs)