<?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://sunliwen.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://sunliwen.com</link>
	<description>Never lose my passion!</description>
	<lastBuildDate>Sat, 21 Jan 2012 05:16:52 +0000</lastBuildDate>
	<language>zh</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Python处理Last-Modified</title>
		<link>http://sunliwen.com/2011/11/python-handle-last-modified/</link>
		<comments>http://sunliwen.com/2011/11/python-handle-last-modified/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 18:19:00 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1702</guid>
		<description><![CDATA[Python检查某个URI是否有修改，可以检查HTTP Response的Last-Modified字段 import urllib2 import datetime from urllib2 import HTTPError import datetime def check_modified&#40;url, last_modified, field_name=&#34;Last-Modified&#34;&#41;: request = urllib2.Request&#40;url&#41; opener = urllib2.build_opener&#40;&#41; request.add_header&#40;'If-Modified-Since', last_modified&#41; try: resp = opener.open&#40;request&#41; print &#34;Last-Modified: &#34; + resp.headers.get&#40;field_name&#41; except HTTPError as e: if 304 == e.code: print &#34;Not Modified Since: &#34; + last_modified &#160; last_modified = datetime.datetime&#40;2011,11,15,0,0&#41;.strftime&#40;&#34;%a, %d %h %Y [...]]]></description>
			<content:encoded><![CDATA[<p>Python检查某个URI是否有修改，可以检查HTTP Response的Last-Modified字段</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">urllib2</span> <span style="color: #ff7700;font-weight:bold;">import</span> HTTPError
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">def</span> check_modified<span style="color: black;">&#40;</span>url, last_modified, field_name=<span style="color: #483d8b;">&quot;Last-Modified&quot;</span><span style="color: black;">&#41;</span>:
    request = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span> 
    opener = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
    request.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'If-Modified-Since'</span>, last_modified<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        resp = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Last-Modified: &quot;</span> + resp.<span style="color: black;">headers</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>field_name<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> HTTPError <span style="color: #ff7700;font-weight:bold;">as</span> e:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff4500;">304</span> == e.<span style="color: #dc143c;">code</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Not Modified Since: &quot;</span> + last_modified
&nbsp;
last_modified = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2011</span>,<span style="color: #ff4500;">11</span>,<span style="color: #ff4500;">15</span>,<span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%a, %d %h %Y %H:%M:%S GMT&quot;</span><span style="color: black;">&#41;</span>
check_modified<span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.kuaishubao.com'</span>, last_modified<span style="color: black;">&#41;</span></pre></div></div>

<p>有的服务器并不返回Last-Modified, 所以需要看情况处理，比如</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">check_modified<span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://sunliwen.com'</span>, last_modified, field_name=<span style="color: #483d8b;">&quot;date&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>参考：<a href="http://www.faqs.org/rfcs/rfc2616.html">http://www.faqs.org/rfcs/rfc2616.html</a></p>
<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="ckepop">
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_douban"></a>
	<a class="jiathis_button_instapaper"></a>
	<a class="jiathis_button_stubleupon"></a>
	<a href="http://www.jiathis.com/share?uid=1544311" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">var jiathis_config = {data_track_clickback:true};</script>
<script type="text/javascript" src="http://v2.jiathis.com/code_mini/jia.js?uid=1544311" charset="utf-8"></script>
<!-- JiaThis Button END --></div><hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/http/" rel="tag">HTTP</a>, <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a><br/>
<a href="http://sunliwen.com/2011/11/python-handle-last-modified/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/11/python-handle-last-modified/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用Python Decorator简化代码</title>
		<link>http://sunliwen.com/2011/10/python-decorator/</link>
		<comments>http://sunliwen.com/2011/10/python-decorator/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 06:39:20 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1508</guid>
		<description><![CDATA[看码不说话。 def deco&#40;callable&#41;: def method&#40;a, b&#41;: print &#34;a: %s&#34; % a print &#34;b: %s&#34; % b return callable&#40;a, b&#41; return method &#160; @deco def test1&#40;a,b&#41;: print a+b &#160; test1&#40;10,20&#41; 上面的Decorator只能装饰有两个参数的函数，否则 @deco def test2&#40;a,b,c&#41;: print a+b+c 会报错 &#62;&#62;&#62; test2&#40;10,20,30&#41; Traceback &#40;most recent call last&#41;: File &#34;&#60;stdin&#62;&#34;, line 1, in &#60;module&#62; TypeError: method&#40;&#41; takes exactly 2 arguments [...]]]></description>
			<content:encoded><![CDATA[<p>看码不说话。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> deco<span style="color: black;">&#40;</span><span style="color: #008000;">callable</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;a: %s&quot;</span> <span style="color: #66cc66;">%</span> a
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;b: %s&quot;</span> <span style="color: #66cc66;">%</span> b
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> method
&nbsp;
@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test1<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b
&nbsp;
test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span></pre></div></div>

<p>上面的Decorator只能装饰有两个参数的函数，否则</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test2<span style="color: black;">&#40;</span>a,b,c<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b+c</pre></div></div>

<p>会报错</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span>,<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #ff4500;">1</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>module<span style="color: #66cc66;">&gt;</span>
<span style="color: #008000;">TypeError</span>: method<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> takes exactly <span style="color: #ff4500;">2</span> arguments <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span> given<span style="color: black;">&#41;</span></pre></div></div>

<p>如需支持装饰任意参数的函数，得使用以下方法：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> deco<span style="color: black;">&#40;</span><span style="color: #008000;">callable</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> method<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kws<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;args: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;kws: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>kws<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kws<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> method
&nbsp;
@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test1<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b
&nbsp;
@deco
<span style="color: #ff7700;font-weight:bold;">def</span> test2<span style="color: black;">&#40;</span>a,b,c<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a+b+c
&nbsp;
test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span>,<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span>,c=<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span></pre></div></div>

<p>结果是：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">30</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> test1<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;">20</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">30</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span>,<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">20</span>, <span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">60</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> test2<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,b=<span style="color: #ff4500;">20</span>,c=<span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
args: <span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: black;">&#41;</span>
args: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'c'</span>: <span style="color: #ff4500;">30</span>, <span style="color: #483d8b;">'b'</span>: <span style="color: #ff4500;">20</span><span style="color: black;">&#125;</span>
<span style="color: #ff4500;">60</span></pre></div></div>

<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="ckepop">
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_douban"></a>
	<a class="jiathis_button_instapaper"></a>
	<a class="jiathis_button_stubleupon"></a>
	<a href="http://www.jiathis.com/share?uid=1544311" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">var jiathis_config = {data_track_clickback:true};</script>
<script type="text/javascript" src="http://v2.jiathis.com/code_mini/jia.js?uid=1544311" charset="utf-8"></script>
<!-- JiaThis Button END --></div><hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a><br/>
<a href="http://sunliwen.com/2011/10/python-decorator/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/10/python-decorator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ubuntu下安装PHP-FPM</title>
		<link>http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/</link>
		<comments>http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 17:28:13 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[Ops]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://sunliwen.com/?p=1344</guid>
		<description><![CDATA[把VPS的Lenny5换成了Ubuntu10.04，借这个机会试一下效率更高的 PHP-FPM 。 以下是fabric脚本： def init_nmp&#40;&#41;: install_nginx&#40;&#41; install_php&#40;&#41; install_phpfpm&#40;&#41; &#160; def install_nginx&#40;&#41;: sudo&#40;&#34;add-apt-repository ppa:nginx/stable&#34;&#41; sudo&#40;&#34;apt-get update&#34;&#41; sudo&#40;&#34;apt-get -y -q install nginx-full nginx-common&#34;&#41; &#160; def install_php&#40;&#41;: # More packages upon request sudo&#40;&#34;apt-get -y -q --force-yes install php5-cli php5-cgi php5-mysql&#34;&#41; sudo&#40;&#34;apt-get -y -q --force-yes install php5-mcrypt libmcrypt mcrypt&#34;&#41; &#160; def install_phpfpm&#40;&#41;: # TODO: use canonical php-fpm package [...]]]></description>
			<content:encoded><![CDATA[<p>把VPS的Lenny5换成了Ubuntu10.04，借这个机会试一下效率更高的 <a href="http://php-fpm.org">PHP-FPM</a> 。</p>
<p>以下是<a href="http://fabfile.org">fabric</a>脚本：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> init_nmp<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    install_nginx<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    install_php<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    install_phpfpm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> install_nginx<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;add-apt-repository ppa:nginx/stable&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get update&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q install nginx-full nginx-common&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> install_php<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># More packages upon request</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q --force-yes install php5-cli php5-cgi php5-mysql&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q --force-yes install php5-mcrypt libmcrypt mcrypt&quot;</span><span style="color: black;">&#41;</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> install_phpfpm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># TODO: use canonical php-fpm package when available</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;add-apt-repository ppa:brianmercer/php&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get update&quot;</span><span style="color: black;">&#41;</span>
    sudo<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;apt-get -y -q install php5-fpm&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>在<code>/etc/php5/fpm/php5-fpm.conf</code>里将tcp修改成unix socket方式，单机情况下性能更好。</p>

<div class="wp_syntax"><div class="code"><pre class="config" style="font-family:monospace;">listen = /var/run/php5-fpm.sock
;listen = 127.0.0.1:9000</pre></div></div>

<p>Nginx里的配置:</p>

<div class="wp_syntax"><div class="code"><pre class="config" style="font-family:monospace;">location ~ \.php$ { 
    try_files $uri = 404; 
    include /etc/nginx/fastcgi_params; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
&nbsp;
    fastcgi_split_path_info         ^(.+\.php)(.*)$; 
    include fastcgi_params; 
    fastcgi_intercept_errors        on; 
    fastcgi_ignore_client_abort     off; 
    fastcgi_connect_timeout         60; 
    fastcgi_send_timeout            180; 
    fastcgi_read_timeout            180; 
    fastcgi_buffer_size             128k; 
    fastcgi_buffers             4   256k; 
    fastcgi_busy_buffers_size       256k; 
    fastcgi_temp_file_write_size    256k; 
}</pre></div></div>

<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="ckepop">
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_douban"></a>
	<a class="jiathis_button_instapaper"></a>
	<a class="jiathis_button_stubleupon"></a>
	<a href="http://www.jiathis.com/share?uid=1544311" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">var jiathis_config = {data_track_clickback:true};</script>
<script type="text/javascript" src="http://v2.jiathis.com/code_mini/jia.js?uid=1544311" charset="utf-8"></script>
<!-- JiaThis Button END --></div><hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/nginx/" rel="tag">Nginx</a>, <a href="http://sunliwen.com/tag/ops/" rel="tag">Ops</a>, <a href="http://sunliwen.com/tag/php/" rel="tag">PHP</a>, <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a>, <a href="http://sunliwen.com/tag/ubuntu/" rel="tag">Ubuntu</a><br/>
<a href="http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2011/09/fabric-install-nginx-phpfpm-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>通过RopeVIM在VIM中实现Python自动完成</title>
		<link>http://sunliwen.com/2009/12/ropevim-rope-in-vim/</link>
		<comments>http://sunliwen.com/2009/12/ropevim-rope-in-vim/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 18:45:13 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[VIM]]></category>

		<guid isPermaLink="false">http://thinklouder.cn/?p=1044</guid>
		<description><![CDATA[ropevim可以实现在vim中自动完成，自动import等功能。 参考: http://rope.sourceforge.net/ropevim.html 下载几个rope，ropevim，ropemode： mkdir ~/install/ropehg hg clone http://bitbucket.org/agr/rope/ ~/install/ropehg/rope hg clone http://bitbucket.org/agr/ropevim/ ~/install/ropehg/ropevim hg clone http://bitbucket.org/agr/ropemode/ ~/install/ropehg/ropemode 可以选择安装： cd ~/install/ropehg/rope python setup.py install cd ~/install/ropehg/ropevim python setup.py install cd ~/install/ropehg/ropemode python setup.py install 或者在 .vimrc 中添加下面的代码: let $PYTHONPATH .= &#34;:/Users/liwen/install/ropehg/rope:/Users/liwen/install/ropehg/ropemode:/Users/liwen/install/ropehg/ropevim&#34; source /Users/liwen/install/ropehg/ropevim/ropevim.vim 配置ropevim的代码如下 let ropevim_codeassist_maxfixes=10 let ropevim_guess_project=1 let ropevim_vim_completion=1 let ropevim_enable_autoimport=1 let ropevim_extended_complete=1 &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>ropevim可以实现在vim中自动完成，自动import等功能。<br />
参考: <a href="http://rope.sourceforge.net/ropevim.html">http://rope.sourceforge.net/ropevim.html</a></p>
<p>下载几个rope，ropevim，ropemode：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> ~<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>ropehg
hg clone http:<span style="color: #000000; font-weight: bold;">//</span>bitbucket.org<span style="color: #000000; font-weight: bold;">/</span>agr<span style="color: #000000; font-weight: bold;">/</span>rope<span style="color: #000000; font-weight: bold;">/</span> ~<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>ropehg<span style="color: #000000; font-weight: bold;">/</span>rope
hg clone http:<span style="color: #000000; font-weight: bold;">//</span>bitbucket.org<span style="color: #000000; font-weight: bold;">/</span>agr<span style="color: #000000; font-weight: bold;">/</span>ropevim<span style="color: #000000; font-weight: bold;">/</span> ~<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>ropehg<span style="color: #000000; font-weight: bold;">/</span>ropevim
hg clone http:<span style="color: #000000; font-weight: bold;">//</span>bitbucket.org<span style="color: #000000; font-weight: bold;">/</span>agr<span style="color: #000000; font-weight: bold;">/</span>ropemode<span style="color: #000000; font-weight: bold;">/</span> ~<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>ropehg<span style="color: #000000; font-weight: bold;">/</span>ropemode</pre></div></div>

<p>可以选择安装：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>ropehg<span style="color: #000000; font-weight: bold;">/</span>rope  
python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>ropehg<span style="color: #000000; font-weight: bold;">/</span>ropevim  
python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>install<span style="color: #000000; font-weight: bold;">/</span>ropehg<span style="color: #000000; font-weight: bold;">/</span>ropemode  
python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>或者在 .vimrc 中添加下面的代码:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">let</span> <span style="color: #000000;">$</span>PYTHONPATH <span style="color: #000000;">.</span>= <span style="color: #C5A22D;">&quot;:/Users/liwen/install/ropehg/rope:/Users/liwen/install/ropehg/ropemode:/Users/liwen/install/ropehg/ropevim&quot;</span>
source <span style="color: #000000;">/</span>Users<span style="color: #000000;">/</span>liwen<span style="color: #000000;">/</span>install<span style="color: #000000;">/</span>ropehg<span style="color: #000000;">/</span>ropevim<span style="color: #000000;">/</span>ropevim<span style="color: #000000;">.</span>vim</pre></div></div>

<p>配置ropevim的代码如下</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">let</span> ropevim_codeassist_maxfixes=<span style="color: #000000; font-weight:bold;">10</span>
<span style="color: #804040;">let</span> ropevim_guess_project=<span style="color: #000000; font-weight:bold;">1</span>
<span style="color: #804040;">let</span> ropevim_vim_completion=<span style="color: #000000; font-weight:bold;">1</span>
<span style="color: #804040;">let</span> ropevim_enable_autoimport=<span style="color: #000000; font-weight:bold;">1</span>
<span style="color: #804040;">let</span> ropevim_extended_complete=<span style="color: #000000; font-weight:bold;">1</span>
&nbsp;
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> CustomCodeAssistInsertMode<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #804040;">call</span> RopeCodeAssistInsertMode<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #804040;">if</span> <span style="color: #25BB4D;">pumvisible</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #804040;">return</span> <span style="color: #C5A22D;">&quot;<span style="">\&lt;</span>C-L&gt;<span style="">\&lt;</span>Down&gt;&quot;</span>
    <span style="color: #804040;">else</span>
        <span style="color: #804040;">return</span> <span style="color: #C5A22D;">''</span>
    <span style="color: #804040;">endif</span>
endfunction
&nbsp;
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> TabWrapperComplete<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #804040;">let</span> cursyn = <span style="color: #25BB4D;">synID</span><span style="color: #000000;">&#40;</span><span style="color: #25BB4D;">line</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'.'</span><span style="color: #000000;">&#41;</span>, <span style="color: #25BB4D;">col</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'.'</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">-</span> <span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>
    <span style="color: #804040;">if</span> <span style="color: #25BB4D;">pumvisible</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #804040;">return</span> <span style="color: #C5A22D;">&quot;<span style="">\&lt;</span>C-Y&gt;&quot;</span>
    <span style="color: #804040;">endif</span>
    <span style="color: #804040;">if</span> <span style="color: #25BB4D;">strpart</span><span style="color: #000000;">&#40;</span><span style="color: #25BB4D;">getline</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'.'</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #25BB4D;">col</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'.'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> =<span style="color: #000000;">~</span> <span style="color: #C5A22D;">'^<span style="">\s</span>*$'</span> <span style="color: #000000;">||</span> cursyn <span style="color: #000000;">!</span>= <span style="color: #000000; font-weight:bold;">0</span>
        <span style="color: #804040;">return</span> <span style="color: #C5A22D;">&quot;<span style="">\&lt;</span>Tab&gt;&quot;</span>
    <span style="color: #804040;">else</span>
        <span style="color: #804040;">return</span> <span style="color: #C5A22D;">&quot;<span style="">\&lt;</span>C-R&gt;=CustomCodeAssistInsertMode()<span style="">\&lt;</span>CR&gt;&quot;</span>
    <span style="color: #804040;">endif</span>
endfunction
&nbsp;
inoremap <span style="color: #000000;">&lt;</span>buffer<span style="color: #000000;">&gt;&lt;</span>silent<span style="color: #000000;">&gt;&lt;</span>expr<span style="color: #000000;">&gt;</span> <span style="color: #000000;">&lt;</span>Tab<span style="color: #000000;">&gt;</span> TabWrapperComplete<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>自动完成直接可以用，但 RopeRename 不工作，可能是工程太大，分起来比较费劲。还不错。 </p>
<p>PS:<br />
1. hg &#8211; <a href="http://mercurial.selenic.com/">mercurial</a><br />
2. 如果遇到以下错误</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">Error detected while processing function LoadRope:
line    1:
E319: Sorry, the command is not available in this version: python &lt;&lt; EOF
line    2:
E492: Not an editor command: import ropevim
line    3:
E492: Not an editor command: EOF
Press ENTER or type command to continue</pre></div></div>

<p>可能由于vim没有启用python支持，可以打开一个buffer，在命令模式下输入</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>python print <span style="color: #C5A22D;">&quot;hello&quot;</span></pre></div></div>

<p>如能正常打印，则配置正确，否则需要重新编译或者安装python支持</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">sudo port install vim +python26
sudo port install mvim +python26</pre></div></div>

<p>Good luck!!</p>
<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="ckepop">
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_douban"></a>
	<a class="jiathis_button_instapaper"></a>
	<a class="jiathis_button_stubleupon"></a>
	<a href="http://www.jiathis.com/share?uid=1544311" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">var jiathis_config = {data_track_clickback:true};</script>
<script type="text/javascript" src="http://v2.jiathis.com/code_mini/jia.js?uid=1544311" charset="utf-8"></script>
<!-- JiaThis Button END --></div><hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a>, <a href="http://sunliwen.com/tag/vim/" rel="tag">VIM</a><br/>
<a href="http://sunliwen.com/2009/12/ropevim-rope-in-vim/#comments-heading">2个评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2009/12/ropevim-rope-in-vim/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>将Python程序编译并转换成Windows可执行程序</title>
		<link>http://sunliwen.com/2009/03/compile-python-to-bytecode-and-py2exe/</link>
		<comments>http://sunliwen.com/2009/03/compile-python-to-bytecode-and-py2exe/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 07:29:56 +0000</pubDate>
		<dc:creator>Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://thinklouder.cn/?p=999</guid>
		<description><![CDATA[因为要在桃园学校的电脑上安装rur-ple，Pockey和Fred翻译了主程序和前三节课程，rur-ple网站的win32安装包没法merge到一起。所以我重新打一个临时的包。 在编译和打包之前需要安装下面的包： Python2.5 http://www.python.org/download/releases/ wxPython 2.6 http://www.wxpython.org/download.php#binaries http://sourceforge.net/projects/wxpython/files/wxPython/2.6.4.0/wxPython2.6-win32-unicode-2.6.4.0-py25.exe/download py2exe 0.6.5 &#8211; 第一个支持Python2.5的版本 http://www.py2exe.org/ 编译程序 参照：http://effbot.org/zone/python-compile.htm 进入要编译的python文件所在目录如下。 D:\sunlw\projects\ygclub\teaching\rurple1.0rc3&#62;python Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type &#34;help&#34;, &#34;copyright&#34;, &#34;credits&#34; or &#34;license&#34; for more information. &#62;&#62;&#62; import compileall &#62;&#62;&#62; compileall.compile_dir(&#34;./&#34;, force=1) 编译完成后运行一下，确认正常。 D:\sunlw\projects\ygclub\teaching\rurple1.0rc3&#62;rur_start.py 貌似运行起来快了一点。 将程序打包成exe 参照：http://www.py2exe.org/index.cgi/Tutorial 添加setup.py文件如下 (http://www.py2exe.org/index.cgi/Tutorial?action=AttachFile&#38;do=view&#38;target=setup.py) from distutils.core [...]]]></description>
			<content:encoded><![CDATA[<p>因为要在桃园学校的电脑上安装rur-ple，Pockey和Fred翻译了主程序和前三节课程，rur-ple网站的win32安装包没法merge到一起。所以我重新打一个临时的包。</p>
<p>在编译和打包之前需要安装下面的包：</p>
<blockquote><p>Python2.5<br />
<a href="http://www.python.org/download/releases/">http://www.python.org/download/releases/</a></p>
<p>wxPython 2.6<br />
<a href="http://www.wxpython.org/download.php#binaries">http://www.wxpython.org/download.php#binaries</a><br />
<a href="http://sourceforge.net/projects/wxpython/files/wxPython/2.6.4.0/wxPython2.6-win32-unicode-2.6.4.0-py25.exe/download">http://sourceforge.net/projects/wxpython/files/wxPython/2.6.4.0/wxPython2.6-win32-unicode-2.6.4.0-py25.exe/download</a></p>
<p>py2exe 0.6.5 &#8211; 第一个支持Python2.5的版本<br />
<a href="http://www.py2exe.org/">http://www.py2exe.org/</a></p></blockquote>
<h3>编译程序</h3>
<p>参照：<a href="http://effbot.org/zone/python-compile.htm">http://effbot.org/zone/python-compile.htm</a></p>
<p>进入要编译的python文件所在目录如下。</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">D:\sunlw\projects\ygclub\teaching\rurple1.0rc3&gt;python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import compileall
&gt;&gt;&gt; compileall.compile_dir(&quot;./&quot;, force=1)</pre></div></div>

<p>编译完成后运行一下，确认正常。</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">D:\sunlw\projects\ygclub\teaching\rurple1.0rc3&gt;rur_start.py</pre></div></div>

<p>貌似运行起来快了一点。</p>
<h3>将程序打包成exe</h3>
<p>参照：<a href="http://www.py2exe.org/index.cgi/Tutorial">http://www.py2exe.org/index.cgi/Tutorial</a><br />
添加setup.py文件如下 (<a href="http://www.py2exe.org/index.cgi/Tutorial?action=AttachFile&amp;do=view&amp;target=setup.py">http://www.py2exe.org/index.cgi/Tutorial?action=AttachFile&amp;do=view&amp;target=setup.py</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup
<span style="color: #ff7700;font-weight:bold;">import</span> py2exe
setup<span style="color: black;">&#40;</span>console=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'rur_start.py'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>然后执行</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">D:\sunlw\projects\ygclub\teaching\rurple1.0rc3&gt;python setup.py py2exe</pre></div></div>

<p>运行exe程序试试</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">D:\sunlw\projects\ygclub\teaching\rurple1.0rc3&gt;.\dist\rur_start.exe</pre></div></div>

<p>成功。</p>
<h3><del datetime="2009-10-22T19:20:32+00:00">安装包</del></h3>
<p><del datetime="2009-10-22T19:20:32+00:00">另外我简单的将编译好的文件用<a href="http://www.7-zip.org/">7zip</a>打成自解压程序。</del></p>
<p>注：<br />
尽量保证依赖的库版本低一些，方便一些安装Windows 2000的电脑使用。</p>
<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="ckepop">
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_douban"></a>
	<a class="jiathis_button_instapaper"></a>
	<a class="jiathis_button_stubleupon"></a>
	<a href="http://www.jiathis.com/share?uid=1544311" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">var jiathis_config = {data_track_clickback:true};</script>
<script type="text/javascript" src="http://v2.jiathis.com/code_mini/jia.js?uid=1544311" charset="utf-8"></script>
<!-- JiaThis Button END --></div><hr />
<p>
<small>
Tags: <a href="http://sunliwen.com/tag/python/" rel="tag">Python</a><br/>
<a href="http://sunliwen.com/2009/03/compile-python-to-bytecode-and-py2exe/#comments-heading">没有评论</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://sunliwen.com/2009/03/compile-python-to-bytecode-and-py2exe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

