Skip to content

Instantly share code, notes, and snippets.

#! /usr/bin/env python
import cmd
class MyShell(cmd.Cmd):
def precmd(self, line):
lines = line.split(';')
self.cmdqueue.extend(lines[1:])
return lines[0]
@yanolab
yanolab / mutistarmap.py
Last active December 10, 2015 13:19
compatible with over python2.6
#! -*- coding: utf-8 -*-
from multiprocessing import Pool
class Worker(object):
def __init__(self, func, *args):
self.func = func
self.args = args
def __call__(self, *args, **kw):
@yanolab
yanolab / cal.rb
Created December 7, 2012 12:38
print this month calendar
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
# This program works with both 1.8.7 and 1.9.3
#
# usage:
# ruby cal.rb [month] [year]
# month and year are optional
require "date"
@yanolab
yanolab / codeiq-answer_in_pyconp2012.py
Created September 15, 2012 08:04
pyconjpで出題されたcodeiqの問題を解いてみた。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import lxml.html
from collections import Counter
doc = lxml.html.parse(open("cache.html"))
users = [x.text.upper() for x in doc.xpath("//div[@class='side_sec_box side_article_area']/div/p[@class='user']/a")[:400]]
print "".join(map(lambda x: x[0], Counter("".join(users)).most_common(5)))
@yanolab
yanolab / countdown.py
Created September 4, 2012 06:13
connpassからpyconjpの定員を取得して残数をツイートするスクリプト
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import re
import os
import sys
import lxml.html
import twitter
api = twitter.Api(
@yanolab
yanolab / .bashrc
Created July 23, 2012 09:34
display branch name and commit status of git repository
# http://lukasrieder.com/2009/07/14/extend-your-bash-ps1.html
function parse_git_dirty_and_branch() {
branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
[[ $branch != '' ]] && echo "($(parse_git_dirty)$branch)"
}
# set a fancy prompt (non-color, unless we know we "want" color)
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
@yanolab
yanolab / argguard.py
Created April 17, 2012 11:08
argment type guard
# -*- coding: utf-8 -*-
import operator
def guard(*guardArgs, **guardKw):
def _argGuard(f):
def _inner(*args, **kw):
argTypes = map(lambda x: type(x), args)
if not all(map(operator.eq, guardArgs, argTypes)):
raise Exception("Type Error: not match type {0}, {1}".format(guardArgs, argTypes))
@yanolab
yanolab / benchmark.txt
Created December 20, 2011 09:46
benchmark functional programming pypy1.6 - 1.7 and python2.7.1
Python2.7.1
==================
In [1]: import sys
In [2]: sys.version
Out[2]: '2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) \n[GCC 4.5.2]'
In [3]: import p002, p002_functional
In [4]: timeit p002.sumevenfibo(4000000)
@yanolab
yanolab / pypypcre.py
Created December 19, 2011 09:11
pcre for pypy
""" libpcre wrapping"""
from pypy.rpython.tool import rffi_platform
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.translator.tool.cbuild import ExternalCompilationInfo
from pypy.translator.platform import platform
from pypy.tool.ansi_print import ansi_log
import py
import os
@yanolab
yanolab / fizzbuzz.py
Created December 15, 2011 03:53
fizzbuzz
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def main(maxnum):
for idx in xrange(1, maxnum+1):
val = idx
if idx % 3 == 0: