用 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.

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).