用 Microsoft Translator API 翻譯 properties 檔案

因為另外一套要錢,所以來用 Microsoft Translator API

#!/usr/bin/env python

# coding: utf-8

import urllib
import sys
import xml.dom.minidom

# Get Bing AppID from https://ssl.bing.com/webmaster/developers/appids.aspx
BING_APPID = 'KERO~'

FILE_FROM = 'lang.properties'
FILE_TO = 'translated.properties'

LANG_FROM = 'en'
LANG_TO = 'zh-chs'

def translate(text, from_lang, to_lang):
    base_url = 'http://api.microsofttranslator.com/v2/Http.svc/Translate?'
    data = urllib.urlencode({'appId':BING_APPID,
                             'from': from_lang.encode('utf-8'),
                             'to': to_lang.encode('utf-8'),
                             'text': text.encode('utf-8')
                            })

    url = base_url + data
    response = urllib.urlopen(url).read()

    dom = xml.dom.minidom.parseString(response)
    result = dom.documentElement.childNodes[0].nodeValue

    return result.encode('utf-8')

def parse_properties(filename):
    langs = {}
    with open(filename, 'r') as f:
        lines = [ line.strip() for line in f.readlines() ]
        for line in lines:
            idx = line.find('=')
            if idx == -1:
                continue

            (key, val) = (line[:idx], line[idx+1:])
            if not key:
                continue

            langs[key] = val

    return langs

def main():
    langs = parse_properties(FILE_FROM)
    translated = { k: translate(v, LANG_FROM, LANG_TO) if v else ""
                   for k,v in langs.items() }

    with open(FILE_TO, 'w+') as f:
        for k,v in sorted(translated.items()):
            f.write("%s=%s\n" % (k,v))

if __name__ == '__main__':
    main()

隨手寫寫,一年沒寫文章,也許有點刻意,差不多該是改變的時候了。

Use VERSIONER_PYTHON_PREFER_32_BIT=yes in Snow Leopard

In python, you can use ctype (dl is deprecated now) to load dynamic link library. In 10.4 and 10.5 it may work fine, but it may occur errors in 10.6, such as

/Library/Frameworks/dummy.framework/dummy: no matching architecture in universal wrapper

Since the python in Snow Leopard is 64-bit in default, for those libraries which does not support 64-bit. You can test your library by

% file /Library/Frameworks/dummy.framework/Versions/Current/dummy

If you library is i386 only, you may need to handle it in this way.

% export VERSIONER_PYTHON_PREFER_32_BIT=yes
% python your_script.py

ericsk has also mentioned this for wxPython in his plurk

For more information, you can read the man page in your Snow Leopard.

Using minimock for Python unit testing

我也忘了什麼時候開始習慣作 unit testing 了,可能是前一個專案在 Xcode 上就開始學著寫一點測試了。

這次在閱讀 Writing Testable Code 對於 6. Static methods: (or living in a procedural world),格外有感覺。

尤其是修改別人的程式碼的時候,如果他總是直接呼叫 static method,不讓你用 mock object 把 implementation 換掉的話,這樣在測試起來就顯得麻煩的多。舉例來說,有一個 class method 會呼叫 SSHHelper.execute_ssh 這個指令去遠端執行一些東西,但這對 unit testing 而言就是一種外部的 dependency,你絕對不希望他真的在測試的時候跑出個 ssh 真的連過去執行些什麼吧。

剛好這次用 MiniMock 作我們的 mock library,因為他的因素,你就可以這樣作

minimock.mock('SSHHelper.execute_ssh', returns=ssh_output)

如此一來,execute_ssh 這個 method 的結果就可以簡單的換成你預期的 ssh_output。

如果是本來就有比較好的設計的話,你也可以用 minimock.Mock (注意大小寫),去產生一個 mock object 出來。

mock_fs = minimock.Mock('MockFileSystem')
mock_fs.size.mock_returns = 1
mytools.fs_imp = mock_fs

如此一來 mock 就顯得簡單多了……

話說回來,之前想寫的 objective-c 跟 DTrace 的 topic 真的都忘的差不多了,算了,以後再說吧,也許之後把 python 的 Exception Chaining and Embedded Tracebacks 的想法整理一下再丟出來吧。

The global statement in python

寫 code 時候踩到地雷,果然沒唸好 scope 果然是不行的

#!/usr/bin/env python

flag = True

def test():
    flag = False

print flag
test()
print flag

結果似乎和想的不太一樣。


# python 2.py
True
True

結果翻了一下 The global statement 才知道

It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

vim 裡的 python autocomplete

pydiction

http://www.vim.org/scripts/script.php?script_id=850 的東西抓回來丟到 .vim 下面,然後按照 readme 的作,之後按 ctrl+n 或者 ctrl+p 就可以看到漂亮的下拉選單了。

強者我同學 Pky 應該會叫我去 http://wiki.python.org/moin/IntegratedDevelopmentEnvironments 找個好用的吧,不過我就是喜歡用 vim 啦~~

Python: range() 與 xrange()

http://docs.python.org/lib/built-in-funcs.html

This function is very similar to range(), but returns an “xrange object” instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range’s elements are never used (such as when the loop is usually terminated with break).

三月,多話的一個月

py-dbus

三月,這的確是我特別多話的一個月份,這個 blog 從來沒有那麼頻繁的被更新。有些朋友知道我的個性,喜歡一個人做事,就悶著頭也好,一個人看場電影。並不是不多話,大多時候只是不太懂得如何與人相處而已。所以也有人知道,當我多話的時候,那些時候卻也剛好是是我的情緒低落的時候。

我也不知道這樣的事情是不是很正確,只是在與人合作上我的確不是那麼得心應手吧!

這個月份看了 Match Point,總而言之這個月的心情有點像柯裕棻在《甜美的剎那》所寫的

心情像伍迪艾倫電影的開場與結束時的味道,有點事不關己,卻又是真心真意的。

然後這半年來的偏頭痛常常惹得我精神疲勞,去看醫生也總是得到一樣的回答「這是典型的偏頭痛」,開了止痛藥還有一種血管擴張藥給我。弄的我不知道是精神耗弱還是怎麼的,常常有思緒無法連貫,前一刻想到的東西,下一秒就有種 Déjà vu 的感受。也有朋友說這種感受是腦傷的前兆,並不像法文發音那樣的浪漫。我只是笑了笑,就這樣回應他了。

工作兩個半月了,我到底走在什麼樣的路上,是偏離還是筆直,老實說我一點也不知道了。