Skip to content

Instantly share code, notes, and snippets.

@kwatch
kwatch / gist:3154226
Created July 21, 2012 01:52
DSL for routing
route "/users/", :GET=>:index, :POST=>:create
route "/users/new", :GET=>:new
route "/users/:id", :GET=>:show, :PUT=>:update, :DELETE=>:destroy
route "/users/:id/edit", :GET=>:edit
## or
url_path "/users" do
route "/", :GET=>:index, :POST=>:create
route "/new", :GET=>:new
@kwatch
kwatch / gist:3148115
Created July 20, 2012 01:35
Sequenceを作る関数をPythonとRubyで書いてみた
##
## Python
##
def seq(initial, formula):
x = initial
while True:
yield x
x = formula(x)
sequence = seq(1, lambda x: x+1)
@kwatch
kwatch / gist:2814940
Created May 27, 2012 16:18
Rubyの特殊変数一覧
$! raise された例外オブジェクト
$" require で読み込まれたファイルの配列
$#
$$ 現在のプロセス ID
$%
$& 正規表現にマッチした箇所の文字列
$' 正規表現にマッチした箇所より後ろの文字列
$(
$)
$* Ruby スクリプトに指定された引数。ARGV と同じ
@kwatch
kwatch / bench_getframe.py
Created May 27, 2012 04:15
benchmark script to measure cost of sys._getframe(1) and sys._getframe(1).f_locals
# -*- coding: utf-8 -*-
##
## benchmark script to measure cost of sys._getframe(1) and sys._getframe(1).f_locals
##
## How to run:
## $ easy_install benchmarker
## $ python bench_getframe.py -h
## $ python bench_getframe.py -n 1000000 -c 5 -X 1
##
@kwatch
kwatch / python_template_bench.py
Created May 18, 2012 23:46
Benchmark for Python Template Engine Libraries (Tenjin and Mako)
# -*- coding: utf-8 -*-
###
### benchmark of Tenjin and Mako
###
### usage:
### $ easy_install tenjin
### $ easy_install mako
### $ easy_install benchmarker
### $ python python_template_bench.py -h
@kwatch
kwatch / gist:2722877
Created May 18, 2012 02:53
setdefault() for any object
def setdefault(obj, name, value):
if hasattr(obj, name):
return getattr(obj, name)
else:
setattr(obj, name, value)
return value
@kwatch
kwatch / gist:2585260
Created May 3, 2012 12:13
simulate multi-indeces with tuple, without paren
##
## simulate multi-indeces with tuple, without paren
##
class Foo(object):
def __getitem__(self, index):
return index
if __name__ == '__main__':
@kwatch
kwatch / exception_summary.dbtmako
Created April 10, 2012 10:06
Alternative template file for pyramid_debugtoolbar to open errored file quickly
# -*- coding: utf-8 -*-
<%!
##
## This is an alternative template file for pyramid_debugtoolbar.
## Using this template, you can open errored file very quickly.
##
##
## Usage:
##
@kwatch
kwatch / gist:2342562
Created April 9, 2012 09:43
sqlalchemy.exc.InvalidRequestError: Table 'admin_users' is already defined for this MetaData instance.
$ pserve development.ini --reload
Starting subprocess with file monitor
/Users/kwatch/myproj1/local/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.4-x86_64.egg/sqlalchemy/ext/declarative.py:1336: SAWarning: The classname 'AdminUser' is already in the registry of this declarative base, mapped to <class 'smapo.common.models.base.AdminUser'>
_as_declarative(cls, classname, cls.__dict__)
Traceback (most recent call last):
File "/Users/kwatch/myproj1/local/bin/python2.7/pserve", line 9, in <module>
load_entry_point('pyramid==1.3b2', 'console_scripts', 'pserve')()
File "/Users/kwatch/myproj1/local/lib/python2.7/site-packages/pyramid-1.3b2-py2.7.egg/pyramid/scripts/pserve.py", line 47, in main
return command.run()
File "/Users/kwatch/myproj1/local/lib/python2.7/site-packages/pyramid-1.3b2-py2.7.egg/pyramid/scripts/pserve.py", line 290, in run
@view_config(route_name='user_new', request_method='GET', renderer='form.html')
def user_new(request):
# ...
@view_config(route_name='user_create', request_method='POST', renderer='form.html')
def user_crate(request):
# ...
...