<?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>#raynix# &#187; Python</title>
	<atom:link href="http://raynix.info/archives/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://raynix.info</link>
	<description>When others see chance, I see cause.</description>
	<lastBuildDate>Sat, 04 Sep 2010 07:51:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Unicode编码中的简繁中文互转</title>
		<link>http://raynix.info/archives/522</link>
		<comments>http://raynix.info/archives/522#comments</comments>
		<pubDate>Thu, 30 Apr 2009 08:51:22 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Language!]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Python & Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[中文]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=522</guid>
		<description><![CDATA[现在简繁中文的转换已经不像从前那样，是gbk/ big5两种文字编码之间的转换，而是在同一个编码，Unicode，当中不同编码的映射。当然，简繁中文绝对不是简单的一一对应关系，有一对多的个别情况，例如简体的“后” 对应繁体的 “后”和“後”。行业词汇也有差别，例如大陆说“宽带”，而港台说“寬頻”；大陆说“操作系统”，而港台说“作業系統” 等等。 因此当我决定用Python程序将森海塞尔简体中文网站转换为繁体中文网站时，我的计划分三个步骤： 提取行业高频关键字，找到简体和繁体的对应，编成词典 利用现有的Python中文本地化工具包，将关键字之外的文字转换 请港台Freelancer人工校对，确保本土化 第一步，我的词典替换程序在此： #coding:utf8 import sys _dict = {'森海塞尔': 'Sennheiser', '耳机': '耳筒', '通讯耳机': '通話用耳機', '话筒': '咪高峰', '包耳式': '包圍耳殼式', '换能原理': '單元原理', '阻抑': '隔離', '便携包': '便攜袋', '质保': '保用', '带宽': '頻寬', '调制方式': '調節方式', '信噪比': '訊噪比', '杂散辐射': '雜散發射' } _dict_s2t = dict((k, v) for k, v in _dict.iteritems()) _dict_t2s = dict((v, k) [...]]]></description>
			<content:encoded><![CDATA[<p>现在简繁中文的转换已经不像从前那样，是gbk/ big5两种文字编码之间的转换，而是在同一个编码，Unicode，当中不同编码的映射。当然，简繁中文绝对不是简单的一一对应关系，有一对多的个别情况，例如简体的“后” 对应繁体的 “后”和“後”。行业词汇也有差别，例如大陆说“宽带”，而港台说“寬頻”；大陆说“操作系统”，而港台说“作業系統” 等等。</p>
<p>因此当我决定用Python程序将<a href="http://www.sennheiser.com.cn" target="_blank">森海塞尔简体中文网站</a>转换为<a href="http://www.sennheiser.com.hk" target="_blank">繁体中文网站</a>时，我的计划分三个步骤：</p>
<ol>
<li>提取行业高频关键字，找到简体和繁体的对应，编成词典</li>
<li>利用现有的Python中文本地化工具包，将关键字之外的文字转换</li>
<li>请港台Freelancer人工校对，确保本土化</li>
</ol>
<p>第一步，我的词典替换程序在此：</p>
<pre>#coding:utf8
import sys

_dict = {'森海塞尔': 'Sennheiser', '耳机': '耳筒',  '通讯耳机': '通話用耳機', '话筒': '咪高峰', '包耳式': '包圍耳殼式', '换能原理': '單元原理', '阻抑': '隔離',
'便携包': '便攜袋', '质保': '保用', '带宽': '頻寬', '调制方式': '調節方式', '信噪比': '訊噪比', '杂散辐射': '雜散發射' }

_dict_s2t = dict((k, v) for k, v in _dict.iteritems())
_dict_t2s = dict((v, k) for k, v in _dict.iteritems())

def translate2traditional(data):

    for k, v in _dict_s2t.iteritems():
        data = data.replace(k, v)
    return data

def translate2simplified(data):

    for k, v in _dict_t2s.iteritems():
        data = data.replace(k, v)
    return data

def test_dict():

    global _dict_s2t, _dict_t2s
    for k, v in _dict_s2t.iteritems():
        print k, v

    for k, v in _dict_t2s.iteritems():
        print k, v

if __name__ == '__main__':

    if len(sys.argv) &gt; 2:
        f = open(sys.argv[2], 'r')
        if sys.argv[1] == 's2t':
            print translate2traditional(f.read())
        elif sys.argv[1] == 't2s':
            print translate2simplified(f.read())
        else:
            print 'Wrong operation type.'
    else:
        print 'usage: python trans_dict.py [s2t | t2s] file.name'</pre>
<p>第二步，我利用的是<a href="http://code.google.com/p/pyzh/" target="_blank">pyzh中文工具包</a>中的fanjian.py完成。特别感谢pyzh项目组的全部成员和fanjian.py程序作者ne.manman和zsp007！</p>
<p>下面就交给香港的朋友了，希望不会累坏他:)</p>
<p>顺便说，Songbird很不错，有点山寨iTunes，那又怎样呢？毕竟Linux下面还没iTunes。</p>
<p><a href="http://getsongbird.com/" target="_blank"><img src="http://lh6.ggpht.com/_ZO8fi9o7nlg/SfbI1XAuY2I/AAAAAAAAA-4/Jh7xxolCcqk/s144/songbird.png" alt="" /><br />
Songbird</a></p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/522">Permalink</a> |
<a href="http://raynix.info/archives/522#comments">2 条评论</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/522&title=Unicode编码中的简繁中文互转">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a>, <a href="http://raynix.info/archives/tag/unicode" rel="tag">unicode</a>, <a href="http://raynix.info/archives/tag/%e4%b8%ad%e6%96%87" rel="tag">中文</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/522/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>从6.7到0.01</title>
		<link>http://raynix.info/archives/507</link>
		<comments>http://raynix.info/archives/507#comments</comments>
		<pubDate>Mon, 20 Apr 2009 09:21:36 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Python & Django]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=507</guid>
		<description><![CDATA[编程果然不能偷懒！我这一偷懒，CPU就不知道要受多少累、冒多少热气了。一开始，我采用了很无脑的穷举方法，复杂度是O(n**2)，结果肯定正确的，但是长度一万的数组就可以耗去6.7秒的CPU时间。 raymond@raymond-laptop:~/_Work/Dev/python$ python le.py time:  0:00:06.708924 return:  1 0 0 0 之后，在高手提示下将复杂度降低到n (log n + 1) ，结果不错了。 raymond@raymond-laptop:~/_Work/Dev/python$ python le.py time:  0:00:00.010430 return:  1 0 0 0 对应改进后的代码： def leader3(array): t = len(array) //2 array.sort() p = -1 c = 1 for i in array: if p == i : c+=1 if c &#62; t: [...]]]></description>
			<content:encoded><![CDATA[<p>编程果然不能偷懒！我这一偷懒，CPU就不知道要受多少累、冒多少热气了。一开始，我采用了很无脑的穷举方法，复杂度是O(n**2)，结果肯定正确的，但是长度一万的数组就可以耗去6.7秒的CPU时间。</p>
<blockquote><p>raymond@raymond-laptop:~/_Work/Dev/python$ python le.py<br />
time:  <strong>0:00:06.708924</strong><br />
return:  1 0 0 0</p></blockquote>
<p>之后，在高手提示下将复杂度降低到n (log n + 1) ，结果不错了。</p>
<blockquote><p>raymond@raymond-laptop:~/_Work/Dev/python$ python le.py<br />
time:  <strong>0:00:00.010430</strong><br />
return:  1 0 0 0</p></blockquote>
<p>对应改进后的代码：</p>
<pre>def leader3(array):
	t = len(array) //2
	array.sort()
	p = -1
	c = 1
	for i in array:
		if p == i :
			c+=1
			if c &gt; t:
				return 1
		else :
			p = i
			c = 1
		#print i, p, c

	return 0</pre>
<p>这是我要记住的一课。</p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/507">Permalink</a> |
<a href="http://raynix.info/archives/507#comments">唉, 一个评论都没</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/507&title=从6.7到0.01">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/%e7%ae%97%e6%b3%95" rel="tag">算法</a>, <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/507/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>偷闲，写了个初级二叉树排序</title>
		<link>http://raynix.info/archives/495</link>
		<comments>http://raynix.info/archives/495#comments</comments>
		<pubDate>Tue, 14 Apr 2009 07:32:25 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Python & Django]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=495</guid>
		<description><![CDATA[二叉树是计算机算法中普遍采用的数据结构。在所有排序算法中具有高效率的二叉树排序就是基于此结构。我抽空用新学的Python写了一个试试： class node: left = None right = None value = 0 def __init__(self, v): self.value = v def walk(self): r = [] if self.left: r.extend(self.left.walk()) r.append(self.value) if self.right: r.extend(self.right.walk()) return r def insert(self, n): if self.value &#60; n: if self.right: self.right.insert(n) else: self.right = node(n) else: if self.left: self.left.insert(n) else: self.left = node(n) if [...]]]></description>
			<content:encoded><![CDATA[<p>二叉树是计算机算法中普遍采用的数据结构。在<a href="http://en.wikipedia.org/wiki/Sort_algorithm" target="_blank">所有排序算法</a>中具有高效率的<a href="http://blog.csdn.net/andylin02/archive/2008/01/27/2068596.aspx" target="_blank">二叉树排序</a>就是基于此结构。我抽空用新学的Python写了一个试试：</p>
<pre>class node:
	left = None
	right = None
	value = 0

	def __init__(self, v):
		self.value = v

	def walk(self):
		r = []
		if self.left:
			r.extend(self.left.walk())
		r.append(self.value)
		if self.right:
			r.extend(self.right.walk())
		return r

	def insert(self, n):
		if self.value &lt; n:
			if self.right:
				self.right.insert(n)
			else:
				self.right = node(n)
		else:
			if self.left:
				self.left.insert(n)
			else:
				self.left = node(n)

if __name__ == '__main__':
    s = [21,545,65,33,1,2324,232,42]
    p = node(s[0])
    for i in s[1:]:
	    p.insert(i)
    print p.walk()</pre>
<p>运行结果</p>
<blockquote><p>python bi-tree.py<br />
[1, 21, 33, 42, 65, 232, 545, 2324]</p></blockquote>
<p>结论：一次编写，基本正确。python很合我的路子啊。</p>
<p>BTW 用&lt;pre&gt;贴python code很正点:)</p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/495">Permalink</a> |
<a href="http://raynix.info/archives/495#comments">2 条评论</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/495&title=偷闲，写了个初级二叉树排序">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/%e7%ae%97%e6%b3%95" rel="tag">算法</a>, <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/495/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python＋Django笔记之三</title>
		<link>http://raynix.info/archives/463</link>
		<comments>http://raynix.info/archives/463#comments</comments>
		<pubDate>Thu, 12 Mar 2009 07:32:47 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Python & Django]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=463</guid>
		<description><![CDATA[关于urls.py 假设test项目下有一个应用模块notes，那么： test/urls.py： from django.conf.urls.defaults import * urlpatterns = patterns(' ', (r'^notes/', include('notes.urls')), ... ) 这样所有匹配/notes/*的访问将转交给notes/urls.py处理。 test/notes/urls.py: ... urlpatterns = patterns(' ', url(r'^list/$', views.list_notes, name='note_list'), url(r'^(?P&#60;note_id&#62;\d+)/$', views.show_note, name='note_show'), ) 首先，由于需要反向解析（页面名称-&#62;URL），所以要使用url()，而不是原来的tuple。对应url(r'^list/$', views.list_notes, name='note_list'), 将匹配/notes/list/的访问指向给views.py中的list_notes()方法，并将'note_list'名称反向解析到/notes/list/的URL，以便在webapp中生成链接。 第二行中的(?P&#60;note_id&#62;\d+)是一个参数定义，其中note_id是参数名，\d+说明此参数是一个数字。其实起到的作用就是将/note/123/这样的url解释成一个调用show_note(request, note_id=123)。如果参数不是数字而是字词，那么\d+应该改为\w+，具体定义可以参考regex规则。 © raynix for #raynix#, 2009. &#124; Permalink &#124; 唉, 一个评论都没 &#124; Add to del.icio.us Post tags: Django, Python Feed enhanced [...]]]></description>
			<content:encoded><![CDATA[<p><strong>关于urls.py</strong></p>
<p>假设test项目下有一个应用模块notes，那么：</p>
<p>test/urls.py：</p>
<blockquote><p>from django.conf.urls.defaults import *</p>
<p>urlpatterns = patterns(' ',</p>
<p>(r'^notes/', include('notes.urls')),</p>
<p>...</p>
<p>)</p></blockquote>
<p>这样所有匹配/notes/*的访问将转交给notes/urls.py处理。</p>
<p>test/notes/urls.py:</p>
<blockquote><p>...</p>
<p>urlpatterns = patterns(' ',</p>
<p>url(r'^list/$', views.list_notes, name='note_list'),</p>
<p>url(r'^(?P&lt;note_id&gt;\d+)/$', views.show_note, name='note_show'),</p>
<p>)</p></blockquote>
<p>首先，由于需要反向解析（页面名称-&gt;URL），所以要使用url()，而不是原来的tuple。对应url(r'^list/$', views.list_notes, name='note_list'), 将匹配/notes/list/的访问指向给views.py中的list_notes()方法，并将'note_list'名称反向解析到/notes/list/的URL，以便在webapp中生成链接。</p>
<p>第二行中的(?P&lt;note_id&gt;\d+)是一个参数定义，其中note_id是参数名，\d+说明此参数是一个数字。其实起到的作用就是将/note/123/这样的url解释成一个调用show_note(request, note_id=123)。如果参数不是数字而是字词，那么\d+应该改为\w+，具体定义可以参考regex规则。</p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/463">Permalink</a> |
<a href="http://raynix.info/archives/463#comments">唉, 一个评论都没</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/463&title=Python＋Django笔记之三">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/django" rel="tag">Django</a>, <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/463/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>带着小抄去上班</title>
		<link>http://raynix.info/archives/458</link>
		<comments>http://raynix.info/archives/458#comments</comments>
		<pubDate>Mon, 09 Mar 2009 07:34:30 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=458</guid>
		<description><![CDATA[你上考场那年带小抄了么？带了也别说，那是作弊。不过上班跟考试不同，你要尽可能的用上一切能方便工作的工具。下面是我最近喜欢的几张小抄： 以上小抄来自 http://www.addedbytes.com/cheat-sheets Thanks Dave! © raynix for #raynix#, 2009. &#124; Permalink &#124; 唉, 一个评论都没 &#124; Add to del.icio.us Post tags: css, Python, regex Feed enhanced by Better Feed from Ozh]]></description>
			<content:encoded><![CDATA[<p>你上考场那年带小抄了么？带了也别说，那是作弊。不过上班跟考试不同，你要尽可能的用上一切能方便工作的工具。下面是我最近喜欢的几张小抄：</p>

<a href='http://raynix.info/archives/458/css-cheat-sheet-v2' title='css-cheat-sheet-v2'><img width="150" height="150" src="http://raynix.info/wp-content/uploads/2009/03/css-cheat-sheet-v2-150x150.png" class="attachment-thumbnail" alt="css-cheat-sheet-v2" title="css-cheat-sheet-v2" /></a>
<a href='http://raynix.info/archives/458/python-cheat-sheet-v1' title='python-cheat-sheet-v1'><img width="150" height="150" src="http://raynix.info/wp-content/uploads/2009/03/python-cheat-sheet-v1-150x150.png" class="attachment-thumbnail" alt="python-cheat-sheet-v1" title="python-cheat-sheet-v1" /></a>
<a href='http://raynix.info/archives/458/regular-expressions-cheat-sheet-v2' title='regular-expressions-cheat-sheet-v2'><img width="150" height="150" src="http://raynix.info/wp-content/uploads/2009/03/regular-expressions-cheat-sheet-v2-150x150.png" class="attachment-thumbnail" alt="regular-expressions-cheat-sheet-v2" title="regular-expressions-cheat-sheet-v2" /></a>

<p>以上小抄来自<a href="http://www.addedbytes.com/cheat-sheets" target="_blank"> http://www.addedbytes.com/cheat-sheets</a> Thanks Dave!</p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/458">Permalink</a> |
<a href="http://raynix.info/archives/458#comments">唉, 一个评论都没</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/458&title=带着小抄去上班">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/css" rel="tag">css</a>, <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a>, <a href="http://raynix.info/archives/tag/regex" rel="tag">regex</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/458/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
