This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'erb' | |
sql_template = <<END | |
SELECT | |
FROM users | |
WHERE true | |
% if gender | |
AND gender = :gender | |
% end | |
% if age_min |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from oktest import ok, test | |
class FooTest(unittest.TestCase): | |
@test("'日本語'") | |
def _(self): | |
msg = "日本語" | |
ok (msg) == "にほんご" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
### | |
### New feature in Pyramid 1.4: '_depth' argument for view_config() | |
### | |
### New argument '_depth' of view_config() in Pyramid 1.4 allows you | |
### to define custome decorator for view methods. | |
### | |
### '_depth' という新しい引数が Pyramid 1.4 から view_cnfig() に加わった。 | |
### これを使うと、ビューメソッド用の独自デコレータが定義できる。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query2sql(query): | |
"""convert query object into non-prepared sql string in SQLAlchemy and psycopg2""" | |
compiler = query.statement.compile() | |
params = compiler.params | |
prepared_sql = compiler.string # or str(compiler) | |
psycopg2_cursor = query.session.connection().connection.cursor() | |
sql = psycopg2_cursor.mogrify(prepared_sql, params) | |
return sql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Facebook APIが失敗したときのエラー | |
{ | |
"error": { | |
"message": "Error validating access token: Session has expired at unix time 1359522000. The current unix time is 1359527917.", | |
"type": "OAuthException", | |
"code": 190, | |
"error_subcode": 463 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## モデルクラスから Query クラス (or Collection クラス) を分離すれば、 | |
## 黒魔術を一切使わなくても named_scope が実現できることを示すサンプル | |
class ORM::Entity | |
end | |
class ORM::Query # or ORM::Collection | |
def where(*args) ... end | |
def order_by(*args) ... end | |
def limit(n) ... end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from pyramid.view import view_config | |
from venusian import ATTACH_ATTR # view_config() uses 'venusian' module internally | |
## | |
## utility | |
## | |
def view_method(fn, **kwargs): | |
# get frame of class definition |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
### Decorator for view function works very well. | |
### | |
def GET(route_name, template): | |
def deco(func): | |
@view_config(route_name=route_name, request_method='GET', | |
renderer='templates/'+template) | |
def fn(request): | |
# ... do something here ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
## | |
## define AssertionObject class | |
## | |
use strict; | |
use warnings; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
## | |
## add at_end() which registers blocks called end of test. | |
## | |
use strict; | |
use warnings; | |