<?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>南柯一梦</title>
	<atom:link href="http://www.lfyzjck.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lfyzjck.com</link>
	<description>专注互联网、IT行业</description>
	<lastBuildDate>Wed, 16 May 2012 07:36:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>【转】基于用户投票的排名算法</title>
		<link>http://www.lfyzjck.com/2012-05-16/595.html</link>
		<comments>http://www.lfyzjck.com/2012-05-16/595.html#comments</comments>
		<pubDate>Wed, 16 May 2012 07:36:39 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[杂谈]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=595</guid>
		<description><![CDATA[实在不忍心复制粘贴了，附上文章的链接，哪个混蛋以前给我说数学没用的？ 1.基于用户投票的排名算法（一）：Delicious和Hacker News http://www.ruanyifeng.com/blog/2012/02/ranking_algorithm_hacker_news.html 2.基于用户投票的排名算法（二）：Reddit http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_reddit.html &#160; 3.基于用户投票的排名算法（三）：Stack Overflow http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_stack_overflow.html &#160; 4.基于用户投票的排名算法（四）：牛顿冷却定律 http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_newton_s_law_of_cooling.html &#160; 5.基于用户投票的排名算法（五）：威尔逊区间 http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_wilson_score_interval.html &#160; 6.基于用户投票的排名算法（六）：贝叶斯平均 http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_bayesian_average.html 热门日志豆瓣风格的Wordpress主题图书馆自动续借系统数独(soduku)解法——基于Python的实现更换域名到lfyzjck.com各种排序算法(Python实现)]]></description>
			<content:encoded><![CDATA[<p>实在不忍心复制粘贴了，附上文章的链接，哪个混蛋以前给我说数学没用的？<br />
1.基于用户投票的排名算法（一）：Delicious和Hacker News<br />
<a href="http://www.ruanyifeng.com/blog/2012/02/ranking_algorithm_hacker_news.html">http://www.ruanyifeng.com/blog/2012/02/ranking_algorithm_hacker_news.html</a><br />
2.基于用户投票的排名算法（二）：Reddit</p>
<p><a href="http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_reddit.html">http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_reddit.html</a></p>
<p>&nbsp;</p>
<p>3.基于用户投票的排名算法（三）：Stack Overflow</p>
<p><a href="http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_stack_overflow.html">http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_stack_overflow.html</a></p>
<p>&nbsp;</p>
<p>4.基于用户投票的排名算法（四）：牛顿冷却定律</p>
<p><a href="http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_newton_s_law_of_cooling.html">http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_newton_s_law_of_cooling.html</a></p>
<p>&nbsp;</p>
<p>5.基于用户投票的排名算法（五）：威尔逊区间</p>
<p><a href="http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_wilson_score_interval.html">http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_wilson_score_interval.html</a></p>
<p>&nbsp;</p>
<p>6.基于用户投票的排名算法（六）：贝叶斯平均</p>
<p><a href="http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_bayesian_average.html">http://www.ruanyifeng.com/blog/2012/03/ranking_algorithm_bayesian_average.html</a></p>
<h2  class="related_post_title">热门日志</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2011-05-16/521.html" title="豆瓣风格的Wordpress主题">豆瓣风格的Wordpress主题</a></li><li><a href="http://www.lfyzjck.com/2011-04-18/511.html" title="图书馆自动续借系统">图书馆自动续借系统</a></li><li><a href="http://www.lfyzjck.com/2011-01-12/438.html" title="数独(soduku)解法——基于Python的实现">数独(soduku)解法——基于Python的实现</a></li><li><a href="http://www.lfyzjck.com/2010-03-16/310.html" title="更换域名到lfyzjck.com">更换域名到lfyzjck.com</a></li><li><a href="http://www.lfyzjck.com/2011-05-19/530.html" title="各种排序算法(Python实现)">各种排序算法(Python实现)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2012-05-16/595.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>一个见简单的短网址(ShortURL)服务</title>
		<link>http://www.lfyzjck.com/2012-05-10/590.html</link>
		<comments>http://www.lfyzjck.com/2012-05-10/590.html#comments</comments>
		<pubDate>Thu, 10 May 2012 03:50:15 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[shorturl]]></category>
		<category><![CDATA[web.py]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=590</guid>
		<description><![CDATA[基于web.py和sqlite3完成，属于玩具之作，无法承受大流量访问，仅供学习只用。 关于shorturl的生成算法参考自：http://blog.xe28.tk/archives/188/。每个url都会生成一组4个key，默认选择第一个存入数据库，如果出现重复，选择第二个，以此类推。（4个key都被用了！？你今天洗脸了么？） 如果你还没有安装过web.py，请参考这里：http://webpy.org/install； 如果你还没有安装过Python，请按下Ctrl+W。 数据库结构如下： 1 2 3 4 5 6 ##schema.sql CREATE TABLE IF NOT EXISTS `url` &#40; `key` char&#40;6&#41; NOT NULL, `link` varchar&#40;255&#41; NOT NULL, unique&#40;key&#41; &#41; &#160; 该结构仅适用于sqlite，如果是mysql或者pgsql，最好加上一个自增的ID作为主键。 程序一共两个文件，code.py和shorturl.py。 1 2 3 4 5 6 7 8 &#8230; <a href="http://www.lfyzjck.com/2012-05-10/590.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>基于web.py和sqlite3完成，属于玩具之作，无法承受大流量访问，仅供学习只用。</p>
<p>关于shorturl的生成算法参考自：<a href="http://blog.xe28.tk/archives/188/">http://blog.xe28.tk/archives/188/</a>。每个url都会生成一组4个key，默认选择第一个存入数据库，如果出现重复，选择第二个，以此类推。（4个key都被用了！？你今天洗脸了么？）</p>
<p>如果你还没有安装过web.py，请参考这里：<a href="http://webpy.org/install">http://webpy.org/install</a>；</p>
<p>如果你还没有安装过Python，请按下Ctrl+W。</p>
<p>数据库结构如下：</p>

<div class="wp_codebox"><table><tr id="p5905"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p590code5"><pre class="sql" style="font-family:Consolas;">##schema<span style="color: #66cc66;">.</span>sql
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`url`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`key`</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`link`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">unique</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">key</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>该结构仅适用于sqlite，如果是mysql或者pgsql，最好加上一个自增的ID作为主键。<br />
程序一共两个文件，code.py和shorturl.py。</p>

<div class="wp_codebox"><table><tr id="p5906"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code" id="p590code6"><pre class="python" style="font-family:Consolas;"><span style="color: #808080; font-style: italic;">#code.py</span>
<span style="color: #ff7700;font-weight:bold;">import</span> web
<span style="color: #ff7700;font-weight:bold;">from</span> shorturl <span style="color: #ff7700;font-weight:bold;">import</span> short 
&nbsp;
urls = <span style="color: black;">&#40;</span>
	<span style="color: #483d8b;">'/'</span>, <span style="color: #483d8b;">'Index'</span>,
	<span style="color: #483d8b;">'/create/'</span>, <span style="color: #483d8b;">'Create'</span>,
	<span style="color: #483d8b;">'/(<span style="color: #000099; font-weight: bold;">\w</span>{6})'</span>, <span style="color: #483d8b;">'Redirect'</span>,
<span style="color: black;">&#41;</span>
&nbsp;
prefix = <span style="color: #483d8b;">'http://localhost:8080/'</span>
db = web.<span style="color: black;">database</span><span style="color: black;">&#40;</span>dbn=<span style="color: #483d8b;">&quot;sqlite&quot;</span>, db=<span style="color: #483d8b;">&quot;shorturl.db&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> generate_url<span style="color: black;">&#40;</span>short<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">return</span> prefix + short
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> query_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">return</span> db.<span style="color: black;">query</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT * FROM url WHERE key=$key LIMIT 1&quot;</span>, <span style="color: #008000;">vars</span>=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'key'</span>: key<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">list</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Index:
	<span style="color: #ff7700;font-weight:bold;">def</span> GET<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		render = web.<span style="color: black;">template</span>.<span style="color: black;">render</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'templates/'</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> render.<span style="color: black;">index</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Shor URL'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Redirect:
	<span style="color: #ff7700;font-weight:bold;">def</span> GET<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key<span style="color: black;">&#41;</span>:
		target = query_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> web.<span style="color: black;">seeother</span><span style="color: black;">&#40;</span>target<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">link</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> web.<span style="color: black;">notfound</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Create:
	<span style="color: #ff7700;font-weight:bold;">def</span> GET<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		url = web.<span style="color: #008000;">input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">url</span>
		target = short<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
		i = <span style="color: #ff4500;">0</span>
		result = query_key<span style="color: black;">&#40;</span>target<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">link</span> <span style="color: #66cc66;">!</span>= url:
			i += <span style="color: #ff4500;">1</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> i <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">4</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
			result = query_key<span style="color: black;">&#40;</span>target<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
			db.<span style="color: black;">insert</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'url'</span>, key = target<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, link = url<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> generate_url<span style="color: black;">&#40;</span>target<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
	app = web.<span style="color: black;">application</span><span style="color: black;">&#40;</span>urls, <span style="color: #008000;">locals</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	app.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>&nbsp;</p>

<div class="wp_codebox"><table><tr id="p5907"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p590code7"><pre class="python" style="font-family:Consolas;"><span style="color: #808080; font-style: italic;">#shorturl.py</span>
<span style="color: #ff7700;font-weight:bold;">from</span> hashlib <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">md5</span>
&nbsp;
key = <span style="color: #483d8b;">'lfyzjck'</span>
chars = <span style="color: black;">&#91;</span>
		<span style="color: #483d8b;">&quot;a&quot;</span> , <span style="color: #483d8b;">&quot;b&quot;</span> , <span style="color: #483d8b;">&quot;c&quot;</span> , <span style="color: #483d8b;">&quot;d&quot;</span> , <span style="color: #483d8b;">&quot;e&quot;</span> , <span style="color: #483d8b;">&quot;f&quot;</span> , <span style="color: #483d8b;">&quot;g&quot;</span> , <span style="color: #483d8b;">&quot;h&quot;</span> ,  
        <span style="color: #483d8b;">&quot;i&quot;</span> , <span style="color: #483d8b;">&quot;j&quot;</span> , <span style="color: #483d8b;">&quot;k&quot;</span> , <span style="color: #483d8b;">&quot;l&quot;</span> , <span style="color: #483d8b;">&quot;m&quot;</span> , <span style="color: #483d8b;">&quot;n&quot;</span> , <span style="color: #483d8b;">&quot;o&quot;</span> , <span style="color: #483d8b;">&quot;p&quot;</span> ,  
        <span style="color: #483d8b;">&quot;q&quot;</span> , <span style="color: #483d8b;">&quot;r&quot;</span> , <span style="color: #483d8b;">&quot;s&quot;</span> , <span style="color: #483d8b;">&quot;t&quot;</span> , <span style="color: #483d8b;">&quot;u&quot;</span> , <span style="color: #483d8b;">&quot;v&quot;</span> , <span style="color: #483d8b;">&quot;w&quot;</span> , <span style="color: #483d8b;">&quot;x&quot;</span> ,  
        <span style="color: #483d8b;">&quot;y&quot;</span> , <span style="color: #483d8b;">&quot;z&quot;</span> , <span style="color: #483d8b;">&quot;0&quot;</span> , <span style="color: #483d8b;">&quot;1&quot;</span> , <span style="color: #483d8b;">&quot;2&quot;</span> , <span style="color: #483d8b;">&quot;3&quot;</span> , <span style="color: #483d8b;">&quot;4&quot;</span> , <span style="color: #483d8b;">&quot;5&quot;</span> ,  
        <span style="color: #483d8b;">&quot;6&quot;</span> , <span style="color: #483d8b;">&quot;7&quot;</span> , <span style="color: #483d8b;">&quot;8&quot;</span> , <span style="color: #483d8b;">&quot;9&quot;</span> , <span style="color: #483d8b;">&quot;A&quot;</span> , <span style="color: #483d8b;">&quot;B&quot;</span> , <span style="color: #483d8b;">&quot;C&quot;</span> , <span style="color: #483d8b;">&quot;D&quot;</span> ,  
        <span style="color: #483d8b;">&quot;E&quot;</span> , <span style="color: #483d8b;">&quot;F&quot;</span> , <span style="color: #483d8b;">&quot;G&quot;</span> , <span style="color: #483d8b;">&quot;H&quot;</span> , <span style="color: #483d8b;">&quot;I&quot;</span> , <span style="color: #483d8b;">&quot;J&quot;</span> , <span style="color: #483d8b;">&quot;K&quot;</span> , <span style="color: #483d8b;">&quot;L&quot;</span> ,  
        <span style="color: #483d8b;">&quot;M&quot;</span> , <span style="color: #483d8b;">&quot;N&quot;</span> , <span style="color: #483d8b;">&quot;O&quot;</span> , <span style="color: #483d8b;">&quot;P&quot;</span> , <span style="color: #483d8b;">&quot;Q&quot;</span> , <span style="color: #483d8b;">&quot;R&quot;</span> , <span style="color: #483d8b;">&quot;S&quot;</span> , <span style="color: #483d8b;">&quot;T&quot;</span> ,  
        <span style="color: #483d8b;">&quot;U&quot;</span> , <span style="color: #483d8b;">&quot;V&quot;</span> , <span style="color: #483d8b;">&quot;W&quot;</span> , <span style="color: #483d8b;">&quot;X&quot;</span> , <span style="color: #483d8b;">&quot;Y&quot;</span> , <span style="color: #483d8b;">&quot;Z&quot;</span>
		<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> short<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
	result = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
	s = <span style="color: #dc143c;">md5</span><span style="color: black;">&#40;</span>key + url<span style="color: black;">&#41;</span>.<span style="color: black;">hexdigest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:
		hexint = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#91;</span>i <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">8</span>:<span style="color: black;">&#40;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">8</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span> 0x3fffffff
		outChars = <span style="color: #483d8b;">&quot;&quot;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span>:
			index = hexint <span style="color: #66cc66;">&amp;</span> 0x0000003D
			outChars += chars<span style="color: black;">&#91;</span>index<span style="color: black;">&#93;</span>
			hexint = hexint <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">5</span>
		result.<span style="color: black;">append</span><span style="color: black;">&#40;</span>outChars<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> result</pre></td></tr></table></div>

<p>&nbsp;</p>
<h3>如何运行：</h3>

<div class="wp_codebox"><table><tr id="p5908"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p590code8"><pre class="python" style="font-family:Consolas;">python paht/to/<span style="color: #dc143c;">code</span>.<span style="color: black;">py</span> 端口号（默认为<span style="color: #ff4500;">8080</span>）</pre></td></tr></table></div>

<p>然后再浏览器里打开http://localhost:8080/create/?url=XXXX来生成短网址<br />
8080是默认的端口号，XXX是待生成的链接。<br />
&nbsp;</p>
<h3>打包下载</h3>
<p><a href="http://pan.baidu.com/netdisk/singlepublic?fid=171174_726228181">http://pan.baidu.com/netdisk/singlepublic?fid=171174_726228181</a></p>
<h2  class="related_post_title">你可能还对以下日志感兴趣</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2012-05-08/584.html" title="Python中的文件遍历">Python中的文件遍历</a></li><li><a href="http://www.lfyzjck.com/2011-05-19/530.html" title="各种排序算法(Python实现)">各种排序算法(Python实现)</a></li><li><a href="http://www.lfyzjck.com/2011-04-18/511.html" title="图书馆自动续借系统">图书馆自动续借系统</a></li><li><a href="http://www.lfyzjck.com/2011-01-12/438.html" title="数独(soduku)解法——基于Python的实现">数独(soduku)解法——基于Python的实现</a></li><li><a href="http://www.lfyzjck.com/2010-12-14/412.html" title="Python中对字典(dict)和列表(list)的排序">Python中对字典(dict)和列表(list)的排序</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2012-05-10/590.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python中的文件遍历</title>
		<link>http://www.lfyzjck.com/2012-05-08/584.html</link>
		<comments>http://www.lfyzjck.com/2012-05-08/584.html#comments</comments>
		<pubDate>Tue, 08 May 2012 05:00:43 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[yield]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=584</guid>
		<description><![CDATA[再来水一发，关于Python的文件遍历，大概有两种方法，一种是较为便利的os.walk()，还有一种是利用os.listdir()递归遍历。 方法一：利用os.walk os.walk可以自顶向下或者自底向上遍历整个文件树，然后返回一个含有3个元素的tuple，(dirpath, dirnames, filenames)，要注意的是，os.walk()会返回一个generater，所以调用的时候一定要放到for循环中。 1 2 3 4 5 import os def walk_dir&#40;dirname&#41;: for root,dirs,files in os.walk&#40;dirname&#41;: for f in files: yield os.path.join&#40;root, f&#41; &#160; 方法二：利用os.listdir() 这个方法也比较简单，列出目录然后判断是不是文件夹，不是的话直接返回文件路径，是的话递归调用。 1 2 3 4 5 6 7 8 9 10 import os def walk_dir2&#40;dirname&#41;: &#8230; <a href="http://www.lfyzjck.com/2012-05-08/584.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>再来水一发，关于Python的文件遍历，大概有两种方法，一种是较为便利的os.walk()，还有一种是利用os.listdir()递归遍历。</p>
<h3>方法一：利用os.walk</h3>
<p>os.walk可以自顶向下或者自底向上遍历整个文件树，然后返回一个含有3个元素的tuple，(dirpath, dirnames, filenames)，要注意的是，os.walk()会返回一个generater，所以调用的时候一定要放到for循环中。</p>

<div class="wp_codebox"><table><tr id="p58411"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p584code11"><pre class="python" style="font-family:Consolas;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">def</span> walk_dir<span style="color: black;">&#40;</span>dirname<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> root,dirs,files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span>dirname<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> files:
			<span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root, f<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<h3>方法二：利用os.listdir()</h3>
<p>这个方法也比较简单，列出目录然后判断是不是文件夹，不是的话直接返回文件路径，是的话递归调用。</p>

<div class="wp_codebox"><table><tr id="p58412"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p584code12"><pre class="python" style="font-family:Consolas;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">def</span> walk_dir2<span style="color: black;">&#40;</span>dirname<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span>dirname<span style="color: black;">&#41;</span>:
		path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>dirname, d<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
			<span style="color: #808080; font-style: italic;">#note recursive in for...in...</span>
			<span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> walk_dir2<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
				<span style="color: #ff7700;font-weight:bold;">yield</span> f
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			<span style="color: #ff7700;font-weight:bold;">yield</span> path</pre></td></tr></table></div>

<p>&nbsp;<br />
这里值得一提的是，由于函数中使用了yield，所以会被认为是一个generater，递归调用的时候也需要放到for循环中，否则函数并不会被真正执行。最新的Python 3.3中引入了yield from语法，可以解决上面的问题。<br />
&nbsp;</p>
<h3>扩展阅读：</h3>
<p>1.<a href="http://docs.python.org/library/os.html">http://docs.python.org/library/os.html</a><br />
2.<a href="http://stackoverflow.com/questions/6755869/yield-in-a-recursive-function">http://stackoverflow.com/questions/6755869/yield-in-a-recursive-function</a><br />
3.<a href="http://www.lfyzjck.com/2010-10-23/360.html" title="关于Python中的yield">关于python yield用法</a></p>
<h2  class="related_post_title">你可能还对以下日志感兴趣</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2010-10-23/360.html" title="关于Python中的yield">关于Python中的yield</a></li><li><a href="http://www.lfyzjck.com/2012-05-10/590.html" title="一个见简单的短网址(ShortURL)服务">一个见简单的短网址(ShortURL)服务</a></li><li><a href="http://www.lfyzjck.com/2011-05-19/530.html" title="各种排序算法(Python实现)">各种排序算法(Python实现)</a></li><li><a href="http://www.lfyzjck.com/2011-04-18/511.html" title="图书馆自动续借系统">图书馆自动续借系统</a></li><li><a href="http://www.lfyzjck.com/2011-01-12/438.html" title="数独(soduku)解法——基于Python的实现">数独(soduku)解法——基于Python的实现</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2012-05-08/584.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Apache虚拟主机(vhost)配置</title>
		<link>http://www.lfyzjck.com/2012-04-27/575.html</link>
		<comments>http://www.lfyzjck.com/2012-04-27/575.html#comments</comments>
		<pubDate>Fri, 27 Apr 2012 05:32:18 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[操作系统]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Virtual Host]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=575</guid>
		<description><![CDATA[先附上官方的examples：http://httpd.apache.org/docs/2.0/vhosts/examples.html 首先找到Apache的配置文件httpd.conf 确认有下面一行： 1 2 # Virtual hosts Include &#34;conf/extra/httpd-vhosts.conf&#34; 如果没有，请在配置文件末尾添加。然后找到httpd-vhosts.conf添加配置。 复制里面的一个example，进行修改，这里以example.com为例。 1 2 3 4 5 6 7 8 9 10 # # Use name-based virtual hosting. # NameVirtualHost *:80 &#60;virtualhost *:80&#62; ServerAdmin postmaster@dummy-host.localhost DocumentRoot &#34;D:/xampp/htdocs/example&#34; ServerName www.example.com ErrorLog &#8230; <a href="http://www.lfyzjck.com/2012-04-27/575.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>先附上官方的examples：<a href="http://httpd.apache.org/docs/2.0/vhosts/examples.html">http://httpd.apache.org/docs/2.0/vhosts/examples.html</a></p>
<p>首先找到Apache的配置文件httpd.conf</p>
<p>确认有下面一行：</p>

<div class="wp_codebox"><table><tr id="p57517"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p575code17"><pre class="html" style="font-family:Consolas;"># Virtual hosts
Include &quot;conf/extra/httpd-vhosts.conf&quot;</pre></td></tr></table></div>

<p>如果没有，请在配置文件末尾添加。然后找到httpd-vhosts.conf添加配置。</p>
<p>复制里面的一个example，进行修改，这里以example.com为例。</p>

<div class="wp_codebox"><table><tr id="p57518"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p575code18"><pre class="html" style="font-family:Consolas;">#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
&lt;virtualhost *:80&gt;
    ServerAdmin postmaster@dummy-host.localhost
    DocumentRoot &quot;D:/xampp/htdocs/example&quot;
    ServerName www.example.com
    ErrorLog &quot;logs/www.example.com-error.log&quot;
&lt;/virtualhost&gt;</pre></td></tr></table></div>

<p>NameViretualHost是启用基于名字的虚拟主机，若不配置该选项，则只有第一个VirtualHost的配置会生效。</p>
<p>DocumentRoot指向网站的根目录即可，需要注意的是，localhost是指向默认的DocumentRoot，但添加过vhost配置以后，这个配置会失效，所以我们也需要为localhost配置一个虚拟主机，方便本地的调试。继续添加下面的配置：</p>

<div class="wp_codebox"><table><tr id="p57519"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p575code19"><pre class="html" style="font-family:Consolas;">&lt;virtualhost *:80&gt;
    ServerAdmin webmaster@localhost
    DocumentRoot &quot;D:/xampp/htdocs&quot;
    ServerName localhost
    ErrorLog &quot;logs/localhost-error.log&quot;
&lt;/virtualhost&gt;</pre></td></tr></table></div>

<p>若有多个站点，多个IP，可以参照下面的配置：</p>

<div class="wp_codebox"><table><tr id="p57520"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p575code20"><pre class="html" style="font-family:Consolas;">Listen 80
&nbsp;
# This is the &quot;main&quot; server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver
&nbsp;
# This is the other address
NameVirtualHost 172.20.30.50
&nbsp;
&lt;virtualhost 172.20.30.50&gt;
DocumentRoot /www/example1
ServerName www.example1.com
&nbsp;
# Other directives here ...
&nbsp;
&lt;/virtualhost&gt;
&nbsp;
&lt;virtualhost 172.20.30.50&gt;
DocumentRoot /www/example2
ServerName www.example2.org
&nbsp;
# Other directives here ...
&lt;/virtualhost&gt;</pre></td></tr></table></div>

<p>所有不是发送至172.20.30.50的请求都会转发到main server；发送到172.20.30.50的请求，如果HostName未知或者在HTTP报头中没有指定HostName，那么该请求会转发至www.example1.com处理。</p>
<p>配置好之后，重启Apache即可让配置生效。</p>
<h2  class="related_post_title">热门日志</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2011-05-16/521.html" title="豆瓣风格的Wordpress主题">豆瓣风格的Wordpress主题</a></li><li><a href="http://www.lfyzjck.com/2011-04-18/511.html" title="图书馆自动续借系统">图书馆自动续借系统</a></li><li><a href="http://www.lfyzjck.com/2011-01-12/438.html" title="数独(soduku)解法——基于Python的实现">数独(soduku)解法——基于Python的实现</a></li><li><a href="http://www.lfyzjck.com/2010-03-16/310.html" title="更换域名到lfyzjck.com">更换域名到lfyzjck.com</a></li><li><a href="http://www.lfyzjck.com/2011-05-19/530.html" title="各种排序算法(Python实现)">各种排序算法(Python实现)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2012-04-27/575.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>HP Veer入手小记</title>
		<link>http://www.lfyzjck.com/2011-11-18/565.html</link>
		<comments>http://www.lfyzjck.com/2011-11-18/565.html#comments</comments>
		<pubDate>Fri, 18 Nov 2011 14:53:05 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[杂谈]]></category>
		<category><![CDATA[Veer]]></category>
		<category><![CDATA[WebOS]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=565</guid>
		<description><![CDATA[由于之前的触屏手机不能摸了，加上手头紧张，千挑万选之下发现了HP Veer这个机器。到现在为止入手HP Veer有小半个月了，经过几番折腾终于达到能用状态了，写篇文章纪念下。不得不说WebOS是个发育不健全的系统，虽然也有很多优秀的地方。 1、外观 Veer的尺寸很小，只有3.1英寸，大概就是横放在手掌心里，刚好能占据一半的面积。只有你把键盘滑出来，拿在手里才会觉得踏实，否则就有种随时都会从你手中溜走的感觉，以至于现在上厕所都不敢再看手机了。就这点来说，Veer的尺寸和外观的确比较适合女性。右上方有电源/锁屏键，还有一个静音键（我觉得这个有些多余），右侧是usb以及耳机扩展槽的接口。Veer的USB接口设计这里需要单独提下，USB线跟手机连接的一端通过磁石吸附到手机的接口上，很独特而且感觉非常舒服，但仅限感觉。实际用的过程中就会发现，由于没有传统接口那样的插槽，USB连接很不牢固，很容易断开。充电或者跟电脑连接的时候，你不得不小心翼翼的把手机摆好，而且不能乱碰。 再说说键盘吧，按键本身包括按键的间隔都非常小，得用指甲盖才能准确按到，手大的人要尤其注意了。全键盘输入习惯后还算流畅，问题不大。 2、操作体验 这个是WebOS最值得一提的地方了。Veer的硬件还算给力，800MHZ的CPU，512MB RAM + 8GB ROM，配合WebOS操作非常流畅，这点上远胜Android（曾经的Hero用户泪奔）。卡片式的界面很有爱而且确实很方便，手势输入很强大。照相机没有闪光灯，不过拍照质量和速度都不错。最给力的还是游戏了，可玩的比较多，而且画面流畅，对比650元的价格，不得不说性价比超值。（低端Android机在这点可能也要被秒杀了） 3、软件 通讯录和邮件方面都没有问题，用Google Sync或者Exchange都可以轻松搞定，Calendar同步需要打个补丁，理所当然的没有农历。 由于第三方软件相对贫乏，追求便利性的话还是绕道玩Android比较好。当然常用的QQ，Renren，新浪微博的客户端还是有的，地图暂时没找到合适且能用的，于是GPS就成了摆设。 中文输入法虽然差强人意，不过还好有而且能用。HP收购WebOS后大概没考虑过进入中国市场（当然现在更加不可能），WebOS完全没有针对中文用户的优化（我本来想用“几乎”的，但仔细想想确实没有）。界面想改成中文也需要装个蛋疼的补丁，而且会引发其他奇怪的问题。所以我很淡定的到现在也还在看着英文界面。 浏览器目前还没第三方的，自带的效果好但是耗流量，看看专门手机版的网页还是没什么压力。对我这种打开浏览器只看GR的人来说，更是无所谓的事情。J2ME版的UC虽然能用，但也是一蛋疼的货，嫌麻烦的直接忽略吧。 最后来吐槽下Veer的重力感应系统，系统没有提供可以关闭重力感应的地方，于是有时候侧躺下看手机的时候，竖着拿手机屏幕会横过来，横着拿手机屏幕会竖过来，然后就有点想骂人的冲动。 4、结论 单纯的看硬件配置，这个价格很划算；单纯想体验吧WebOS,Veer也是不错的选择；单纯每天发发短信打打电话，偶尔用游戏打发下时间的人，Veer也挺适合；单纯想装B的人，这个机器拿出去也很拉风，撞机概率小的一塌糊涂，真的撞机了我也强烈建议认识下会买Veer的那个人。但是如果期待Veer像iPhone或者Android给您生活带来便利，不好意思，这个机器是用来折腾您的，不是供您折腾的。 热门日志豆瓣风格的Wordpress主题图书馆自动续借系统数独(soduku)解法——基于Python的实现更换域名到lfyzjck.com各种排序算法(Python实现)]]></description>
			<content:encoded><![CDATA[<p>    由于之前的触屏手机不能摸了，加上手头紧张，千挑万选之下发现了HP Veer这个机器。到现在为止入手HP Veer有小半个月了，经过几番折腾终于达到能用状态了，写篇文章纪念下。不得不说WebOS是个发育不健全的系统，虽然也有很多优秀的地方。</p>
<p><strong>1、外观</strong></p>
<p>    Veer的尺寸很小，只有3.1英寸，大概就是横放在手掌心里，刚好能占据一半的面积。只有你把键盘滑出来，拿在手里才会觉得踏实，否则就有种随时都会从你手中溜走的感觉，以至于现在上厕所都不敢再看手机了。就这点来说，Veer的尺寸和外观的确比较适合女性。右上方有电源/锁屏键，还有一个静音键（我觉得这个有些多余），右侧是usb以及耳机扩展槽的接口。Veer的USB接口设计这里需要单独提下，USB线跟手机连接的一端通过磁石吸附到手机的接口上，很独特而且感觉非常舒服，但仅限感觉。实际用的过程中就会发现，由于没有传统接口那样的插槽，USB连接很不牢固，很容易断开。充电或者跟电脑连接的时候，你不得不小心翼翼的把手机摆好，而且不能乱碰。</p>
<p>    再说说键盘吧，按键本身包括按键的间隔都非常小，得用指甲盖才能准确按到，手大的人要尤其注意了。全键盘输入习惯后还算流畅，问题不大。</p>
<p><strong>2、操作体验</strong></p>
<p>    这个是WebOS最值得一提的地方了。Veer的硬件还算给力，800MHZ的CPU，512MB RAM + 8GB ROM，配合WebOS操作非常流畅，这点上远胜Android（曾经的Hero用户泪奔）。卡片式的界面很有爱而且确实很方便，手势输入很强大。照相机没有闪光灯，不过拍照质量和速度都不错。最给力的还是游戏了，可玩的比较多，而且画面流畅，对比650元的价格，不得不说性价比超值。（低端Android机在这点可能也要被秒杀了）</p>
<p><span id="more-565"></span></p>
<p><strong>3、软件</strong></p>
<p>    通讯录和邮件方面都没有问题，用Google Sync或者Exchange都可以轻松搞定，Calendar同步需要打个补丁，理所当然的没有农历。<br />
    由于第三方软件相对贫乏，追求便利性的话还是绕道玩Android比较好。当然常用的QQ，Renren，新浪微博的客户端还是有的，地图暂时没找到合适且能用的，于是GPS就成了摆设。<br />
    中文输入法虽然差强人意，不过还好有而且能用。HP收购WebOS后大概没考虑过进入中国市场（当然现在更加不可能），WebOS完全没有针对中文用户的优化（我本来想用“几乎”的，但仔细想想确实没有）。界面想改成中文也需要装个蛋疼的补丁，而且会引发其他奇怪的问题。所以我很淡定的到现在也还在看着英文界面。<br />
    浏览器目前还没第三方的，自带的效果好但是耗流量，看看专门手机版的网页还是没什么压力。对我这种打开浏览器只看GR的人来说，更是无所谓的事情。J2ME版的UC虽然能用，但也是一蛋疼的货，嫌麻烦的直接忽略吧。</p>
<p>    最后来吐槽下Veer的重力感应系统，系统没有提供可以关闭重力感应的地方，于是有时候侧躺下看手机的时候，竖着拿手机屏幕会横过来，横着拿手机屏幕会竖过来，然后就有点想骂人的冲动。</p>
<p><strong>4、结论</strong></p>
<p>    单纯的看硬件配置，这个价格很划算；单纯想体验吧WebOS,Veer也是不错的选择；单纯每天发发短信打打电话，偶尔用游戏打发下时间的人，Veer也挺适合；单纯想装B的人，这个机器拿出去也很拉风，撞机概率小的一塌糊涂，真的撞机了我也强烈建议认识下会买Veer的那个人。但是如果期待Veer像iPhone或者Android给您生活带来便利，不好意思，这个机器是用来折腾您的，不是供您折腾的。</p>
<h2  class="related_post_title">热门日志</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2011-05-16/521.html" title="豆瓣风格的Wordpress主题">豆瓣风格的Wordpress主题</a></li><li><a href="http://www.lfyzjck.com/2011-04-18/511.html" title="图书馆自动续借系统">图书馆自动续借系统</a></li><li><a href="http://www.lfyzjck.com/2011-01-12/438.html" title="数独(soduku)解法——基于Python的实现">数独(soduku)解法——基于Python的实现</a></li><li><a href="http://www.lfyzjck.com/2010-03-16/310.html" title="更换域名到lfyzjck.com">更换域名到lfyzjck.com</a></li><li><a href="http://www.lfyzjck.com/2011-05-19/530.html" title="各种排序算法(Python实现)">各种排序算法(Python实现)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2011-11-18/565.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>在Yii Framework中集成Twig</title>
		<link>http://www.lfyzjck.com/2011-09-24/558.html</link>
		<comments>http://www.lfyzjck.com/2011-09-24/558.html#comments</comments>
		<pubDate>Sat, 24 Sep 2011 06:45:35 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[PHPer]]></category>
		<category><![CDATA[Twig]]></category>
		<category><![CDATA[Yii Framework]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=558</guid>
		<description><![CDATA[Twig是一款快速、安全、灵活的PHP模板引擎，它内置了许多filter和tags，并且支持模板继承，能让你用最简洁的代码来描述你的模板。他的语法和Python下的模板引擎Jinjia以及Django的模板语法都非常像。 比如我们在PHP中需要输出变量并且将其进行转义时，语法比较累赘： 1 2 &#60; ?php echo $var ?&#62; &#60; ?php echo htmlspecialchars&#40;\$var, ENT_QUOTES, 'UTF-8'&#41; ?&#62; 但是在Twig中可以这样写: 1 2 3 {{ var }} {{ var&#124;escape }} {{ var&#124;e }} {# shortcut to escape a variable #} 遍历数组： 1 2 3 &#8230; <a href="http://www.lfyzjck.com/2011-09-24/558.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Twig是一款快速、安全、灵活的PHP模板引擎，它内置了许多filter和tags，并且支持模板继承，能让你用最简洁的代码来描述你的模板。他的语法和Python下的模板引擎Jinjia以及Django的模板语法都非常像。<br />
比如我们在PHP中需要输出变量并且将其进行转义时，语法比较累赘：</p>

<div class="wp_codebox"><table><tr id="p55828"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p558code28"><pre class="php" style="font-family:Consolas;"><span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$var</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/htmlspecialchars"><span style="color: #990000;">htmlspecialchars</span></a><span style="color: #009900;">&#40;</span>\<span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>但是在Twig中可以这样写:</p>

<div class="wp_codebox"><table><tr id="p55829"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p558code29"><pre class="html" style="font-family:Consolas;">{{ var }}
{{ var|escape }}
{{ var|e }}         {# shortcut to escape a variable #}</pre></td></tr></table></div>

<p>遍历数组：</p>

<div class="wp_codebox"><table><tr id="p55830"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p558code30"><pre class="html" style="font-family:Consolas;">{% for user in users %}
  * {{ user.name }}
{% else %}
  No user has been found.
{% endfor %}</pre></td></tr></table></div>

<p><span id="more-558"></span><br />
但是要在Yii Framework集成Twig就会遇到点麻烦了，官方网站中已经有能够集成Twig的方案，所以这里我也不再赘述。但是由于Twig中是不支持PHP语法的，所以在有些表达上会遇到困难，比如我们在写Form的视图时，经常会这么写：</p>

<div class="wp_codebox"><table><tr id="p55831"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p558code31"><pre class="php" style="font-family:Consolas;"><span style="color: #339933;">&lt;</span> ?php <span style="color: #000088;">$form</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beginWidget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CActiveForm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #339933;">&lt;</span>span<span style="color: #339933;">&gt;</span>Login<span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">label</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
			<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">textField</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">label</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
			<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">passwordField</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;last&quot;</span><span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>button type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span><span style="color: #339933;">&gt;</span>Login<span style="color: #339933;">&lt;/</span>button<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span> ?php <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">endWidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>但是这样的语法是没法在twig中表达的，所以想去扩展下Twig的功能，让他能够支持我们自定义的widget标签，然后自动解析成我们需要的代码。<br />
总共需要两个类：TokenParser和Node，下面直接上代码：</p>

<div class="wp_codebox"><table><tr id="p55832"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
</pre></td><td class="code" id="p558code32"><pre class="php" style="font-family:Consolas;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">/*
 * This file is an extension of Twig.
 *
 * (c) 2010 lfyzjck
 */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * parser widget tag in Yii framework
 *
 * {% beginwidget 'CActiveForm' as form %}
 *    content of form
 * {% endwidget %}
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Yii_WidgetBlock_TokenParser <span style="color: #000000; font-weight: bold;">extends</span> Twig_TokenParser
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Parses a token and returns a node.
     *
     * @param Twig_Token $token A Twig_Token instance
     *
     * @return Twig_NodeInterface A Twig_NodeInterface instance
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> parse<span style="color: #009900;">&#40;</span>Twig_Token <span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$lineno</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$token</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$stream</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stream</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expect</span><span style="color: #009900;">&#40;</span>Twig_Token<span style="color: #339933;">::</span><span style="color: #004000;">STRING_TYPE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stream</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">test</span><span style="color: #009900;">&#40;</span>Twig_Token<span style="color: #339933;">::</span><span style="color: #004000;">PUNCTUATION_TYPE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getExpressionParser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parseHashExpression</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Twig_Node_Expression_Array<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lineno</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$stream</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expect</span><span style="color: #009900;">&#40;</span>Twig_Token<span style="color: #339933;">::</span><span style="color: #004000;">NAME_TYPE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$assign</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stream</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expect</span><span style="color: #009900;">&#40;</span>Twig_Token<span style="color: #339933;">::</span><span style="color: #004000;">NAME_TYPE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$stream</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expect</span><span style="color: #009900;">&#40;</span>Twig_Token<span style="color: #339933;">::</span><span style="color: #004000;">BLOCK_END_TYPE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">subparse</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'decideBlockEnd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$stream</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">expect</span><span style="color: #009900;">&#40;</span>Twig_Token<span style="color: #339933;">::</span><span style="color: #004000;">BLOCK_END_TYPE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Yii_Node_WidgetBlock<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'assign'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$assign</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$body</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lineno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Gets the tag name associated with this token parser.
     *
     * @param string The tag name
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'beginwidget'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> decideBlockEnd<span style="color: #009900;">&#40;</span>Twig_Token <span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$token</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">test</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'endwidget'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Yii_Node_WidgetBlock <span style="color: #000000; font-weight: bold;">extends</span> Twig_Node
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$attrs</span><span style="color: #339933;">,</span> Twig_NodeInterface <span style="color: #000088;">$body</span><span style="color: #339933;">,</span> Twig_Node_Expression_Array <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lineno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$attrs</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_merge"><span style="color: #990000;">array_merge</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$attrs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$nodes</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'args'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'body'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attrs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lineno</span><span style="color: #339933;">,</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> compile<span style="color: #009900;">&#40;</span>Twig_Compiler <span style="color: #000088;">$compiler</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$compiler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDebugInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$compiler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$context[&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'assign'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;] = $context[&quot;this&quot;]-&gt;beginWidget(&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alias'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;,'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$argNode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'args'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$compiler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">subcompile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argNode</span><span style="color: #009900;">&#41;</span>
				 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">raw</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">');'</span><span style="color: #009900;">&#41;</span>
				 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">raw</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$compiler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">indent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">subcompile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$compiler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">raw</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$context[&quot;this&quot;]-&gt;endWidget();'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>然后在Twig初始化的地方增加我们的语法解析类：</p>

<div class="wp_codebox"><table><tr id="p55833"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p558code33"><pre class="php" style="font-family:Consolas;"><span style="color: #000088;">$twig</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTokenParser</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Yii_WidgetBlock_TokenParser<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>然后我们就可以在twig的模板里这么写了：</p>

<div class="wp_codebox"><table><tr id="p55834"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p558code34"><pre class="html" style="font-family:Consolas;">{% beginwidget 'CActiveForm' as form %}
	&lt;ul&gt;
		&lt;li&gt;
			{{ form.label(model, 'username') }}
			{{ form.textField(model, 'username') }}
		&lt;/li&gt;
		&lt;li&gt;
			{{ form.label(model, 'password') }}
			{{ form.passwordField(model, 'password') }}
		&lt;/li&gt;
	&lt;/ul&gt;
{% endwidget %}</pre></td></tr></table></div>

<h2  class="related_post_title">你可能还对以下日志感兴趣</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2011-04-11/506.html" title="在Yii中实现CKEditor编辑器中的图片上传功能">在Yii中实现CKEditor编辑器中的图片上传功能</a></li><li><a href="http://www.lfyzjck.com/2010-11-27/396.html" title="PHP的文件缓存(File Cache)">PHP的文件缓存(File Cache)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2011-09-24/558.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fedora 15下安装RT5370无线网卡驱动小记</title>
		<link>http://www.lfyzjck.com/2011-09-12/548.html</link>
		<comments>http://www.lfyzjck.com/2011-09-12/548.html#comments</comments>
		<pubDate>Mon, 12 Sep 2011 02:38:14 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[rt5370]]></category>
		<category><![CDATA[w311m]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=548</guid>
		<description><![CDATA[几天前给台式机买了块无线网卡，因为自己是windows和linux双系统，所以希望无线网卡也能支持Linux。最后选定了腾达的W311M，因为官方明确表示这款USB无线网卡支持Linux。 网上大概查了一下，安装何种驱动主要取决于无线网卡所用的芯片型号，比如我这款网卡就是Ralink rt5370，然后从源码编译安装，一般就能正常使用。 不过我把无线网卡插入电脑后就能直接识别了，但是却连不上任何一个AP。查看了一下系统已加载的mod，发现了rt2800usb及rt2800lib两个mod，大概是rt2800的驱动能兼容rt2870系列的芯片吧。 $ lsusb Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. &#160; 驱动下载地址： http://www.tenda.com.cn/uploadfile/downloads/uploadfile/201108/Ralink/5370%20Linux.rar 驱动解压后会发现这个驱动不止适用rt5370，同样可以用于rt3070、rt2870（貌似rt5370,rt3070都是rt2870内核的？不过这点没有证实过）。直接make源码会报错，所以我们需要先修改一些文件： 1、在os/linux/config.mk中找到HAS_WPA_SUPPLICANT以及HAS_NATIVE_WPA_SUPPLICANT_SUPPORT，将他们的值都设为”y”（不含引号） 2、在os/linux/usb_main_dev.c中加入一行：MODULE_LICENSE(“GPL”);否则的话make时会报错缺少授权协议。 3、用su提权至root，在源码根目录下：make &#038;&#038; make install 屏蔽系统自带的驱动了： 编辑 /etc/modprobe.d/blacklist.conf，加入下面两行： blacklist rt2800lib blacklist rt2800usb 完成上面这些工作，就可以加载模块了： modprobe rt5370sta ifconfig ra0 192.168.1.100 up #网卡名称也许不是ra0，请自行通过ifconfig查看 iwconfig &#8230; <a href="http://www.lfyzjck.com/2011-09-12/548.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>几天前给台式机买了块无线网卡，因为自己是windows和linux双系统，所以希望无线网卡也能支持Linux。最后选定了腾达的W311M，因为官方明确表示这款USB无线网卡支持Linux。</p>
<p>网上大概查了一下，安装何种驱动主要取决于无线网卡所用的芯片型号，比如我这款网卡就是Ralink rt5370，然后从源码编译安装，一般就能正常使用。</p>
<p>不过我把无线网卡插入电脑后就能直接识别了，但是却连不上任何一个AP。查看了一下系统已加载的mod，发现了rt2800usb及rt2800lib两个mod，大概是rt2800的驱动能兼容rt2870系列的芯片吧。</p>
<p><code><br />
$ lsusb<br />
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp.<br />
</code></p>
<p>&nbsp;<br />
<b>驱动下载地址：</b> <a href="http://www.tenda.com.cn/uploadfile/downloads/uploadfile/201108/Ralink/5370%20Linux.rar">http://www.tenda.com.cn/uploadfile/downloads/uploadfile/201108/Ralink/5370%20Linux.rar</a></p>
<p>驱动解压后会发现这个驱动不止适用rt5370，同样可以用于rt3070、rt2870（貌似rt5370,rt3070都是rt2870内核的？不过这点没有证实过）。直接make源码会报错，所以我们需要先修改一些文件：</p>
<ol>
<li>1、在os/linux/config.mk中找到HAS_WPA_SUPPLICANT以及HAS_NATIVE_WPA_SUPPLICANT_SUPPORT，将他们的值都设为”y”（不含引号）</li>
<li>2、在os/linux/usb_main_dev.c中加入一行：<br />MODULE_LICENSE(“GPL”);<br />否则的话make时会报错缺少授权协议。</li>
<li>3、用su提权至root，在源码根目录下：make &#038;&#038; make install
<p>屏蔽系统自带的驱动了：</p>
<p>编辑 /etc/modprobe.d/blacklist.conf，加入下面两行：</p>
<p><code><br />
blacklist rt2800lib<br />
blacklist rt2800usb<br />
</code>
</li>
</ol>
<p>完成上面这些工作，就可以加载模块了：<br />
<code><br />
modprobe rt5370sta<br />
ifconfig ra0 192.168.1.100 up                      #网卡名称也许不是ra0，请自行通过ifconfig查看<br />
iwconfig ra0<br />
</code><br />
顺利的话已经可以连接无线开始上网冲浪了。</p>
<p>后记：说实话这个驱动一直让我折腾了好久，主要是一直没发现和系统自带驱动相冲突的问题，不过最后还是可以用了。这款网卡信号算不上强，但还够用，毕竟是Mini系列的，加之价格有便宜的要死，总体来说是一款性价比很高的网卡。Windows下面有软AP，Linux下还没实验过，反正没有太大的需求。</p>
<h2  class="related_post_title">你可能还对以下日志感兴趣</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2010-03-02/305.html" title="Fedora 12下使用root用户登录到系统">Fedora 12下使用root用户登录到系统</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2011-09-12/548.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>各种排序算法(Python实现)</title>
		<link>http://www.lfyzjck.com/2011-05-19/530.html</link>
		<comments>http://www.lfyzjck.com/2011-05-19/530.html#comments</comments>
		<pubDate>Thu, 19 May 2011 13:12:49 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[二路并归排序]]></category>
		<category><![CDATA[快速排序]]></category>
		<category><![CDATA[排序算法]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=530</guid>
		<description><![CDATA[Python实现的快速排序，二路并归排序，冒泡排序，选择排序 <a href="http://www.lfyzjck.com/2011-05-19/530.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>前段时间为准备百度面试恶补的东西，虽然最后还是被刷了，还是把那几天的“战利品”放点上来，算法一直是自己比较薄弱的地方，以后还要更加努力啊。</p>
<p>下面用Python实现了几个常用的排序，如快速排序，选择排序，以及二路并归排序等等。</p>

<div class="wp_codebox"><table><tr id="p53035"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code" id="p530code35"><pre class="python" style="font-family:Consolas;"><span style="color: #808080; font-style: italic;">#encoding=utf-8</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">copy</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">copy</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> directInsertSort<span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot; 直接插入排序 &quot;&quot;&quot;</span>
	size = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,size<span style="color: black;">&#41;</span>:
		tmp, j = seq<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, i
		<span style="color: #ff7700;font-weight:bold;">while</span> j <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> tmp <span style="color: #66cc66;">&lt;</span> seq<span style="color: black;">&#91;</span>j-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:
			seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>, j = seq<span style="color: black;">&#91;</span>j-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, j-<span style="color: #ff4500;">1</span>
		seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span> = tmp
	<span style="color: #ff7700;font-weight:bold;">return</span> seq
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> directSelectSort<span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot; 直接选择排序 &quot;&quot;&quot;</span>
	size = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,size - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
		k = i<span style="color: #66cc66;">;</span>j = i+<span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">while</span> j <span style="color: #66cc66;">&lt;</span> size:
			<span style="color: #ff7700;font-weight:bold;">if</span> seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> seq<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span>:
				k = j
			j += <span style="color: #ff4500;">1</span>
		seq<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>,seq<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span> = seq<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span>,seq<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> seq
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> bubbleSort<span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;冒泡排序&quot;&quot;&quot;</span>
	size = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,size<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,size-i<span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> seq<span style="color: black;">&#91;</span>j+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>:
				seq<span style="color: black;">&#91;</span>j+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>,seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span> = seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>,seq<span style="color: black;">&#91;</span>j+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> seq
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _divide<span style="color: black;">&#40;</span>seq, low, high<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;快速排序划分函数&quot;&quot;&quot;</span>
	tmp = seq<span style="color: black;">&#91;</span>low<span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> low <span style="color: #66cc66;">!</span>= high:
		<span style="color: #ff7700;font-weight:bold;">while</span> low <span style="color: #66cc66;">&lt;</span> high <span style="color: #ff7700;font-weight:bold;">and</span> seq<span style="color: black;">&#91;</span>high<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span>= tmp: high -= <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> low <span style="color: #66cc66;">&lt;</span> high:
			seq<span style="color: black;">&#91;</span>low<span style="color: black;">&#93;</span> = seq<span style="color: black;">&#91;</span>high<span style="color: black;">&#93;</span>
			low += <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">while</span> low <span style="color: #66cc66;">&lt;</span> high <span style="color: #ff7700;font-weight:bold;">and</span> seq<span style="color: black;">&#91;</span>low<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span>= tmp: low += <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> low <span style="color: #66cc66;">&lt;</span> high:
			seq<span style="color: black;">&#91;</span>high<span style="color: black;">&#93;</span> = seq<span style="color: black;">&#91;</span>low<span style="color: black;">&#93;</span>
			high -= <span style="color: #ff4500;">1</span>
	seq<span style="color: black;">&#91;</span>low<span style="color: black;">&#93;</span> = tmp
	<span style="color: #ff7700;font-weight:bold;">return</span> low
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _quickSort<span style="color: black;">&#40;</span>seq, low, high<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;快速排序辅助函数&quot;&quot;&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> low <span style="color: #66cc66;">&gt;</span>= high: <span style="color: #ff7700;font-weight:bold;">return</span>
	mid = _divide<span style="color: black;">&#40;</span>seq, low, high<span style="color: black;">&#41;</span>
	_quickSort<span style="color: black;">&#40;</span>seq, low, mid - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
	_quickSort<span style="color: black;">&#40;</span>seq, mid + <span style="color: #ff4500;">1</span>, high<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> quickSort<span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;快速排序包裹函数&quot;&quot;&quot;</span>
	size = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>
	_quickSort<span style="color: black;">&#40;</span>seq, <span style="color: #ff4500;">0</span>, size - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> seq
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> merge<span style="color: black;">&#40;</span>seq, left, mid, right<span style="color: black;">&#41;</span>:
	tmp = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
	i, j = left, mid
	<span style="color: #ff7700;font-weight:bold;">while</span> i <span style="color: #66cc66;">&lt;</span> mid <span style="color: #ff7700;font-weight:bold;">and</span> j <span style="color: #66cc66;">&lt;</span>= right:
		<span style="color: #ff7700;font-weight:bold;">if</span> seq<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>:
			tmp.<span style="color: black;">append</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
			i += <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			tmp.<span style="color: black;">append</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
			j += <span style="color: #ff4500;">1</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> i <span style="color: #66cc66;">&lt;</span> mid: tmp.<span style="color: black;">extend</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span>i:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> j <span style="color: #66cc66;">&lt;</span>= right: tmp.<span style="color: black;">extend</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span>j:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
	seq<span style="color: black;">&#91;</span>left:right+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> = tmp<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>:right-left+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _mergeSort<span style="color: black;">&#40;</span>seq, left, right<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> left == right: 
		<span style="color: #ff7700;font-weight:bold;">return</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>:
		mid = <span style="color: black;">&#40;</span>left + right<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span>
		_mergeSort<span style="color: black;">&#40;</span>seq, left, mid<span style="color: black;">&#41;</span>
		_mergeSort<span style="color: black;">&#40;</span>seq, mid + <span style="color: #ff4500;">1</span>, right<span style="color: black;">&#41;</span>
		merge<span style="color: black;">&#40;</span>seq, left, mid+<span style="color: #ff4500;">1</span>, right<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#二路并归排序</span>
<span style="color: #ff7700;font-weight:bold;">def</span> mergeSort<span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>:
	size = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>
	_mergeSort<span style="color: black;">&#40;</span>seq, <span style="color: #ff4500;">0</span>, size - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> seq
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
	s = <span style="color: black;">&#91;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> s
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> directSelectSort<span style="color: black;">&#40;</span><span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> directInsertSort<span style="color: black;">&#40;</span><span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> bubbleSort<span style="color: black;">&#40;</span><span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> quickSort<span style="color: black;">&#40;</span><span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> mergeSort<span style="color: black;">&#40;</span><span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>运行结果如下：</p>

<div class="wp_codebox"><table><tr id="p53036"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p530code36"><pre class="python" style="font-family:Consolas;">E:\python_project\practice<span style="color: #66cc66;">&gt;</span>sorting.<span style="color: black;">py</span>
<span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">56</span>, <span style="color: #ff4500;">76</span>, <span style="color: #ff4500;">64</span>, <span style="color: #ff4500;">84</span>, <span style="color: #ff4500;">26</span>, <span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">51</span>, <span style="color: #ff4500;">88</span>, <span style="color: #ff4500;">81</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">95</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">29</span>, <span style="color: #ff4500;">28</span>, <span style="color: #ff4500;">69</span>, <span style="color: #ff4500;">61</span>, <span style="color: #ff4500;">45</span><span style="color: black;">&#93;</span>
&nbsp;
&nbsp;
<span style="color: black;">&#91;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">26</span>, <span style="color: #ff4500;">28</span>, <span style="color: #ff4500;">29</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">45</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">51</span>, <span style="color: #ff4500;">56</span>, <span style="color: #ff4500;">61</span>, <span style="color: #ff4500;">64</span>, <span style="color: #ff4500;">69</span>, <span style="color: #ff4500;">76</span>, <span style="color: #ff4500;">81</span>, <span style="color: #ff4500;">84</span>, <span style="color: #ff4500;">88</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">95</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">26</span>, <span style="color: #ff4500;">28</span>, <span style="color: #ff4500;">29</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">45</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">51</span>, <span style="color: #ff4500;">56</span>, <span style="color: #ff4500;">61</span>, <span style="color: #ff4500;">64</span>, <span style="color: #ff4500;">69</span>, <span style="color: #ff4500;">76</span>, <span style="color: #ff4500;">81</span>, <span style="color: #ff4500;">84</span>, <span style="color: #ff4500;">88</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">95</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">26</span>, <span style="color: #ff4500;">28</span>, <span style="color: #ff4500;">29</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">45</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">51</span>, <span style="color: #ff4500;">56</span>, <span style="color: #ff4500;">61</span>, <span style="color: #ff4500;">64</span>, <span style="color: #ff4500;">69</span>, <span style="color: #ff4500;">76</span>, <span style="color: #ff4500;">81</span>, <span style="color: #ff4500;">84</span>, <span style="color: #ff4500;">88</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">95</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">26</span>, <span style="color: #ff4500;">28</span>, <span style="color: #ff4500;">29</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">45</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">51</span>, <span style="color: #ff4500;">56</span>, <span style="color: #ff4500;">61</span>, <span style="color: #ff4500;">64</span>, <span style="color: #ff4500;">69</span>, <span style="color: #ff4500;">76</span>, <span style="color: #ff4500;">81</span>, <span style="color: #ff4500;">84</span>, <span style="color: #ff4500;">88</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">95</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">26</span>, <span style="color: #ff4500;">28</span>, <span style="color: #ff4500;">29</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">45</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">51</span>, <span style="color: #ff4500;">56</span>, <span style="color: #ff4500;">61</span>, <span style="color: #ff4500;">64</span>, <span style="color: #ff4500;">69</span>, <span style="color: #ff4500;">76</span>, <span style="color: #ff4500;">81</span>, <span style="color: #ff4500;">84</span>, <span style="color: #ff4500;">88</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">95</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<h2  class="related_post_title">你可能还对以下日志感兴趣</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2012-05-10/590.html" title="一个见简单的短网址(ShortURL)服务">一个见简单的短网址(ShortURL)服务</a></li><li><a href="http://www.lfyzjck.com/2012-05-08/584.html" title="Python中的文件遍历">Python中的文件遍历</a></li><li><a href="http://www.lfyzjck.com/2011-04-18/511.html" title="图书馆自动续借系统">图书馆自动续借系统</a></li><li><a href="http://www.lfyzjck.com/2011-01-12/438.html" title="数独(soduku)解法——基于Python的实现">数独(soduku)解法——基于Python的实现</a></li><li><a href="http://www.lfyzjck.com/2010-12-14/412.html" title="Python中对字典(dict)和列表(list)的排序">Python中对字典(dict)和列表(list)的排序</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2011-05-19/530.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>豆瓣风格的WordPress主题</title>
		<link>http://www.lfyzjck.com/2011-05-16/521.html</link>
		<comments>http://www.lfyzjck.com/2011-05-16/521.html#comments</comments>
		<pubDate>Mon, 16 May 2011 15:21:48 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[PHPer]]></category>
		<category><![CDATA[douban]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[Wordpress Theme]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=521</guid>
		<description><![CDATA[丢在电脑已经很久的一份设计稿，今天把它放出来了，就是你现在看到的这个主题。 这个主题是基于Wordpress默认主题twentyten开发的，第一次做很多不足还请见谅。目前版本是v0.1，如果您有什么建议，请不要吝惜您的键盘寿命，在文章内给我留言。 功能： 完整的小工具支持 支持自定义菜单 支持Wordpress插件 更新日志： 2011-5-17： 发布wordpress-douban-style v0.2，修复了部分样式问题，移动搜索条到页面顶部，添加订阅按钮和follow按钮。 2011-11-4： 增加图片lazyload，文章部分字体改为豆瓣的深灰色，使用主题自带的jQuery而不是Wordpress中的jQuery。 下载：douban-wordpress-theme-v0.3.tar.gz 你可能还对以下日志感兴趣趁热打铁，说说Wordpress的自定义菜单(custom menus)将Google reader中订阅的内容展示在WordPress中]]></description>
			<content:encoded><![CDATA[<p>丢在电脑已经很久的一份设计稿，今天把它放出来了，就是你现在看到的这个主题。</p>
<p>这个主题是基于Wordpress默认主题twentyten开发的，第一次做很多不足还请见谅。目前版本是v0.1，如果您有什么建议，请不要吝惜您的键盘寿命，在文章内给我留言。</p>
<p>功能：</p>
<ol>
<li>完整的小工具支持</li>
<li>支持自定义菜单</li>
<li>支持Wordpress插件</li>
</ol>
<p><a href="http://www.lfyzjck.com/wp-content/uploads/2011/05/screenshot.png"><img class="aligncenter size-full wp-image-522" title="screenshot" src="http://www.lfyzjck.com/wp-content/uploads/2011/05/screenshot.png" alt="豆瓣风格的Wordpress主题截图" width="300" height="260" /></a></p>
<blockquote><p>更新日志：</p>
<p>2011-5-17： 发布wordpress-douban-style v0.2，修复了部分样式问题，移动搜索条到页面顶部，添加订阅按钮和follow按钮。<br />
2011-11-4： 增加图片lazyload，文章部分字体改为豆瓣的深灰色，使用主题自带的jQuery而不是Wordpress中的jQuery。
</p></blockquote>
<p>下载：<a title="点击下载" href="http://www.lfyzjck.com/wp-content/download/douban-wordpress-theme-v0.3.tar.gz" target="_blank">douban-wordpress-theme-v0.3.tar.gz</a></p>
<h2  class="related_post_title">你可能还对以下日志感兴趣</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2011-01-30/464.html" title="趁热打铁，说说Wordpress的自定义菜单(custom menus)">趁热打铁，说说Wordpress的自定义菜单(custom menus)</a></li><li><a href="http://www.lfyzjck.com/2009-06-12/72.html" title="将Google reader中订阅的内容展示在WordPress中">将Google reader中订阅的内容展示在WordPress中</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2011-05-16/521.html/feed</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>ShoutBox</title>
		<link>http://www.lfyzjck.com/2011-04-20/516.html</link>
		<comments>http://www.lfyzjck.com/2011-04-20/516.html#comments</comments>
		<pubDate>Wed, 20 Apr 2011 12:31:00 +0000</pubDate>
		<dc:creator>南柯一梦</dc:creator>
				<category><![CDATA[PHPer]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Shoutbox]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://www.lfyzjck.com/?p=516</guid>
		<description><![CDATA[知道这个东西叫Shoutbox也花了很多时间，任何人都可以在这个盒子里很方便的发送消息，消息会被即时的显示在页面上，被所有人看到。这种shoutbox在很多国外论坛上很常见，国内好像不怎么有。去年跟小豪做很多无聊项目的时候，有客户提出要求要实现这么个功能，这大概是唯一感觉还好玩点的东西了吧！可惜最后还是没用用上，哈哈。无聊放上来，怀念一下那段时光。 截图： 原理其实比较简单，就是利用Ajax定时刷新，删掉旧的消息把新的消息弹出来，但是看起来还是蛮酷的东西。 代码就不放了，数据库用的是sqlite，搭好PHP环境直接就可以运行。 下载地址：Shoutbox 你可能还对以下日志感兴趣通过gettext方式实现PHP国际化（i18n）PHP的文件缓存(File Cache)通过Yahoo Finance API获取股票数据Fedora下用配置nginx+fastcgi+php+mysql开发环境Mcrypt模块简介（译）]]></description>
			<content:encoded><![CDATA[<p>知道这个东西叫Shoutbox也花了很多时间，任何人都可以在这个盒子里很方便的发送消息，消息会被即时的显示在页面上，被所有人看到。这种shoutbox在很多国外论坛上很常见，国内好像不怎么有。去年跟小豪做很多无聊项目的时候，有客户提出要求要实现这么个功能，这大概是唯一感觉还好玩点的东西了吧！可惜最后还是没用用上，哈哈。无聊放上来，怀念一下那段时光。</p>
<p>截图：</p>
<div><a href="http://www.lfyzjck.com/wp-content/uploads/2011/04/shoutbox.jpg"><img class="aligncenter size-full wp-image-517" title="shoutbox" src="http://www.lfyzjck.com/wp-content/uploads/2011/04/shoutbox.jpg" alt="shoutbox" width="418" height="351" /></a></div>
<div>原理其实比较简单，就是利用Ajax定时刷新，删掉旧的消息把新的消息弹出来，但是看起来还是蛮酷的东西。</div>
<div>代码就不放了，数据库用的是sqlite，搭好PHP环境直接就可以运行。</div>
<div></div>
<div><strong>下载地址：<a title="点击下载" href="http://www.lfyzjck.com/wp-content/download/shoutbox.rar" target="_blank">Shoutbox</a></strong></div>
<h2  class="related_post_title">你可能还对以下日志感兴趣</h2><ul class="related_post"><li><a href="http://www.lfyzjck.com/2010-12-29/399.html" title="通过gettext方式实现PHP国际化（i18n）">通过gettext方式实现PHP国际化（i18n）</a></li><li><a href="http://www.lfyzjck.com/2010-11-27/396.html" title="PHP的文件缓存(File Cache)">PHP的文件缓存(File Cache)</a></li><li><a href="http://www.lfyzjck.com/2010-11-25/389.html" title="通过Yahoo Finance API获取股票数据">通过Yahoo Finance API获取股票数据</a></li><li><a href="http://www.lfyzjck.com/2010-11-07/370.html" title="Fedora下用配置nginx+fastcgi+php+mysql开发环境">Fedora下用配置nginx+fastcgi+php+mysql开发环境</a></li><li><a href="http://www.lfyzjck.com/2010-03-31/252.html" title="Mcrypt模块简介（译）">Mcrypt模块简介（译）</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lfyzjck.com/2011-04-20/516.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

