timeit / cProfile function in Python

timeit 預設是執行程式片段 1,000,000 次,然後取平均時間(秒)

from timeit import timeit
import cProfile


def func1(n):
    array = [str(x * x) for x in xrange(n)]
    return ', '.join(array)


def func2(n):
    array = (str(x * x) for x in xrange(n))
    return ', '.join(array)


print timeit('func1(10)', 'from __main__ import func1')
print timeit('func2(10)', 'from __main__ import func2')

# 不像 timeit,cProfile 並不需要 import
cProfile.run('func1(10)')

ref:
http://www.cnblogs.com/moinmoin/archive/2011/03/18/python-runtime-measuring.html
http://www.codedata.com.tw/python/python-tutorial-the-6th-class-2-performance-pycon-taiwan/

豆瓣圖書 API

Oauth 2.0

http://developers.douban.com/wiki/?title=oauth2

權限不同,token 就會不同

Configuration

with python-social-auth

SOCIAL_AUTH_DOUBAN_OAUTH2_KEY = 'YOUR_KEY'
SOCIAL_AUTH_DOUBAN_OAUTH2_SECRET = 'YOUR_SECRET'
SOCIAL_AUTH_DOUBAN_OAUTH2_SCOPE = [
    'douban_basic_common,book_basic_r,book_basic_w'
]

Usage

http://developers.douban.com/wiki/?title=api_v2
http://developers.douban.com/wiki/?title=book_v2

headers = {
    'Authorization': 'Bearer YOUR_TOKEN',
}

for isbn in (item['isbn_13'], item['isbn_10']):
    book_isbn_url = 'https://api.douban.com/v2/book/isbn/%s' % (isbn)
    r = requests.get(book_isbn_url, headers=headers)
    if r.ok:
        book = json.loads(r.content)
        book_id = book['id']

        book_collect_url = 'https://api.douban.com/v2/book/%s/collection' % (book_id)
        data = {
            'status': item['status'],
        }
        if 'comment' in item:
            data['comment'] = item['comment']
        r2 = requests.post(book_collect_url, data=data, headers=headers)
        if not r2.ok:
            if r2.status_code == 409:
                # 已收藏,略過
                pass
            else:
                log.msg('post fail: %s' % (book_name), level=log.ERROR)
                log.msg('post fail: %s' % (r2.status_code), level=log.ERROR)

        break
    else:
        log.msg('isbn not found: %s' % (book_name), level=log.ERROR)
        log.msg('isbn not found: %s' % (isbn), level=log.ERROR)

Compile nginx with sticky-module on Ubuntu 12.04

# 先移除用 apt-get 安裝的 nginx
$ sudo apt-get autoremove nginx

開始編譯

$ sudo su root

$ cd /usr/src/
$ apt-get source nginx

$ cd nginx-1.6.0/debian/modules
$ wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/1.2.5.tar.gz
$ tar zxvf 1.2.5.tar.gz
$ mv nginx-goodies-nginx-sticky-module-ng-bd312d586752/ nginx-goodies-nginx-sticky-module-ng/

$ vim /usr/src/nginx-1.6.0/debian/rules
# 在 full_configure_flags 底下加上:
# --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module \
# --add-module=$(MODULESDIR)/nginx-goodies-nginx-sticky-module-ng

$ cd /usr/src/nginx-1.6.0/
$ aptitude build-dep nginx
$ aptitude install liblua5.1-0-dev init-system-helpers
$ dpkg-buildpackage -b

$ cd /usr/src
$ dpkg -i nginx-common_1.6.0-1+precise0_all.deb
$ dpkg -i nginx-full_1.6.0-1+precise0_amd64.deb

# 驗證一下
$ nginx -V

ref:
http://gravitronic.com/compiling-the-nginx-sticky-session-module-in-ubuntu/