Skip to content

Instantly share code, notes, and snippets.

@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: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 / 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() に加わった。
### これを使うと、ビューメソッド用の独自デコレータが定義できる。
import unittest
from oktest import ok, test
class FooTest(unittest.TestCase):
@test("'日本語'")
def _(self):
msg = "日本語"
ok (msg) == "にほんご"
require 'erb'
sql_template = <<END
SELECT
FROM users
WHERE true
% if gender
AND gender = :gender
% end
% if age_min
@kwatch
kwatch / gist:11360274
Created April 28, 2014 02:13
[Q] Is it possible to rename column1, column2, ... into arbitrary names in VALUES statement?
-- I want to rename column1 and column2 into id and name
psql=> values (101, 'Steve'), (102, 'Bill');
column1 | column2
---------+---------
101 | Steve
102 | Bill
(2 rows)
@kwatch
kwatch / gist:669a90687b3311927612
Created May 9, 2014 03:29
Upsert on PostgreSQL
/* ref: http://lets.postgresql.jp/documents/technical/9.1/1 */
create table members (
id serial primary key
, name varchar(255) not null unique
, gender char(1) not null -- 'F': female, 'M': male
, role varchar(255)
);
-- Run 'update' statement, or run 'insert' statement when failed.
@kwatch
kwatch / gist:02b1a5a8899b67df2623
Last active September 14, 2024 13:52
Example to support 'geometry' type (on PostgreSQL) in SQLAlchemy
from sqlalchemy import func
from sqlalchemy.types import UserDefinedType, Float
class EasyGeometry(UserDefinedType):
def get_col_spec(self):
return "GEOMETRY"
def bind_expression(self, bindvalue):
@kwatch
kwatch / gist:699dd6361cfbdf135496
Last active August 29, 2015 14:02
Python2.7のファイル容量を95MBから42MBへと半分以下に減らしてみた
### Pythonのtestモジュールを消すと、ファイル容量が28MB減った
$ du -sk python/2.7.7
95664 python/2.7.7
$ du -sk python/2.7.7/lib/python2.7/test
28412 python/2.7.7/lib/python2.7/test
$ rm -rf python/2.7.7/lib/python2.7/test/[a-zA-Z1-9]*
$ du -sk python/2.7.7
67264 python/2.7.7
$ ruby -e 'p 95664-67264'
28400
@kwatch
kwatch / gist:20baef0af58f8de4c694
Last active August 29, 2015 14:02
他の人のブランチをマージしたら、マイグレーションがコンフリクトしちゃった!どうしたらいいの? (Migr8.rb編)
## たとえば history.txt の内容がこういう履歴になっていたとして、
bcgf7647 # [john] create 'users' table
fdon4243 # [john] add 'birthday' column
## alice が 2 件のマイグレーションを追加したとします
bcgf7647 # [john] create 'users' table
fdon4243 # [john] add 'birthday' column
ycii2472 # [alice] change 'birthday' column as not-null