Skip to content

Instantly share code, notes, and snippets.

@kwatch
kwatch / run-cdnjs.rb
Created July 3, 2014 10:02
CDNJS: latest version of twitter-bootstrap is expected as 3.2.0, but got 3.1.1 with API.
# -*- coding: utf-8 -*-
##
## latest version of twitter-bootstrap is expected as 3.2.0 but got 3.1.1 with API.
##
require 'open-uri'
require 'json'
libname = 'twitter-bootstrap'
@kwatch
kwatch / cdnjs-dl.rb
Created July 3, 2014 09:13
CDNJSからライブラリをダウンロードしてくるRubyスクリプト
# -*- coding: utf-8 -*-
require 'open-uri'
require 'fileutils'
require 'json'
CDNJS_URL = "http://api.cdnjs.com/libraries?search={keyword}&fields=assets"
#
libname = 'angular.js'
@kwatch
kwatch / CHANGES_Oktest-0.15.0.rst
Last active August 29, 2015 14:03
Oktest 0.15.0 変更点
  • [enhance] oktest.web.WSGITestクラスが、マルチパート形式をサポート ex:

    ## マルチパート形式のデータを作成
    from oktest.web import MultiPart
    mp = MultiPart()    # or boundary='abcdef'; mp = MutliPart(boundary)
    mp.add("name1", "value1")          # add string value
    with open("logo.png", 'wb') as f:  # add file value
        mp.add("file1", f.read(), "logo.png", "image/png")
    ## マルチパートデータを指定してリクエストを投げる
    
@kwatch
kwatch / example.py
Last active August 29, 2015 14:03
[python] Is it impossible to override __init__() method of classes implemented in C?
from datetime import datetime
class Foo(datetime):
def __init__(self):
datetime.__init__(self, 2014, 7, 1, 0, 0, 0)
obj = Foo()
### result:
#
@kwatch
kwatch / gist:9a26421386d39c7c2780
Created June 25, 2014 05:43
要件:条件に一致するレコードが1件もない場合は、デフォルト値として 0 を使いたい
--
-- 要件:条件に一致するレコードが1件もない場合は、デフォルト値として 0 を使いたい
--
-- 実験1: 通常は、条件に一致するレコードが1件もない場合は 0 rows になる
psql=> select point::integer
from user_point_history
where user_id = 12595 and created_at > '2014-06-01';
point
@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
@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: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: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: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)