<?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>Hi! I&#039;m clsung &#187; python</title>
	<atom:link href="http://blog.dragon2.net/category/hacker/programming/python/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.dragon2.net</link>
	<description>clsung&#039;s blog site</description>
	<lastBuildDate>Mon, 06 Feb 2012 09:29:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>dotCloud first test</title>
		<link>http://blog.dragon2.net/2011/05/23/1022.php</link>
		<comments>http://blog.dragon2.net/2011/05/23/1022.php#comments</comments>
		<pubDate>Mon, 23 May 2011 02:41:15 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/?p=1022</guid>
		<description><![CDATA[% pip install -U dotcloud % dotcloud create wall Enter your api key &#40;You can find it at http://www.dotcloud.com/account/settings&#41;: Created &#34;wall&#34; % dotcloud deploy -t python wall.ffstream Created &#34;wall.ffstream&#34;. % dotcloud push wall.ffstream . # upload . ssh://dotcloud@uploader.dotcloud.com:1060/wall.ffstream # git Warning: Permanently added &#8216;[uploader.dotcloud.com]:1060,[174.129.15.77]:1060&#8242; &#40;RSA&#41; to the list of known hosts. Counting objects: 17, done. Delta compression using up to 2 threads. Compressing objects: 100% &#40;14/14&#41;, done. Writing objects: 100% &#40;17/17&#41;, 4.75 KiB, done. Total 17 &#40;delta 1&#41;, reused 0 &#40;delta 0&#41; To ssh://dotcloud@uploader.dotcloud.com:1060/wall.ffstream &#160;* &#91;new branch&#93; &#160; &#160; &#160;master -&#62; master Scheduling build Fetching logs&#8230; Warning: Permanently added &#8216;[ffstream.wall.dotcloud.com]:yyyy,[xxx.xxx.xxx.xxx]:yyyy&#8217; &#40;RSA&#41; to the list of known hosts. &#8211; Build started&#8230; .gitignore __init__.py forms.py friendship.html manage.py plurk.py settings.py urls.py views.py wsgi.py Fetched code revision c1802d6 Reloading nginx configuration: nginx. uwsgi: stopped uwsgi: started Connection to ffstream.wall.dotcloud.com closed. Oops, I forget to specify the project is a django project. % echo django &#62; requirements.txt % dotcloud push wall.ffstream &#8230;. Downloading Django-1.3.tar.gz &#40;6.5Mb&#41;: 6.5Mb downloaded &#160; Storing download in cache at /home/dotcloud/.pip-cache/http%3A%2F%2Fpypi.python.org%2Fpackages%2Fsource%2FD%2FDjango%2FDjango-1.3.tar.gz &#160; Running setup.py egg_info for package django Installing collected packages: django &#160; Running setup.py install for django &#160; &#160; changing mode of build/scripts-2.6/django-admin.py from 644 to 755 &#160; &#160; changing mode [...]]]></description>
			<content:encoded><![CDATA[<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> pip <span class="kw2">install</span> <span class="re5">-U</span> dotcloud <br />
<span class="sy0">%</span> dotcloud create <span class="kw2">wall</span><br />
Enter your api key <span class="br0">&#40;</span>You can <span class="kw2">find</span> it at http:<span class="sy0">//</span>www.dotcloud.com<span class="sy0">/</span>account<span class="sy0">/</span>settings<span class="br0">&#41;</span>: <br />
Created <span class="st0">&quot;wall&quot;</span><br />
<span class="sy0">%</span> dotcloud deploy <span class="re5">-t</span> python wall.ffstream<br />
Created <span class="st0">&quot;wall.ffstream&quot;</span>.<br />
<span class="sy0">%</span> dotcloud push wall.ffstream .<br />
<span class="co0"># upload . ssh://dotcloud@uploader.dotcloud.com:1060/wall.ffstream</span><br />
<span class="co0"># git</span><br />
Warning: Permanently added <span class="st_h">&#8216;[uploader.dotcloud.com]:1060,[174.129.15.77]:1060&#8242;</span> <span class="br0">&#40;</span>RSA<span class="br0">&#41;</span> to the list of known hosts.<br />
Counting objects: 17, done.<br />
Delta compression using up to 2 threads.<br />
Compressing objects: 100<span class="sy0">%</span> <span class="br0">&#40;</span>14<span class="sy0">/</span>14<span class="br0">&#41;</span>, done.<br />
Writing objects: 100<span class="sy0">%</span> <span class="br0">&#40;</span>17<span class="sy0">/</span>17<span class="br0">&#41;</span>, 4.75 KiB, done.<br />
Total 17 <span class="br0">&#40;</span>delta 1<span class="br0">&#41;</span>, reused 0 <span class="br0">&#40;</span>delta 0<span class="br0">&#41;</span><br />
To <span class="kw2">ssh</span>:<span class="sy0">//</span>dotcloud<span class="sy0">@</span>uploader.dotcloud.com:<span class="nu0">1060</span><span class="sy0">/</span>wall.ffstream<br />
&nbsp;<span class="sy0">*</span> <span class="br0">&#91;</span>new branch<span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp;master -<span class="sy0">&gt;</span> master<br />
Scheduling build<br />
Fetching logs&#8230;<br />
Warning: Permanently added <span class="st_h">&#8216;[ffstream.wall.dotcloud.com]:yyyy,[xxx.xxx.xxx.xxx]:yyyy&#8217;</span> <span class="br0">&#40;</span>RSA<span class="br0">&#41;</span> to the list of known hosts.<br />
<span class="re5">&#8211;</span> Build started&#8230;<br />
.gitignore<br />
__init__.py<br />
forms.py<br />
friendship.html<br />
manage.py<br />
plurk.py<br />
settings.py<br />
urls.py<br />
views.py<br />
wsgi.py<br />
Fetched code revision c1802d6<br />
Reloading nginx configuration: nginx.<br />
uwsgi: stopped<br />
uwsgi: started<br />
Connection to ffstream.wall.dotcloud.com closed.</div>
</div>
<p>Oops, I forget to specify the project is a django project.</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> <span class="kw3">echo</span> django <span class="sy0">&gt;</span> requirements.txt<br />
<span class="sy0">%</span> dotcloud push wall.ffstream<br />
&#8230;.<br />
Downloading Django-1.3.tar.gz <span class="br0">&#40;</span>6.5Mb<span class="br0">&#41;</span>: 6.5Mb downloaded<br />
&nbsp; Storing download <span class="kw1">in</span> cache at <span class="sy0">/</span>home<span class="sy0">/</span>dotcloud<span class="sy0">/</span>.pip-cache<span class="sy0">/</span>http<span class="sy0">%</span>3A<span class="sy0">%</span>2F<span class="sy0">%</span>2Fpypi.python.org<span class="sy0">%</span>2Fpackages<span class="sy0">%</span>2Fsource<span class="sy0">%</span>2FD<span class="sy0">%</span>2FDjango<span class="sy0">%</span>2FDjango-1.3.tar.gz<br />
&nbsp; Running setup.py egg_info <span class="kw1">for</span> package django<br />
Installing collected packages: django<br />
&nbsp; Running setup.py <span class="kw2">install</span> <span class="kw1">for</span> django<br />
&nbsp; &nbsp; changing mode of build<span class="sy0">/</span>scripts-<span class="nu0">2.6</span><span class="sy0">/</span>django-admin.py from <span class="nu0">644</span> to <span class="nu0">755</span><br />
&nbsp; &nbsp; changing mode of <span class="sy0">/</span>home<span class="sy0">/</span>dotcloud<span class="sy0">/</span>env<span class="sy0">/</span>bin<span class="sy0">/</span>django-admin.py to <span class="nu0">755</span><br />
Successfully installed django<br />
Cleaning up&#8230;<br />
Reloading nginx configuration: nginx.<br />
uwsgi: stopped<br />
uwsgi: started<br />
Connection to ffstream.wall.dotcloud.com closed.</div>
</div>
<p>Okay. Rocks <img src='http://blog.dragon2.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2011/05/23/1022.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Python Script to test Plurk OAuth API</title>
		<link>http://blog.dragon2.net/2011/05/19/1028.php</link>
		<comments>http://blog.dragon2.net/2011/05/19/1028.php#comments</comments>
		<pubDate>Wed, 18 May 2011 23:19:30 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[plurk]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/?p=1028</guid>
		<description><![CDATA[Plurk has released API 2. Instead of using their test console, I decided to write a command line console script&#8230; Here is the code: import oauth2 as oauth import urlparse import httplib #Plurk OAuth service endpoints: #obtain request token: REQUEST_TOKEN_URL = &#8216;http://www.plurk.com/OAuth/request_token&#8217; #authorization page: AUTHORIZE_URL = &#8216;http://www.plurk.com/OAuth/authorize&#8217; #obtain access token: ACCESS_TOKEN_URL = &#8216;http://www.plurk.com/OAuth/access_token&#8217; &#160; &#160; &#160; &#160; def get_consumer_token&#40;&#41;: &#160; &#160; &#160; &#160; &#160; &#160; # Setup &#160; &#160; print &#34;Prepare the CONSUMER Info&#34; &#160; &#160; verified = &#8216;n&#8217; &#160; &#160; while verified.lower&#40;&#41; != &#8216;y&#8217;: &#160; &#160; &#160; &#160; key = raw_input&#40;&#8216;Input the CONSUMER_KEY: &#8216;&#41; &#160; &#160; &#160; &#160; secret = raw_input&#40;&#8216;Input the CONSUMER_SECRET: &#8216;&#41; &#160; &#160; &#160; &#160; print &#8216;Consumer Key: %s&#8217; % str&#40;key&#41; &#160; &#160; &#160; &#160; print &#8216;Consumer Secret: %s&#8217; % str&#40;secret&#41; &#160; &#160; &#160; &#160; verified = raw_input&#40;&#8216;Are you sure? (y/N) &#8216;&#41; &#160; &#160; return dict&#40;&#123;&#8216;KEY&#8217;:key,&#8216;SECRET&#8217;:secret&#125;&#41; def get_request_token&#40;key, secret&#41;: &#160; &#160; # Setup &#160; &#160; consumer = oauth.Consumer&#40;key, secret&#41; &#160; &#160; client = oauth.Client&#40;consumer&#41; &#160; &#160; sign_method = oauth.SignatureMethod_HMAC_SHA1&#40;&#41; &#160; &#160; request = oauth.Request.from_consumer_and_token&#40;consumer=consumer, &#160; &#160; &#160; &#160; http_method=&#8216;POST&#8217;, http_url=REQUEST_TOKEN_URL, &#160; &#160; &#160; &#160; is_form_encoded=True&#41; &#160; &#160; request.sign_request&#40;sign_method, consumer, None&#41; &#160; &#160; # Get Request Token &#160; &#160; resp, content = client.request&#40;REQUEST_TOKEN_URL, &#34;POST&#34;, &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.plurk.com/">Plurk</a> has released <a href="http://www.plurk.com/API/2/">API 2</a>. Instead of using their test console, I decided to write a command line console script&#8230;<span id="more-1028"></span></p>
<p>Here is the code:</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="kw1">import</span> oauth2 <span class="kw1">as</span> oauth<br />
<span class="kw1">import</span> <span class="kw3">urlparse</span><br />
<span class="kw1">import</span> <span class="kw3">httplib</span> </p>
<p><span class="co1">#Plurk OAuth service endpoints:</span><br />
<span class="co1">#obtain request token:</span><br />
REQUEST_TOKEN_URL = <span class="st0">&#8216;http://www.plurk.com/OAuth/request_token&#8217;</span><br />
<span class="co1">#authorization page:</span><br />
AUTHORIZE_URL = <span class="st0">&#8216;http://www.plurk.com/OAuth/authorize&#8217;</span><br />
<span class="co1">#obtain access token:</span><br />
ACCESS_TOKEN_URL = <span class="st0">&#8216;http://www.plurk.com/OAuth/access_token&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="kw1">def</span> get_consumer_token<span class="br0">&#40;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1"># Setup</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;Prepare the CONSUMER Info&quot;</span></p>
<p>&nbsp; &nbsp; verified = <span class="st0">&#8216;n&#8217;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> verified.<span class="me1">lower</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">!</span>= <span class="st0">&#8216;y&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; key = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&#8216;Input the CONSUMER_KEY: &#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; secret = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&#8216;Input the CONSUMER_SECRET: &#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&#8216;Consumer Key: %s&#8217;</span> <span class="sy0">%</span> <span class="kw2">str</span><span class="br0">&#40;</span>key<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&#8216;Consumer Secret: %s&#8217;</span> <span class="sy0">%</span> <span class="kw2">str</span><span class="br0">&#40;</span>secret<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verified = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&#8216;Are you sure? (y/N) &#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">dict</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">&#8216;KEY&#8217;</span>:key,<span class="st0">&#8216;SECRET&#8217;</span>:secret<span class="br0">&#125;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">def</span> get_request_token<span class="br0">&#40;</span>key, secret<span class="br0">&#41;</span>:</p>
<p>&nbsp; &nbsp; <span class="co1"># Setup</span><br />
&nbsp; &nbsp; consumer = oauth.<span class="me1">Consumer</span><span class="br0">&#40;</span>key, secret<span class="br0">&#41;</span><br />
&nbsp; &nbsp; client = oauth.<span class="me1">Client</span><span class="br0">&#40;</span>consumer<span class="br0">&#41;</span><br />
&nbsp; &nbsp; sign_method = oauth.<span class="me1">SignatureMethod_HMAC_SHA1</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; request = oauth.<span class="me1">Request</span>.<span class="me1">from_consumer_and_token</span><span class="br0">&#40;</span>consumer=consumer,<br />
&nbsp; &nbsp; &nbsp; &nbsp; http_method=<span class="st0">&#8216;POST&#8217;</span>, http_url=REQUEST_TOKEN_URL,<br />
&nbsp; &nbsp; &nbsp; &nbsp; is_form_encoded=<span class="kw2">True</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; request.<span class="me1">sign_request</span><span class="br0">&#40;</span>sign_method, consumer, <span class="kw2">None</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="co1"># Get Request Token</span><br />
&nbsp; &nbsp; resp, content = client.<span class="me1">request</span><span class="br0">&#40;</span>REQUEST_TOKEN_URL, <span class="st0">&quot;POST&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers=request.<span class="me1">to_header</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> resp<span class="br0">&#91;</span><span class="st0">&#8216;status&#8217;</span><span class="br0">&#93;</span> <span class="sy0">!</span>= <span class="st0">&#8217;200&#8242;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">raise</span> <span class="kw2">Exception</span><span class="br0">&#40;</span><span class="st0">&quot;Invalid response %s.&quot;</span> <span class="sy0">%</span> resp<span class="br0">&#91;</span><span class="st0">&#8216;status&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="co1"># Get Token Key/Secret</span><br />
&nbsp; &nbsp; <span class="kw3">token</span> = <span class="kw2">dict</span><span class="br0">&#40;</span><span class="kw3">urlparse</span>.<span class="me1">parse_qsl</span><span class="br0">&#40;</span>content<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&#8216;Token Key: %s&#8217;</span> <span class="sy0">%</span> <span class="kw2">str</span><span class="br0">&#40;</span><span class="kw3">token</span><span class="br0">&#91;</span><span class="st0">&#8216;oauth_token&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&#8216;Token Secret: %s&#8217;</span> <span class="sy0">%</span> <span class="kw2">str</span><span class="br0">&#40;</span><span class="kw3">token</span><span class="br0">&#91;</span><span class="st0">&#8216;oauth_token_secret&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw3">token</span></p>
<p><span class="kw1">def</span> get_verifier<span class="br0">&#40;</span>token_key<span class="br0">&#41;</span>:</p>
<p>&nbsp; &nbsp; <span class="co1"># Setup</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;Open the following URL and authorize it&quot;</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;%s?oauth_token=%s&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span>AUTHORIZE_URL, token_key<span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; verified = <span class="st0">&#8216;n&#8217;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> verified.<span class="me1">lower</span><span class="br0">&#40;</span><span class="br0">&#41;</span> == <span class="st0">&#8216;n&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; verifier = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&#8216;Input the verification number: &#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verified = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&#8216;Are you sure? (y/n) &#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> verifier</p>
<p><span class="kw1">def</span> get_access_token<span class="br0">&#40;</span>token_key, token_secret, verifier<span class="br0">&#41;</span>:</p>
<p>&nbsp; &nbsp; <span class="co1"># Setup</span><br />
&nbsp; &nbsp; consumer = oauth.<span class="me1">Consumer</span><span class="br0">&#40;</span>CONSUMER_KEY, CONSUMER_SECRET<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw3">token</span> = oauth.<span class="me1">Token</span><span class="br0">&#40;</span>token_key, token_secret<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw3">token</span>.<span class="me1">set_verifier</span><span class="br0">&#40;</span>verifier<span class="br0">&#41;</span><br />
&nbsp; &nbsp; client = oauth.<span class="me1">Client</span><span class="br0">&#40;</span>consumer,<span class="kw3">token</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; request = oauth.<span class="me1">Request</span>.<span class="me1">from_consumer_and_token</span><span class="br0">&#40;</span>consumer=consumer,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">token</span>=<span class="kw3">token</span>, http_method=<span class="st0">&#8216;POST&#8217;</span>, http_url=ACCESS_TOKEN_URL,<br />
&nbsp; &nbsp; &nbsp; &nbsp; parameters = <span class="br0">&#123;</span><span class="st0">&#8216;oauth_token_secret&#8217;</span>: token_secret,<span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; is_form_encoded=<span class="kw2">True</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; sign_method = oauth.<span class="me1">SignatureMethod_HMAC_SHA1</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; request.<span class="me1">sign_request</span><span class="br0">&#40;</span>sign_method, consumer, <span class="kw3">token</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="co1"># Get Access Token</span><br />
&nbsp; &nbsp; resp, content = client.<span class="me1">request</span><span class="br0">&#40;</span>ACCESS_TOKEN_URL, <span class="st0">&quot;POST&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers=request.<span class="me1">to_header</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> resp<span class="br0">&#91;</span><span class="st0">&#8216;status&#8217;</span><span class="br0">&#93;</span> <span class="sy0">!</span>= <span class="st0">&#8217;200&#8242;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> content<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">raise</span> <span class="kw2">Exception</span><span class="br0">&#40;</span><span class="st0">&quot;Invalid response %s.&quot;</span> <span class="sy0">%</span> resp<span class="br0">&#91;</span><span class="st0">&#8216;status&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="co1"># Get Token Key/Secret</span><br />
&nbsp; &nbsp; <span class="kw3">token</span> = <span class="kw2">dict</span><span class="br0">&#40;</span><span class="kw3">urlparse</span>.<span class="me1">parse_qsl</span><span class="br0">&#40;</span>content<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&#8216;Access Key: %s&#8217;</span> <span class="sy0">%</span> <span class="kw2">str</span><span class="br0">&#40;</span><span class="kw3">token</span><span class="br0">&#91;</span><span class="st0">&#8216;oauth_token&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&#8216;Access Secret: %s&#8217;</span> <span class="sy0">%</span> <span class="kw2">str</span><span class="br0">&#40;</span><span class="kw3">token</span><span class="br0">&#91;</span><span class="st0">&#8216;oauth_token_secret&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw3">token</span></p>
<p><span class="kw1">if</span> __name__ == <span class="st0">&#8216;__main__&#8217;</span>:<br />
&nbsp; &nbsp; consumer = get_consumer_token<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw3">token</span> = get_request_token<span class="br0">&#40;</span>consumer<span class="br0">&#91;</span><span class="st0">&#8216;KEY&#8217;</span><span class="br0">&#93;</span>, consumer<span class="br0">&#91;</span><span class="st0">&#8216;SECRET&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; verifier = get_verifier<span class="br0">&#40;</span><span class="kw3">token</span><span class="br0">&#91;</span><span class="st0">&#8216;oauth_token&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; access_token = get_access_token<span class="br0">&#40;</span><span class="kw3">token</span><span class="br0">&#91;</span><span class="st0">&#8216;oauth_token&#8217;</span><span class="br0">&#93;</span>, <span class="kw3">token</span><span class="br0">&#91;</span><span class="st0">&#8216;oauth_token_secret&#8217;</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; verifier<span class="br0">&#41;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2011/05/19/1028.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[python] 新手玩 virtualenv + pip + yolk</title>
		<link>http://blog.dragon2.net/2011/04/04/1007.php</link>
		<comments>http://blog.dragon2.net/2011/04/04/1007.php#comments</comments>
		<pubDate>Sun, 03 Apr 2011 16:13:31 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pip]]></category>
		<category><![CDATA[virtualenv]]></category>
		<category><![CDATA[yolk]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/?p=1007</guid>
		<description><![CDATA[Python 提供了 pip 可以安裝 python packages，也提供了 virtualenv 可以把 develop 環境獨立出來，所以就在我的 MBP 上試看看。 % pip install -U virtualenv % virtualenv &#8211;no-site-packages &#8211;distribute sandbox 注意 &#8216;&#8211;no-site-packages&#8217; 這個參數一定要下，這是為了不要去動到原本的 site-packages 目錄，也就是做 "純“ 的啦。至於 &#8216;&#8211;distribute&#8217; 要不要就看個人，我自己是因為新手，所以就用新出來的選項。所以採用 distribute 而不是用原本的 setuptools 。 在還沒進 virtualenv 建出來的環境（簡稱：sandbox，雖然英文打起來比中文還長&#8230;..）前，想先安裝任何 python package，比如 "django"，就下 % pip install -E sandbox django 要進 sandbox 就在 sandbox 目錄裡下 % source bin/active 這時候就可以當做沒有 virtualenv 這東西啦，要亂搞任你玩，不過風險責任當然也是自負啦。玩完要閃了，就下 % deactivate 就可以離開 sandbox 啦。 然後 yolk 這玩意，是必備的工具，下 % yolk -l 可以把安裝的套件列出來： (sandbox) MacBook-Pro:sandbox clsung$ yolk -l Django &#8211; 1.3 &#8211; active Python &#8211; 2.7.1 &#8211; active development (/usr/local/Cellar/python/2.7.1/lib/python2.7/lib-dynload) distribute &#8211; 0.6.14 &#8211; active pip &#8211; 0.8.3 &#8211; active wsgiref &#8211; 0.1.2 &#8211; active development (/usr/local/Cellar/python/2.7.1/lib/python2.7) yolk &#8211; 0.4.1 &#8211; active 這要幹嘛呢，這可以幫助我們下次做一個新的 sandbox 時（換機器，給 team member 用之類的），就照著安裝就好了。更好的方法是讓 sandbox 可以自動安裝。這時換回 pip ，下 % pip freeze &#62; req.txt 就可以看到 req.txt 有 Django==1.3 distribute==0.6.14 wsgiref==0.1.2 yolk==0.4.1 那麼假如我們要弄個叫 starbucks 的環境，就只要在 pip install 時加個 -r req.txt 就可以啦： % pip install -E starbucks -r req.txt 介紹完畢！ Update 2011/12/23: virtualenv 1.7 版將 &#8216;&#8211;no-site-packages&#8217; 設為 default ，所以可以不加此參數。]]></description>
			<content:encoded><![CDATA[<p><a href="http://python.org/">Python</a> 提供了 <a href="http://www.pip-installer.org/">pip</a> 可以安裝 <a href="http://pypi.python.org/pypi/">python packages</a>，也提供了 <a href="http://www.virtualenv.org/en/latest/">virtualenv</a> 可以把 develop 環境獨立出來，所以就在我的 MBP 上試看看。 <span id="more-1007"></span></p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> pip <span class="kw2">install</span> <span class="re5">-U</span> virtualenv<br />
<span class="sy0">%</span> virtualenv <span class="re5">&#8211;no-site-packages</span> <span class="re5">&#8211;distribute</span> sandbox</div>
</div>
<p>注意 &#8216;&#8211;no-site-packages&#8217; 這個參數一定要下，這是為了不要去動到原本的 site-packages 目錄，也就是做 "純“ 的啦。至於 &#8216;&#8211;distribute&#8217; 要不要就看個人，我自己是因為新手，所以就用新出來的選項。所以採用 <a href="http://pypi.python.org/pypi/distribute">distribute</a> 而不是用原本的 <a href="http://pypi.python.org/pypi/setuptools">setuptools</a> 。<br />
在還沒進 <a href="http://www.virtualenv.org/en/latest/">virtualenv</a> 建出來的環境（簡稱：sandbox，雖然英文打起來比中文還長&#8230;..）前，想先安裝任何 python package，比如 "django"，就下</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> pip <span class="kw2">install</span> <span class="re5">-E</span> sandbox django</div>
</div>
<p>要進 sandbox 就在 sandbox 目錄裡下</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> <span class="kw3">source</span> bin<span class="sy0">/</span>active</div>
</div>
<p>這時候就可以當做沒有 <a href="http://www.virtualenv.org/en/latest/">virtualenv</a> 這東西啦，要亂搞任你玩，不過風險責任當然也是自負啦。玩完要閃了，就下</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> deactivate</div>
</div>
<p>就可以離開 sandbox 啦。<br />
然後 <a href="http://pypi.python.org/pypi/yolk">yolk</a> 這玩意，是必備的工具，下</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> yolk <span class="re5">-l</span></div>
</div>
<p>可以把安裝的套件列出來：</p>
<blockquote><p>(sandbox) MacBook-Pro:sandbox clsung$ yolk -l<br />
Django          &#8211; 1.3          &#8211; active<br />
Python          &#8211; 2.7.1        &#8211; active development (/usr/local/Cellar/python/2.7.1/lib/python2.7/lib-dynload)<br />
distribute      &#8211; 0.6.14       &#8211; active<br />
pip             &#8211; 0.8.3        &#8211; active<br />
wsgiref         &#8211; 0.1.2        &#8211; active development (/usr/local/Cellar/python/2.7.1/lib/python2.7)<br />
yolk            &#8211; 0.4.1        &#8211; active </p></blockquote>
<p>這要幹嘛呢，這可以幫助我們下次做一個新的 sandbox 時（換機器，給 team member 用之類的），就照著安裝就好了。更好的方法是讓 sandbox 可以自動安裝。這時換回 <a href="http://www.pip-installer.org/">pip</a> ，下</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> pip freeze <span class="sy0">&gt;</span> req.txt</div>
</div>
<p>就可以看到 req.txt 有</p>
<blockquote><p>Django==1.3<br />
distribute==0.6.14<br />
wsgiref==0.1.2<br />
yolk==0.4.1</p></blockquote>
<p>那麼假如我們要弄個叫 starbucks 的環境，就只要在 pip install 時加個 -r req.txt 就可以啦：</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;"><span class="sy0">%</span> pip <span class="kw2">install</span> <span class="re5">-E</span> starbucks <span class="re5">-r</span> req.txt</div>
</div>
<p>介紹完畢！</p>
<p>Update 2011/12/23: virtualenv 1.7 版將 &#8216;&#8211;no-site-packages&#8217; 設為 default ，所以可以不加此參數。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2011/04/04/1007.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Python] Natural Language Toolkit (NLTK) &#8211; Part I</title>
		<link>http://blog.dragon2.net/2009/12/07/909.php</link>
		<comments>http://blog.dragon2.net/2009/12/07/909.php#comments</comments>
		<pubDate>Mon, 07 Dec 2009 09:40:09 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[natural language processing]]></category>
		<category><![CDATA[nlp]]></category>
		<category><![CDATA[nltk]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/2009/12/07/909.php</guid>
		<description><![CDATA[對於自然語言處理有興趣的人一定要碰的 Natural Language Toolkit (NLTK) 是用 Python 寫的&#8230;. 本身它就有詳細的文件及書可以參考，所以當然不用從頭介紹。 現在老師出了一個題目，要你算出某些文章的詞頻分佈，還有更進一步的統計。 而學生要做的第一件事，就是把文章可以讀進來。能讀取 corpora 是 NLP 的一小步，可是沒有這一小步，就好像上籃不墊步，很容易被吹走步。&#8230;.. NLTK 提供了幾種 corpus 讀取的方式，可以由書中第二章找到。因為我們要處理的資料是由手邊可得的文件亂選出來的，所以符合的 corpus 種類為 isolated。這部份可以參考的程式碼，可以看看 NLTK 是怎麼處理 webtext 的。那我們要用，就要如法炮製一下囉： 我選的是 nytimes 的新聞，因此把文章收集起來，全部丟到 ~/nltk_data/corpus/nyt 下，並加上如下的程式碼就可以操作 nyt 的文章了。 from nltk.corpus.util import LazyCorpusLoader from nltk.corpus.reader import * nyt = LazyCorpusLoader&#40; &#160; &#160; &#8216;nyt&#8217;, PlaintextCorpusReader, r&#8216;(?!README&#124;.).*&#8217;&#41; LazyCorpusLoader 讓你可以讀取任意的資料，&#8217;nyt&#8217; 指定的是在 ~/nltk_data/corpus 下的目錄， PlaintextCorpusReader 指定的就是 isolated corpus ，而最後面就是有符合該 regex pattern 的檔案了。 當然，有別的作法，書中都有，Chapter 3 就有足夠的範例了。可以自行讀入 raw data，再用 nltk.word_tokenize() 轉化也行，以開檔案來說： import nltk, re, pprint f = open&#40;&#8216;document.txt&#8217;&#41; raw = f.read&#40;&#41; tokens = nltk.word_tokenize&#40;raw&#41; 第一步，就這樣。]]></description>
			<content:encoded><![CDATA[<p>對於自然語言處理有興趣的人一定要碰的 Natural Language Toolkit (NLTK) 是用 Python 寫的&#8230;.<span id="more-909"></span></p>
<p>本身它就有詳細的<a href="http://www.nltk.org/documentation">文件</a>及<a href="http://www.nltk.org/book">書</a>可以參考，所以當然不用從頭介紹。</p>
<p>現在老師出了一個題目，要你算出某些文章的詞頻分佈，還有更進一步的統計。</p>
<p>而學生要做的第一件事，就是把文章可以讀進來。能讀取 corpora 是 NLP 的一小步，可是沒有這一小步，就好像上籃不墊步，很容易被吹走步。&#8230;..</p>
<p>NLTK 提供了幾種 corpus 讀取的方式，可以由<a href="http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html">書中第二章</a><a href="http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html#fig-text-corpus-structure">找到</a>。因為我們要處理的資料是由手邊可得的文件亂選出來的，所以符合的 corpus 種類為 isolated。這部份可以參考的程式碼，可以看看 NLTK 是怎麼<a href="http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html#web-and-chat-text">處理 webtext</a> 的。那我們要用，就要如法炮製一下囉：</p>
<p>我選的是 nytimes 的新聞，因此把文章收集起來，全部丟到 ~/nltk_data/corpus/nyt 下，並加上如下的程式碼就可以操作 nyt 的文章了。</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="kw1">from</span> nltk.<span class="me1">corpus</span>.<span class="me1">util</span> <span class="kw1">import</span> LazyCorpusLoader<br />
<span class="kw1">from</span> nltk.<span class="me1">corpus</span>.<span class="me1">reader</span> <span class="kw1">import</span> <span class="sy0">*</span></p>
<p>nyt = LazyCorpusLoader<span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;nyt&#8217;</span>, PlaintextCorpusReader, r<span class="st0">&#8216;(?!README|<span class="es0">.</span>).*&#8217;</span><span class="br0">&#41;</span></div>
</div>
<p>LazyCorpusLoader 讓你可以讀取任意的資料，&#8217;nyt&#8217; 指定的是在 ~/nltk_data/corpus 下的目錄， PlaintextCorpusReader 指定的就是 isolated corpus ，而最後面就是有符合該 regex pattern 的檔案了。</p>
<p>當然，有別的作法，書中都有，<a href="http://nltk.googlecode.com/svn/trunk/doc/book/ch03.html">Chapter 3</a> 就有足夠的範例了。可以自行讀入 raw data，再用  nltk.word_tokenize() 轉化也行，以開檔案來說：</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="kw1">import</span> nltk, <span class="kw3">re</span>, <span class="kw3">pprint</span></p>
<p>f = <span class="kw2">open</span><span class="br0">&#40;</span><span class="st0">&#8216;document.txt&#8217;</span><span class="br0">&#41;</span><br />
raw = f.<span class="me1">read</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
tokens = nltk.<span class="me1">word_tokenize</span><span class="br0">&#40;</span>raw<span class="br0">&#41;</span></div>
</div>
<p>第一步，就這樣。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2009/12/07/909.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Modules Intro 20091123</title>
		<link>http://blog.dragon2.net/2009/11/23/896.php</link>
		<comments>http://blog.dragon2.net/2009/11/23/896.php#comments</comments>
		<pubDate>Mon, 23 Nov 2009 15:13:00 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[python modules]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/2009/11/23/896.php</guid>
		<description><![CDATA[神奇的 PyMOTW 模組&#8230; PyMOTW 是 PYthon Module Of The Week 的簡寫。為什麼說它神奇呢，因為這算是個教學的模組，你不能拿它直接寫/用 Python 程式，可是你可以經由它學習如何寫 Python。它的簡介是這麼說的: The Python Module of the Week series, or PyMOTW, is a tour of the Python standard library through short examples. 換中文說，就是讓你由範例中學習 Python 的標準函式庫。當然，它也有網頁版(我不確定這樣講對不對 :p)，最近幾期的內容集中在 sys 這個 module，目前(20091123)內容是 Modules and Imports。 不過就我個人而言，用 RSS 訂比 d/l 下來看(compile? :p)還來得方便。]]></description>
			<content:encoded><![CDATA[<p>神奇的 <a href="http://www.doughellmann.com/PyMOTW/">PyMOTW</a> 模組&#8230; <span id="more-896"></span><br />
<a href="http://www.doughellmann.com/PyMOTW/">PyMOTW</a> 是 PYthon Module Of The Week 的簡寫。為什麼說它神奇呢，因為這算是個教學的模組，你不能拿它直接寫/用 <a href="http://www.python.org/">Python</a> 程式，可是你可以經由它學習如何寫 <a href="http://www.python.org/">Python</a>。它的簡介是這麼說的:</p>
<blockquote><p>The Python Module of the Week series, or PyMOTW, is a tour of the Python standard library through short examples. </p></blockquote>
<p>換中文說，就是讓你由範例中學習 <a href="http://www.python.org/">Python</a> 的標準函式庫。當然，它也有網頁版(我不確定這樣講對不對 :p)，最近幾期的內容集中在 <a href="http://docs.python.org/library/sys.html">sys</a> 這個 module，目前(20091123)內容是 <a href="http://blog.doughellmann.com/2009/11/pymotw-sys-part-7-modules-and-imports.html" title="PyMOTW: sys, Part 7: Modules and Imports">Modules and Imports</a>。</p>
<p>不過就我個人而言，用 RSS 訂比 d/l 下來看(compile? :p)還來得方便。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2009/11/23/896.php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyLucene Win32 on googlecode</title>
		<link>http://blog.dragon2.net/2008/04/10/525.php</link>
		<comments>http://blog.dragon2.net/2008/04/10/525.php#comments</comments>
		<pubDate>Thu, 10 Apr 2008 06:34:26 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pylucene]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/?p=525</guid>
		<description><![CDATA[之前要相容地使用 Lucene 的 Index ，所以花了不上功夫在 FreeBSD 上玩 PyLucene。 為了在 Win32 上也可以用 PyLucene，先是去 download 了 python ，再試圖由這裡下載 binary 檔，沒想到竟然一個說我區域不對（這裡不是北京啊～～～），一個是根本連不到。不死心再 google 了一下，發現了這個專案，不錯。 是說 PyLucene 竟然沒有 2.3 的 source tar ball，不知道是在幹嘛？這樣我很難 update textproc/py-lucene 啊。]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.dragon2.net/2006/11/14/392.php">之前</a>要相容地使用 <a href="http://lucene.apache.org/" title="Welcome to Lucene!<br />
">Lucene</a> 的 Index ，所以花了不上功夫在 <a href="http://www.freebsd.org" title="FreeBSD">FreeBSD</a> 上玩 <a href="http://pylucene.osafoundation.org/" title="PyLucene project">PyLucene</a>。<br />
<span id="more-525"></span><br />
為了在 Win32 上也可以用 <a href="http://pylucene.osafoundation.org/" title="PyLucene project">PyLucene</a>，先是去 download 了 <a href="http://www.python.org/" title="Python">python</a> ，再試圖由<a href="http://charupload.wordpress.com/2007/12/28/pylucene-gcj-220-1-for-win32-python-25/" title="PyLucene GCJ 2.2.0-1 for win32, python 2.5">這裡</a>下載 binary 檔，沒想到竟然一個說我區域不對（這裡不是北京啊～～～），一個是根本連不到。不死心再 google 了一下，發現了<a href="http://code.google.com/p/pylucene-win32-binary/" title="PyLucene binaries for win32">這個專案</a>，不錯。</p>
<p>是說 <a href="http://pylucene.osafoundation.org/" title="PyLucene project">PyLucene</a> 竟然沒有 <a href="http://svn.osafoundation.org/pylucene/branches/release-2.3/" title="PyLucene Release-2.3">2.3</a> 的 source tar ball，不知道是在幹嘛？這樣我很難 update <a href="http://www.freshports.org/textproc/py-lucene" title="FreshPorts -- textproc/py-lucene">textproc/py-lucene</a> 啊。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2008/04/10/525.php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Perl 屬於專業編程語言</title>
		<link>http://blog.dragon2.net/2007/01/17/425.php</link>
		<comments>http://blog.dragon2.net/2007/01/17/425.php#comments</comments>
		<pubDate>Wed, 17 Jan 2007 13:59:52 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/2007/01/17/425.php</guid>
		<description><![CDATA[是的，不要懷疑，就如同標題所說的，「Perl 屬於專業編程語言」。看標題就知道這些日子以來我並沒有因為學了 python 與 ruby 就把 Perl 丟到一旁(1) 。 為什麼「Perl 屬於專業編程語言」？原因很簡單，其一：阿西摩寫了這個。裡面提到了一句經典：＂Java 屬於工業編程語言＂；其二，thegiive 發表了這篇，公告了＂以後 Ruby 正式成為商業編程語言＂。當然， python 這裡也有大師級的開講與感想。 所以，身為 Perl 使用者(2) ，不得不站出來講一下話了（php 實在是太少用了 :~）。 我也決定，從今天開始，公告「Perl 屬於專業編程語言」。 謝謝大家。 看我多重視，Perl 的 "P" 還大寫哦是嗎？可是分類裡沒有 Perl 耶]]></description>
			<content:encoded><![CDATA[<p>是的，不要懷疑，就如同標題所說的，「<strong>Perl 屬於<font color="red">專業</font>編程語言</strong>」。看標題就知道這些日子以來我並沒有因為學了 <a href="http://blog.dragon2.net/2006/11/14/392.php" title="快快樂樂學 Python - 由 Ferret 與 PyLucene 談起">python</a> 與 <a href="http://blog.dragon2.net/2007/01/12/421.php" title="[ruby] 第一支 ruby 程式">ruby</a> 就把 Perl 丟到一旁<sup>(<a href="http://blog.dragon2.net/2007/01/17/425.php#footnote_0_425" id="identifier_0_425" class="footnote-link footnote-identifier-link" title="看我多重視，Perl 的 &quot;P&quot; 還大寫哦">1</a>)</sup> 。<br />
<span id="more-425"></span><br />
為什麼「<strong>Perl 屬於<font color="red">專業</font>編程語言</strong>」？原因很簡單，其一：<a href="http://www.one18.com/">阿西摩</a>寫了<a href="http://www.one18.com/?p=109" title="JavaScript ≠ Java">這個</a>。裡面提到了一句經典：＂Java 屬於工業編程語言＂；其二，<a href="http://lightyror.thegiive.net/">thegiive</a> 發表了<a href="http://lightyror.thegiive.net/2007/01/blog-post_16.html" title=" 商業性語言？">這篇</a>，公告了＂以後 Ruby 正式成為商業編程語言＂。當然， python 這裡也有大師級的<a href="http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/203" title="scirping language?">開講</a>與<a href="http://blog.seety.org/everydaywork/archive/635/" title="有時候換個名詞，感覺起來就會不大一樣">感想</a>。</p>
<p>所以，身為 Perl 使用者<sup>(<a href="http://blog.dragon2.net/2007/01/17/425.php#footnote_1_425" id="identifier_1_425" class="footnote-link footnote-identifier-link" title="是嗎？可是分類裡沒有 Perl 耶">2</a>)</sup> ，不得不站出來講一下話了（php 實在是太少用了 :~）。</p>
<p>我也決定，從今天開始，公告「<strong>Perl 屬於<font color="red">專業</font>編程語言</strong>」。</p>
<p>謝謝大家。</p>
<ol class="footnotes"><li id="footnote_0_425" class="footnote">看我多重視，Perl 的 "P" 還大寫哦</li><li id="footnote_1_425" class="footnote">是嗎？可是分類裡沒有 Perl 耶</li></ol>]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2007/01/17/425.php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>快快樂樂學 Python &#8211; 由 Ferret 與 PyLucene 談起</title>
		<link>http://blog.dragon2.net/2006/11/14/392.php</link>
		<comments>http://blog.dragon2.net/2006/11/14/392.php#comments</comments>
		<pubDate>Tue, 14 Nov 2006 08:45:25 +0000</pubDate>
		<dc:creator>clsung</dc:creator>
				<category><![CDATA[phd_student]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[pylucene]]></category>

		<guid isPermaLink="false">http://blog.dragon2.net/archives/2006/11/14/392.php</guid>
		<description><![CDATA[標題當然是假的，又不是天才，怎麼快快樂樂學 Python 。也不是要出書，只是不知道要下什麼標題。 事情由來是這樣的，我在 survey 在 FreeBSD 上可以 run 的 lucene 程式，原本中意的是 ferret(1) ，不過看了一下文件發現 API 不相容 不能讀 dotLucene(2) 的 index ，其實意思就是跟原生的 Java lucene 不相容啦 基於上述兩個理由，我只好含淚斬 Ruby 了。不然上面那個標題就會是「快快樂樂學 Ruby」了。 Python 有個 PyLucene 可以用，而它其實有點接近原生版本的 Lucene ，因為 API 大致相容，說大致是因為我才剛學，不敢保證 可以 access dotLucene 的 index file ，這很重要，敝實驗室的流派就是點點點(3) 它是用 gcj 編譯的，感覺會比 Java 快一點，而我也可以確定大部份的功能是相同的。籠統地說，算是 Lucene 的 Python-binding 。 不過說要學個語言不是那麼容易，雖然說比起英語德語加法語來說，學個程式語言比較起來容易太多了，可是沒有個入門文件也是不行的。上網一查「Python」，結果台灣陣亡，第一頁最有用的就是第一筆： Python 教學文件。這可以位在香港的，看來我以前沒學 Python 也不能怪自己，資源少的可憐的感覺，隨隨便便要找個論壇都還來個 404 。 有了入門文件，沒有個中文文件是不行的。在我安裝了 PyLucene 之後，發現要查找中文還真是不可能的任務。程式一 run 下去就來個 SyntaxError: Non-ASCII character &#8216;xxx&#8217; in file xxxx.py on line xx, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details 的訊息，當然人家在 error message 都給了資料了，不去看一下也不對吧。所以，找出原因，只要在程式碼中加入 # -*- coding: utf-8 -*- 就沒事了。&#8230;當然不會就這樣沒事，結果你不找問題，問題還是會找上你： UnicodeEncodeError: &#8216;ascii&#8217; codec can&#8217;t encode character &#8230;.. 這就糗大了，它又沒說要怎麼辦？只好再次求助 Google 了。結論是，在字串後面加上 .encode("utf-8&#8243;) 就可以了。（現在大家都老大不小了，應該改用 utf-8 了吧） 當然在做 Text Processing 的人，少不了要用到 Regular Expression，那入門文件當然是沒有啦。目前我是正在看這裡，如果有人可以指點一二的，也麻煩告訴我一下吧。 先就這樣吧，反正趁有點小空的時候來學學 Python 也不錯，希望以後可以快快樂樂啦。 Reference： Python 教學文件 How to Use UTF-8 with Python Regular Expression HOWTO PyLucene project Ruby &#8211; Ferret [tags] python, ferret, pylucene, lucene [/tags] ruby 版的 lucene dotNet 版的 lucene甚麼？不不不，我們不是兄弟象]]></description>
			<content:encoded><![CDATA[<p>標題當然是假的，又不是天才，怎麼快快樂樂學 Python 。也不是要出書，只是不知道要下什麼標題。</p>
<p>事情由來是這樣的，我在 survey 在 FreeBSD 上可以 run 的 lucene 程式，原本中意的是 ferret<sup>(<a href="http://blog.dragon2.net/2006/11/14/392.php#footnote_0_392" id="identifier_0_392" class="footnote-link footnote-identifier-link" title="ruby 版的 lucene">1</a>)</sup> ，不過看了一下文件發現 </p>
<ol>
<li>API 不相容</li>
<li>不能讀 dotLucene<sup>(<a href="http://blog.dragon2.net/2006/11/14/392.php#footnote_1_392" id="identifier_1_392" class="footnote-link footnote-identifier-link" title=" dotNet 版的 lucene">2</a>)</sup> 的 index ，其實意思就是跟原生的 Java lucene 不相容啦</li>
</ol>
<p>基於上述兩個理由，我只好含淚斬 Ruby 了。不然上面那個標題就會是「快快樂樂學 Ruby」了。</p>
<p>Python 有個 PyLucene 可以用，而它其實有點接近原生版本的 Lucene ，因為</p>
<ol>
<li>API 大致相容，說大致是因為我才剛學，不敢保證</li>
<li>可以 access dotLucene 的 index file ，這很重要，敝實驗室的流派就是點點點<sup>(<a href="http://blog.dragon2.net/2006/11/14/392.php#footnote_2_392" id="identifier_2_392" class="footnote-link footnote-identifier-link" title="甚麼？不不不，我們不是兄弟象">3</a>)</sup></li>
<li>它是用 gcj 編譯的，感覺會比 Java 快一點，而我也可以確定大部份的功能是相同的。籠統地說，算是 Lucene 的 Python-binding 。</li>
</ol>
<p>不過說要學個語言不是那麼容易，雖然說比起英語德語加法語來說，學個程式語言比較起來容易太多了，可是沒有個入門文件也是不行的。上網一查「Python」，結果台灣陣亡，第一頁最有用的就是第一筆： <a href="http://www.freebsd.org.hk/html/python/tut_tw/tut.html" ttile="Python 教學文件">Python 教學文件</a>。這可以位在香港的，看來我以前沒學 Python 也不能怪自己，資源少的可憐的感覺，隨隨便便要找個論壇都還來個 404 。</p>
<p>有了入門文件，沒有個中文文件是不行的。在我安裝了 PyLucene 之後，發現要查找中文還真是不可能的任務。程式一 run 下去就來個</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="kw2">SyntaxError</span>: Non-ASCII character <span class="st0">&#8216;<span class="es0">x</span>xx&#8217;</span> <span class="kw1">in</span> <span class="kw2">file</span> xxxx.<span class="me1">py</span> on line xx, but no encoding declared<span class="sy0">;</span> see http://www.<span class="me1">python</span>.<span class="me1">org</span>/peps/pep-0263.<span class="me1">html</span> <span class="kw1">for</span> details</div>
</div>
<p>的訊息，當然人家在 error message 都給了資料了，不去看一下也不對吧。所以，找出原因，只要在程式碼中加入</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="co1"># -*- coding: utf-8 -*-</span></div>
</div>
<p>就沒事了。&#8230;當然不會就這樣沒事，結果你不找問題，問題還是會找上你：</p>
<div class="codesnip-container" >UnicodeEncodeError: &#8216;ascii&#8217; codec can&#8217;t encode character &#8230;..</div>
<p>這就糗大了，它又沒說要怎麼辦？只好再次求助 Google 了。結論是，在字串後面加上 .encode("utf-8&#8243;) 就可以了。（現在大家都老大不小了，應該改用 utf-8 了吧）</p>
<p>當然在做 Text Processing 的人，少不了要用到 Regular Expression，那入門文件當然是沒有啦。目前我是正在看<a href="http://www.amk.ca/python/howto/regex/" title="Regular Expression HOWTO">這裡</a>，如果有人可以指點一二的，也麻煩告訴我一下吧。 <img src='http://blog.dragon2.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>先就這樣吧，反正趁有點小空的時候來學學 Python 也不錯，希望以後可以快快樂樂啦。</p>
<p>Reference：</p>
<ul>
<li><a href="http://www.freebsd.org.hk/html/python/tut_tw/tut.html">Python 教學文件</a></li>
<li><a href="http://evanjones.ca/python-utf8.html" title="How to Use UTF-8 with Python">How to Use UTF-8 with Python</a></li>
<li><a href="http://www.amk.ca/python/howto/regex/" title="Regular Expression HOWTO">Regular Expression HOWTO</a></li>
<li><a href="http://pylucene.osafoundation.org/" title="PyLucene project">PyLucene project</a></li>
<li><a href="http://ferret.davebalmain.com/" title="Ferret is a high-performance, full-featured text search engine library written for Ruby">Ruby &#8211; Ferret</a></li>
</ul>
<p>[tags] python, ferret, pylucene, lucene [/tags]</p>
<ol class="footnotes"><li id="footnote_0_392" class="footnote">ruby 版的 lucene</li><li id="footnote_1_392" class="footnote"> dotNet 版的 lucene</li><li id="footnote_2_392" class="footnote">甚麼？不不不，我們不是兄弟象</li></ol>]]></content:encoded>
			<wfw:commentRss>http://blog.dragon2.net/2006/11/14/392.php/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

