Skip to content

Instantly share code, notes, and snippets.

require 'erb'
sql_template = <<END
SELECT
FROM users
WHERE true
% if gender
AND gender = :gender
% end
% if age_min
import unittest
from oktest import ok, test
class FooTest(unittest.TestCase):
@test("'日本語'")
def _(self):
msg = "日本語"
ok (msg) == "にほんご"
@kwatch
kwatch / views.py
Created August 21, 2013 09:11
New feature in Pyramid 1.4: '_depth' argument for view_config(). See https://gist.github.com/kwatch/4204380 for Pyramid 1.3.
# 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() に加わった。
### これを使うと、ビューメソッド用の独自デコレータが定義できる。
@kwatch
kwatch / gist:4672421
Last active December 25, 2019 14:51
How to convert query object into non-prepared sql string in SQLAlchemy and psycopg2
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
@kwatch
kwatch / gist:4671240
Created January 30, 2013 06:41
各種WebAPI失敗時にどのようなレスポンスが返ってくるか
// 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
}
}
@kwatch
kwatch / gist:4586127
Created January 21, 2013 13:40
モデルクラスから Query クラス (or Collection クラス) を分離すれば、黒魔術を一切使わなくても named_scope が実現できることを示すサンプル
## モデルクラスから 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
@kwatch
kwatch / gist:4204380
Last active November 17, 2015 16:17
Custom Decorator for View Method in Pyramid 1.3
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
@kwatch
kwatch / gist:4200068
Created December 4, 2012 02:45
Function Decorator v.s. Method Decorator in Pyramid Framework
###
### 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 ...
@kwatch
kwatch / oktest7.pl
Created September 28, 2012 05:22
define AssertionObject class
# -*- coding: utf-8 -*-
##
## define AssertionObject class
##
use strict;
use warnings;
@kwatch
kwatch / oktest6.pl
Created September 28, 2012 05:16
add at_end() which registers blocks called end of test.
# -*- coding: utf-8 -*-
##
## add at_end() which registers blocks called end of test.
##
use strict;
use warnings;