<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1015999587111204108</id><updated>2024-09-01T22:30:10.250+08:00</updated><category term="epy"/><category term="xpy"/><category term="charset"/><category term="date"/><category term="datetime"/><category term="hash"/><category term="py3k"/><category term="time"/><title type='text'>Pythonly</title><subtitle type='html'>我們是喜愛 Python 的人，這個 blog 是為了向大家介紹我們對於 Python 的喜好及學習心得。</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>ericsk</name><uri>http://www.blogger.com/profile/18370675023130925184</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://blog.ericsk.org/wp-content/uploads/2006/09/171838031_7ae90ee7dd_t.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>10</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-7769883320366803180</id><published>2008-12-29T16:47:00.002+08:00</published><updated>2008-12-29T17:06:17.975+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="date"/><category scheme="http://www.blogger.com/atom/ns#" term="datetime"/><category scheme="http://www.blogger.com/atom/ns#" term="epy"/><category scheme="http://www.blogger.com/atom/ns#" term="time"/><title type='text'>Python 的時間</title><content type='html'>因為經常為 Python 處理時間的問題煩惱，所以打算留篇記錄以便之後查閱。&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;h4&gt;取得今天日期&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import datetime&lt;br /&gt;today = datetime.date.today()&lt;br /&gt;# today 是一個 datetime.date object&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;取得現在日期及時間&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import datetime&lt;br /&gt;current = datetime.datetime.now()&lt;br /&gt;# current 是一個 datetime.datetime object&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;建立日期&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import datetime&lt;br /&gt;some_day = datetime.datetime(2008, 12, 25)&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;建立日期及時間&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import datetime&lt;br /&gt;some_day = datetime.datetime(2008, 12, 25, 12, 0, 0)&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;格式化現在時間&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;from time import strftime&lt;br /&gt;now = strftime(&#39;%Y-%m-%d %H:%M:%S&#39;)&lt;br /&gt;# now 是一個 str, &#39;2008-12-29 16:12:34&#39;&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;格式化 datetime.datetime object&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import datetime&lt;br /&gt;some_day = datetime.datetime(2008, 12, 25)&lt;br /&gt;print some_day.strftime(&#39;%Y-%m-%d&#39;)&lt;br /&gt;# 得到 &#39;2008-12-25&#39;&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;code&gt;datetime.datetime&lt;/code&gt; 可以相減（算差距），也可以直接比較&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;換成 tuple&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import datetime&lt;br /&gt;now_tuple = datetime.datetime.now().timetuple()&lt;br /&gt;# now_tuple 會得到一個 tuple&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;取得 timestamp&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import time&lt;br /&gt;ts = time.time()&lt;br /&gt;# ts 是一個 timestamp, type 為 float&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;從 timestamp 換回 datetime object&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;import time&lt;br /&gt;import datetime&lt;br /&gt;ts = time.time()&lt;br /&gt;dt = datetime.datetime.fromtimestamp(ts)&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;從 time tuple 換成 timestamp&lt;/h4&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;from time import mktime&lt;br /&gt;ts = mktime(now_tuple)&lt;br /&gt;# ts 是 timestamp&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/7769883320366803180/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/12/python.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/7769883320366803180'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/7769883320366803180'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/12/python.html' title='Python 的時間'/><author><name>epy</name><uri>http://www.blogger.com/profile/14340340074123851881</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-2687387537308621876</id><published>2008-10-12T11:18:00.005+08:00</published><updated>2008-10-12T11:36:34.868+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="epy"/><category scheme="http://www.blogger.com/atom/ns#" term="hash"/><title type='text'>驗證檔案的內容</title><content type='html'>我們在很多網站下載檔案時，為了確保檔案內容沒有被修改過，通常檔案的提供者會提供你一組 MD5 或 SHA-1 之類的字串供你驗證，我把這個動作寫成一個 python script 來快速檢查：&lt;br /&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;import hashlib&lt;br /&gt;import os&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;def usage(app_name):&lt;br /&gt;    &quot;&quot;&quot;&lt;br /&gt;    The usage doc&lt;br /&gt;    &quot;&quot;&quot;&lt;br /&gt;    return &quot;&quot;&quot;Usage: %s _filename_ _hash_algo_ _hash_value_&quot;&quot;&quot;&lt;br /&gt;&lt;br /&gt;def main(args):&lt;br /&gt;    &quot;&quot;&quot;&lt;br /&gt;    Main program&lt;br /&gt;    &quot;&quot;&quot;&lt;br /&gt;    try:&lt;br /&gt;        f = open(args[0], &#39;r&#39;)&lt;br /&gt;        dig = getattr(hashlib, args[1])(f.read()).hexdigest()&lt;br /&gt;        f.close()&lt;br /&gt;        print (dig == args[2] and &#39;Valid&#39;) or &#39;Invalid&#39;&lt;br /&gt;    except IOError, e:&lt;br /&gt;        print &#39;找不到檔案 &quot;%s&quot;&#39; % args[0]&lt;br /&gt;    except AttributeError, e:&lt;br /&gt;        print &#39;Hash 演算法名稱錯誤&#39;&lt;br /&gt;&lt;br /&gt;def commandline():&lt;br /&gt;    if len(sys.argv) &gt; 2:&lt;br /&gt;        main(sys.argv[1:])&lt;br /&gt;    else:&lt;br /&gt;        print usage(sys.argv[0])&lt;br /&gt;&lt;br /&gt;if __name__ == &#39;__main__&#39;:&lt;br /&gt;    commandline()&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;簡單的使用方法就是 「script 檔名 演算法 驗證碼」，如果我將這個 script 存成一個叫 verify.py 的檔案，下載了一個 foo.dat 的檔案，要用 sha1 演算法檢查 abcdefg(假的，sha-1 的碼很長)，那就這樣用：&lt;br /&gt;&lt;br /&gt;&lt;code&gt;verify.py foo.dat sha1 abcdefg&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;演算法直接用 python hashlib 裡支援的，如果不支援就會直接跳到 AttributeError :P</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/2687387537308621876/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/10/blog-post.html#comment-form' title='1 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/2687387537308621876'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/2687387537308621876'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/10/blog-post.html' title='驗證檔案的內容'/><author><name>epy</name><uri>http://www.blogger.com/profile/14340340074123851881</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-2476134980344568284</id><published>2008-09-03T20:18:00.010+08:00</published><updated>2008-09-03T20:35:38.083+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="xpy"/><title type='text'>根據 ID 抓取 YouTube 影片</title><content type='html'>&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;&lt;br /&gt;def progress_hook(block_number, block_size, total_size):&lt;br /&gt;    if total_size &gt; 0:&lt;br /&gt;        percentage = 100 * block_number * block_size / total_size&lt;br /&gt;        if percentage &gt; 100:&lt;br /&gt;            percentage = 100&lt;br /&gt;        print &#39;%2d%%&#39; % percentage&lt;br /&gt;&lt;br /&gt;def getYouTubeVideoByID(video_id, output_filename):&lt;br /&gt;    import re, urllib&lt;br /&gt;    url = &#39;http://www.youtube.com/v/%s&#39; % (video_id)&lt;br /&gt;    real_url = urllib.urlopen(url).geturl()&lt;br /&gt;    match = re.findall(&quot;t=([^&amp;amp;]*)&quot;, real_url)&lt;br /&gt;    if match:&lt;br /&gt;        fetch_url = &#39;http://www.youtube.com/get_video.php?video_id=%s&amp;amp;t=%s&#39; % (video_id, match[0])&lt;br /&gt;        print fetch_url&lt;br /&gt;        try:&lt;br /&gt;            urllib.urlretrieve(fetch_url, output_filename, progress_hook)&lt;br /&gt;        except urllib.ContentTooShortError:&lt;br /&gt;            print &#39;ContentTooShortError&#39;&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;其中 progress_hook 是用來顯示進度的，如果不喜歡可以自己修改格式，或是在 getYouTubeVideoByID 裡面呼叫 urllib.urlretrieve 的地方把 progress_hook 這個參數拿掉。&lt;br /&gt;&lt;br /&gt;使用方法： getYouTubeVideoByID(&amp;lt;id&amp;gt;, &amp;lt;file_name&amp;gt;)&lt;br /&gt;範例：&lt;br /&gt;&lt;code&gt;&lt;pre class=&quot;python&quot; name=&quot;code&quot;&gt;getYouTubeVideoByID(&#39;2scOjA_U09w&#39;, &#39;E:\\CCF.flv&#39;)&lt;/pre&gt;&lt;/code&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/2476134980344568284/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/09/id-youtube.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/2476134980344568284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/2476134980344568284'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/09/id-youtube.html' title='根據 ID 抓取 YouTube 影片'/><author><name>xpy</name><uri>http://www.blogger.com/profile/10917282530987667329</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-815456950141093358</id><published>2008-06-18T14:04:00.003+08:00</published><updated>2008-09-03T20:26:39.578+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="xpy"/><title type='text'>動態改變 instance 的 member function</title><content type='html'>首先先建立一個 class：&lt;br /&gt;&lt;code&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;&lt;br /&gt;class foo:&lt;br /&gt;  def say(self, s):&lt;br /&gt;    print &#39;Hello, %s&#39; % (s)&lt;br /&gt;&lt;br /&gt;f = foo()&lt;br /&gt;f.say(&#39;Bob&#39;)&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;這樣會印出 &#39;Hello, Bob&#39;。&lt;br /&gt;如果想把裡面的 say 換成自己定義的 function，可以先定義該 function：&lt;br /&gt;&lt;code&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;&lt;br /&gt;def helloworld(self, s):&lt;br /&gt;  print &#39;Hello World %s&#39; % (s)&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;然後再用 new.instancemethod 把它給換掉：&lt;br /&gt;&lt;code&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;&lt;br /&gt;import new&lt;br /&gt;f.say = new.instancemethod(helloworld, f, foo)&lt;br /&gt;f.say(&#39;Alice&#39;)&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;這樣就會印出 &#39;Hello Wolrd, Alice&#39; 囉</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/815456950141093358/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/06/instance-member-function_18.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/815456950141093358'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/815456950141093358'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/06/instance-member-function_18.html' title='動態改變 instance 的 member function'/><author><name>xpy</name><uri>http://www.blogger.com/profile/10917282530987667329</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-5461999878594430307</id><published>2008-04-29T00:17:00.003+08:00</published><updated>2008-04-29T09:29:48.316+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="charset"/><category scheme="http://www.blogger.com/atom/ns#" term="epy"/><title type='text'>Python 的字元編碼</title><content type='html'>Python 並不像其它語言一樣，看你的程式碼檔案是什麼字元編碼就用什麼編碼來解讀內部的字串。所以必須在檔案開頭處清楚地宣告&lt;br /&gt;&lt;code&gt;# -*- coding: utf-8 -*-&lt;/code&gt;&lt;br /&gt;當然你的程式碼檔案也要存成對應的編碼才能正常執行。&lt;br /&gt;&lt;br /&gt;而如果在一個 utf-8 編碼的程式碼裡，若要將某 utf-8 字串轉成 cp950 （在 Windows 上蠻常用的）的話，只要：&lt;br /&gt;&lt;code&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;str = &quot;我是UTF-8的字串&quot;&lt;br /&gt;print str.decode(&#39;utf-8&#39;).encode(&#39;cp950&#39;)&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;就可以了。相當方便！</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/5461999878594430307/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/04/python.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/5461999878594430307'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/5461999878594430307'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/04/python.html' title='Python 的字元編碼'/><author><name>epy</name><uri>http://www.blogger.com/profile/14340340074123851881</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-5983809510483327442</id><published>2008-04-29T00:09:00.006+08:00</published><updated>2008-04-29T09:28:55.138+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="epy"/><title type='text'>Python 版的 switch case</title><content type='html'>其實一直不太清楚為什麼 Python 內建的語法會沒有 switch/case 的用法，所以這時候只好自己做一下了&lt;br /&gt;&lt;br /&gt;如果本來在 C 裡會寫這樣：&lt;br /&gt;&lt;code&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;x = 0;&lt;br /&gt;switch (value) {&lt;br /&gt;  case 1:&lt;br /&gt;    x += 1;&lt;br /&gt;    break;&lt;br /&gt;  case 2:&lt;br /&gt;    x *= 5;&lt;br /&gt;    break;&lt;br /&gt;  ...&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;那在 Python 裡可以寫成這樣：&lt;br /&gt;&lt;code&gt;&lt;pre name=&quot;code&quot; class=&quot;python&quot;&gt;x  = 0&lt;br /&gt;{&lt;br /&gt;  1: lambda y: y += 1,&lt;br /&gt;  2: lambda y: y *= 5&lt;br /&gt;  ...&lt;br /&gt;}[value](x)&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;簡單地說就是用 dictionary 來作。</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/5983809510483327442/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/04/python-switch-case.html#comment-form' title='3 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/5983809510483327442'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/5983809510483327442'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/04/python-switch-case.html' title='Python 版的 switch case'/><author><name>epy</name><uri>http://www.blogger.com/profile/14340340074123851881</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-8753269717101148147</id><published>2008-04-24T11:58:00.009+08:00</published><updated>2008-09-03T20:32:04.165+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="xpy"/><title type='text'>簡易密碼產生器</title><content type='html'>&lt;code&gt;&lt;br /&gt;&lt;textarea name=&quot;code&quot; class=&quot;python&quot; wrap=&quot;hard&quot;&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;&lt;br /&gt;from random import choice&lt;br /&gt;from string import digits, letters&lt;br /&gt;&lt;br /&gt;PASSLEN = 8 # 密碼長度&lt;br /&gt;POOL = digits + letters # 密碼字元&lt;br /&gt;&lt;br /&gt;print &#39;&#39;.join([ choice(POOL) for i in range(PASSLEN) ])&lt;br /&gt;&lt;/textarea&gt;&lt;br /&gt;&lt;/code&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/8753269717101148147/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/04/blog-post.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/8753269717101148147'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/8753269717101148147'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/04/blog-post.html' title='簡易密碼產生器'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-6564396530243967398</id><published>2008-01-26T15:50:00.000+08:00</published><updated>2008-01-26T16:01:07.942+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="epy"/><category scheme="http://www.blogger.com/atom/ns#" term="py3k"/><title type='text'>在 Mac 上編譯 Python 3 的問題</title><content type='html'>Python 3 (a.k.a. Py3k) 目前已經推出 3.0a2 的版本，我拿到 Mac 系統上編譯時，下的參數為：&lt;br /&gt;&lt;blockquote&gt;./configure --prefix=/usr/local --enable-framework --enable-toolbox-glue --with-threads&lt;/blockquote&gt;&lt;br /&gt;結果在編譯時會出現這樣的錯誤：&lt;br /&gt;&lt;blockquote&gt;LookupError: unknown encoding: X-MAC-TRAD-CHINESE&lt;/blockquote&gt;&lt;br /&gt;看起來是目前抓到的 encoding 會讓某個函式庫無法完成編譯，所以我修改了 &lt;span style=&quot;font-weight:bold;&quot;&gt;Lib/io.py&lt;/span&gt; 這個檔案，然後把&lt;br /&gt;&lt;blockquote&gt;encoding = locale.getpreferredencoding()&lt;/blockquote&gt;&lt;br /&gt;換成&lt;br /&gt;&lt;blockquote&gt;encoding = &quot;utf-8&quot;&lt;/blockquote&gt;&lt;br /&gt;這樣就可以順利完成編譯了！</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/6564396530243967398/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/01/mac-python-3.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/6564396530243967398'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/6564396530243967398'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/01/mac-python-3.html' title='在 Mac 上編譯 Python 3 的問題'/><author><name>epy</name><uri>http://www.blogger.com/profile/14340340074123851881</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-4790965409068698759</id><published>2008-01-24T10:28:00.000+08:00</published><updated>2008-01-24T10:30:03.103+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="epy"/><title type='text'>哪裡會用到 Python ?</title><content type='html'>如我們所知，Python 是一個 scripting language，只要有 Python 直譯器 (interpreter) 就可以執行 Python 程式。Python 在目前主流的作業系統如：Windows, Linux/Unix, Mac 等上都有相同版本的直譯器可以下載，你當然就可以在這些平台上撰寫 Python 程式。&lt;br /&gt;&lt;br /&gt;從桌面系統的程式來說，Windows 上的 Python 可以透過 ctypes 使用 Windows API，也可以使用 IronPython 來結合 .Net framework 的程式庫；wxPython, PyGTK, PyQt, Jython 都是可以跨平台寫出 GUI 程式的函式庫，一些 GNOME 或 KDE 的 widget 就是用 PyGTK 或 PyQt 所寫出來的呢！而 Mac OS X 上當然也有 Python-Cocoa binding -- PyObjc 囉，這個只要灌好 Xcodes 就直接能用 Python 來寫 Mac OS X 應用程式。&lt;br /&gt;&lt;br /&gt;除了桌面系統、個人電腦上執行的 Python 程式，在 Web 開發上也可以使用到 Python，除了直接使用 Python 的 CGI 函式庫或是直接處理 HTTP protocol，你也可以使用像是 Zope, Plone, TurboGears, django 等等 web 開發框架 (framework) 更直接地處理關於 web 的運算、顯示及資料儲存等。&lt;br /&gt;&lt;br /&gt;另外，手持裝置當然也能執行 Python 程式，有人已經將 Python 直譯器放進破解的 iPhone/iPod touch, WinCE 系列的 smart phone 當然也能執行 Python，而 Symbian S60 也有弄出一套 PyS60，也就是讓你透過 Python 語言在 Symbian 系統上開發程式。&lt;br /&gt;&lt;br /&gt;這樣乍看之下，Python 跟 Java 好像還有那麼一點點相像呢！不管是在 desktop, server, mobile device 都可以看到它們的影子，不過比起 Java, Python 的 code 可是輕巧得多，雖然 Python 有硬要縮排，以及稍稍殘廢的 OOP 架構等小缺點，但寫起 Python 程式，這些小缺點可說是瑕不掩瑜...&lt;br /&gt;&lt;br /&gt;Python 已當選了 2007 年度程式語言，不如趁機來好好學學（寫寫）Python 吧！</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/4790965409068698759/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/01/python_5781.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/4790965409068698759'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/4790965409068698759'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/01/python_5781.html' title='哪裡會用到 Python ?'/><author><name>epy</name><uri>http://www.blogger.com/profile/14340340074123851881</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1015999587111204108.post-6303304502038043253</id><published>2008-01-24T10:27:00.001+08:00</published><updated>2008-01-24T10:27:30.222+08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="xpy"/><title type='text'>喜歡 Python 的理由</title><content type='html'>第一次聽到 Python 的時候，只知道它是一個要認縮排的語言，對於這個限制雖然感到不解，卻也沒有仔細去思考它的設計哲學，直到過了很久以後，真的開始學了 Python，才開始愛上了這個大家都認為十分簡單的語言。&lt;br /&gt;Python 有多簡單？大多數的人都可以在一個星期內快速上手。常見的資料結構也幾乎都可以用它內建的三種資料結構（tuple, list, dictionary）搭出來，資料結構中可以混合不同型別的資料，可以免除很多像 C++ 這類編譯語言的限制，使程式設計師能夠更關注在於該做的事，而不是該如何去做。而且絕大多數的系統都有提供 Python 的直譯器，所以它既可以跨平台，也不需要 compiler，很適合用在一些和效能較為無關的地方，像是 UI，因為修改非常方便！若是要計較效能的話，Python 要和 C/C++ 整合可是一點都不難，透過 swig/ctypes/sip 之類的工具，你可以很輕鬆地在 Python 裡呼叫以 C/C++ 所撰寫的函式。&lt;br /&gt;其它的小地方，像是 Python 內建支援大數、複數、Unicode，甚至很特別地支援 if 1 &lt; x &lt; 2 這樣的寫法，都是很吸引我的特色。雖然我接觸 Python 的時間才短短兩、三個月，但是我實在太喜歡 Python 了，於是想要在這裡和大家分享我一些學習的心得筆記，希望可以幫助一些同樣想要入門的朋友們。希望大家多多指教。</content><link rel='replies' type='application/atom+xml' href='http://pythonly.blogspot.com/feeds/6303304502038043253/comments/default' title='張貼留言'/><link rel='replies' type='text/html' href='http://pythonly.blogspot.com/2008/01/python_24.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/6303304502038043253'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1015999587111204108/posts/default/6303304502038043253'/><link rel='alternate' type='text/html' href='http://pythonly.blogspot.com/2008/01/python_24.html' title='喜歡 Python 的理由'/><author><name>xpy</name><uri>http://www.blogger.com/profile/10917282530987667329</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>