<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>笨頭哥的沒營養文章集 &#187; Python</title>
	<atom:link href="http://blog.hubert.tw/category/programming/python-programming-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hubert.tw</link>
	<description>Hubert&#039;s Blog</description>
	<lastBuildDate>Wed, 11 Jan 2012 16:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>用 Microsoft Translator API 翻譯 properties 檔案</title>
		<link>http://blog.hubert.tw/2012/01/11/%e7%94%a8-microsoft-translatorapi-%e7%bf%bb%e8%ad%af-properties-%e6%aa%94%e6%a1%88/</link>
		<comments>http://blog.hubert.tw/2012/01/11/%e7%94%a8-microsoft-translatorapi-%e7%bf%bb%e8%ad%af-properties-%e6%aa%94%e6%a1%88/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 03:47:03 +0000</pubDate>
		<dc:creator>Hubert</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Microsoft Translator API]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.hubert.tw/?p=600</guid>
		<description><![CDATA[因為另外一套要錢，所以來用 Microsoft Translator API 隨手寫寫，一年沒寫文章，也許有點刻意，差不多該是改變的時候了。]]></description>
			<content:encoded><![CDATA[<p>因為另外一套要錢，所以來用 <a href="http://www.microsofttranslator.com/dev/">Microsoft Translator API</a></p>
<pre class="brush: python; title: ; notranslate">
#!/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 &quot;&quot;
                   for k,v in langs.items() }

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

if __name__ == '__main__':
    main()
</pre>
<p>隨手寫寫，一年沒寫文章，也許有點刻意，差不多該是改變的時候了。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hubert.tw/2012/01/11/%e7%94%a8-microsoft-translatorapi-%e7%bf%bb%e8%ad%af-properties-%e6%aa%94%e6%a1%88/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use VERSIONER_PYTHON_PREFER_32_BIT=yes in Snow Leopard</title>
		<link>http://blog.hubert.tw/2010/03/04/use-versioner_python_prefer_32_bityes-in-snow-leopard/</link>
		<comments>http://blog.hubert.tw/2010/03/04/use-versioner_python_prefer_32_bityes-in-snow-leopard/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 08:21:00 +0000</pubDate>
		<dc:creator>Hubert</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA["Snow Leopard"]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[VERSIONER_PYTHON_PREFER_32_BIT]]></category>

		<guid isPermaLink="false">http://blog.hubert.tw/?p=574</guid>
		<description><![CDATA[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 &#8230; <a href="http://blog.hubert.tw/2010/03/04/use-versioner_python_prefer_32_bityes-in-snow-leopard/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In python, you can use <a href="http://docs.python.org/library/ctypes.html">ctype</a> (<a href="http://docs.python.org/library/dl.html">dl</a> 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</p>
<blockquote><p>
/Library/Frameworks/dummy.framework/dummy: no matching architecture in universal wrapper
</p></blockquote>
<p>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</p>
<blockquote><p>% file /Library/Frameworks/dummy.framework/Versions/Current/dummy
</p></blockquote>
<p>If you library is i386 only, you may need to handle it in this way.</p>
<blockquote><p>
% export VERSIONER_PYTHON_PREFER_32_BIT=yes<br />
% python your_script.py
</p></blockquote>
<p><a href="http://blog.ericsk.org/">ericsk</a> has also mentioned this for <a href="http://www.wxpython.org/">wxPython</a> in <a href="http://www.plurk.com/p/3zg4c0">his plurk</a></p>
<p>For more information, you can read the man page in your Snow Leopard.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hubert.tw/2010/03/04/use-versioner_python_prefer_32_bityes-in-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using minimock for Python unit testing</title>
		<link>http://blog.hubert.tw/2010/02/24/using-minimock-for-python-unit-testing/</link>
		<comments>http://blog.hubert.tw/2010/02/24/using-minimock-for-python-unit-testing/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 02:44:43 +0000</pubDate>
		<dc:creator>Hubert</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA["unit test"]]></category>
		<category><![CDATA[minimock]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.hubert.tw/?p=568</guid>
		<description><![CDATA[我也忘了什麼時候開始習慣作 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 真的連過去執行些什麼吧。 剛好這次用 &#8230; <a href="http://blog.hubert.tw/2010/02/24/using-minimock-for-python-unit-testing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>我也忘了什麼時候開始習慣作 unit testing 了，可能是前一個專案在 Xcode 上就開始學著寫一點測試了。</p>
<p>這次在閱讀 <a href="http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-decided-to.html">Writing Testable Code</a> 對於 6. Static methods: (or living in a procedural world)，格外有感覺。</p>
<p>尤其是修改別人的程式碼的時候，如果他總是直接呼叫 static method，不讓你用 mock object 把 implementation 換掉的話，這樣在測試起來就顯得麻煩的多。舉例來說，有一個 class method 會呼叫 SSHHelper.execute_ssh 這個指令去遠端執行一些東西，但這對 unit testing 而言就是一種外部的 dependency，你絕對不希望他真的在測試的時候跑出個 ssh 真的連過去執行些什麼吧。</p>
<p>剛好這次用 <a href="http://pypi.python.org/pypi/MiniMock">MiniMock</a> 作我們的 mock library，因為他的因素，你就可以這樣作</p>
<pre class="brush: python; title: ; notranslate">
minimock.mock('SSHHelper.execute_ssh', returns=ssh_output)
</pre>
<p>如此一來，execute_ssh 這個 method 的結果就可以簡單的換成你預期的 ssh_output。</p>
<p>如果是本來就有比較好的設計的話，你也可以用 minimock.Mock (注意大小寫)，去產生一個 mock object 出來。</p>
<pre class="brush: python; title: ; notranslate">
mock_fs = minimock.Mock('MockFileSystem')
mock_fs.size.mock_returns = 1
mytools.fs_imp = mock_fs
</pre>
<p>如此一來 mock 就顯得簡單多了……</p>
<p>話說回來，之前想寫的 objective-c 跟 DTrace 的 topic 真的都忘的差不多了，算了，以後再說吧，也許之後把 python 的 <a href="http://www.python.org/dev/peps/pep-3134/">Exception Chaining and Embedded Tracebacks</a> 的想法整理一下再丟出來吧。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hubert.tw/2010/02/24/using-minimock-for-python-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The global statement in python</title>
		<link>http://blog.hubert.tw/2008/05/05/the-global-statement-in-python/</link>
		<comments>http://blog.hubert.tw/2008/05/05/the-global-statement-in-python/#comments</comments>
		<pubDate>Mon, 05 May 2008 06:48:02 +0000</pubDate>
		<dc:creator>Hubert</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[global]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://blog.hubert.tw/?p=255</guid>
		<description><![CDATA[寫 code 時候踩到地雷，果然沒唸好 scope 果然是不行的 結果似乎和想的不太一樣。 # 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.]]></description>
			<content:encoded><![CDATA[<p>寫 code 時候踩到地雷，果然沒唸好 scope 果然是不行的</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python

flag = True

def test():
    flag = False

print flag
test()
print flag
</pre>
<p>結果似乎和想的不太一樣。</p>
<p><code><br />
# python 2.py<br />
True<br />
True<br />
</code></p>
<p>結果翻了一下 <a href="http://docs.python.org/ref/global.html">The global statement</a> 才知道</p>
<blockquote><p>
It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.hubert.tw/2008/05/05/the-global-statement-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>vim 裡的 python autocomplete</title>
		<link>http://blog.hubert.tw/2008/04/15/vim-%e8%a3%a1%e7%9a%84-python-autocomplete/</link>
		<comments>http://blog.hubert.tw/2008/04/15/vim-%e8%a3%a1%e7%9a%84-python-autocomplete/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 07:51:46 +0000</pubDate>
		<dc:creator>Hubert</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[pydiction]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://blog.hubert.tw/?p=249</guid>
		<description><![CDATA[把 http://www.vim.org/scripts/script.php?script_id=850 的東西抓回來丟到 .vim 下面，然後按照 readme 的作，之後按 ctrl+n 或者 ctrl+p 就可以看到漂亮的下拉選單了。 強者我同學 Pky 應該會叫我去 http://wiki.python.org/moin/IntegratedDevelopmentEnvironments 找個好用的吧，不過我就是喜歡用 vim 啦～～]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/hub19/2415866136/"><img src="http://farm3.static.flickr.com/2188/2415866136_81a81acb3b.jpg?v=0" alt="pydiction" /></a></p>
<p>把 <a href="http://www.vim.org/scripts/script.php?script_id=850">http://www.vim.org/scripts/script.php?script_id=850</a> 的東西抓回來丟到 .vim 下面，然後按照 readme 的作，之後按 ctrl+n 或者 ctrl+p 就可以看到漂亮的下拉選單了。</p>
<p>強者我同學 <a href="http://blog.yam.com/pkyosx0307">Pky</a> 應該會叫我去 <a href="http://wiki.python.org/moin/IntegratedDevelopmentEnvironments">http://wiki.python.org/moin/IntegratedDevelopmentEnvironments</a> 找個好用的吧，不過我就是喜歡用 vim 啦～～</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hubert.tw/2008/04/15/vim-%e8%a3%a1%e7%9a%84-python-autocomplete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: range() 與 xrange()</title>
		<link>http://blog.hubert.tw/2008/04/04/python-range-%e8%88%87-xrange/</link>
		<comments>http://blog.hubert.tw/2008/04/04/python-range-%e8%88%87-xrange/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 03:06:02 +0000</pubDate>
		<dc:creator>Hubert</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.hubert.tw/?p=245</guid>
		<description><![CDATA[http://docs.python.org/lib/built-in-funcs.html This function is very similar to range(), but returns an &#8220;xrange object&#8221; 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 &#8230; <a href="http://blog.hubert.tw/2008/04/04/python-range-%e8%88%87-xrange/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://docs.python.org/lib/built-in-funcs.html">http://docs.python.org/lib/built-in-funcs.html</a></p>
<blockquote><p>
This function is very similar to range(), but returns an &#8220;xrange object&#8221; 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&#8217;s elements are never used (such as when the loop is usually terminated with break).
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.hubert.tw/2008/04/04/python-range-%e8%88%87-xrange/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

