<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ross Wan&#039;s World!</title>
	<atom:link href="http://mrwlwan.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mrwlwan.wordpress.com</link>
	<description>Python, Ajax, PHP and Linux.</description>
	<lastBuildDate>Mon, 19 Sep 2011 09:38:25 +0000</lastBuildDate>
	<language>zh-cn</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mrwlwan.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ross Wan&#039;s World!</title>
		<link>http://mrwlwan.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mrwlwan.wordpress.com/osd.xml" title="Ross Wan&#039;s World!" />
	<atom:link rel='hub' href='http://mrwlwan.wordpress.com/?pushpress=hub'/>
		<item>
		<title>The Python Challenge Lv.10</title>
		<link>http://mrwlwan.wordpress.com/2011/09/08/the-python-challenge-lv-10/</link>
		<comments>http://mrwlwan.wordpress.com/2011/09/08/the-python-challenge-lv-10/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 04:23:41 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[re]]></category>
		<category><![CDATA[sub]]></category>
		<category><![CDATA[the python challenge]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=512</guid>
		<description><![CDATA[Lv.10 利用第8关的账号和密码进入第10关, 显示是一张黄牛的图片, bull? 图片下面是一个 Python 语句: len(a[30]) = ? 将鼠标放在牛的身上, 原来是一个链接, 点击打开, 显示一个未完的列表: a = [1, 11, 21, 1211, 111221, 看来这题的题目就是根据那个未完的列表提示, 推测 a[30] 的长度. 再看看那个未完的列表有什么规律: a[0] = &#8217;1&#8242; a[1] = &#8217;11&#8242; 每两个字符拆分理解为: 1个&#8217;1&#8242; 等于 a[0] a[2] = &#8217;21&#8242; 每两个拆分理解为: 1个&#8217;2&#8242; 等于 a[1] a[3] = &#8217;1211&#8242; 每两个拆分理解为: 1个&#8217;2&#8242; + 2个&#8217;1&#8242; 等于 a[2] a[4] = &#8217;111221&#8242; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=512&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><a href="http://www.pythonchallenge.com/pc/return/bull.html" target="_blank">Lv.10</a></h1>
<p><span style="font-size:1.3em;line-height:1.5em;">利用<a href="http://mrwlwan.wordpress.com/2011/09/01/the-python-challenge-lv-8/" target="_blank">第8关</a>的账号和密码进入第10关, 显示是一张黄牛的图片, bull? 图片下面是一个 Python 语句:</span></p>
<blockquote><p>len(a[30]) = ?</p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">将鼠标放在牛的身上, 原来是一个链接, 点击打开, 显示一个未完的列表:</span></p>
<blockquote><p>a = [1, 11, 21, 1211, 111221, </p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">看来这题的题目就是根据那个未完的列表提示, 推测 a[30] 的长度.</span></p>
<p><span style="font-size:1.3em;line-height:1.5em;">再看看那个未完的列表有什么规律:</span></p>
<blockquote><p>a[0] = &#8217;1&#8242;<br />
a[1] = &#8217;11&#8242; 每两个字符拆分理解为: 1个&#8217;1&#8242; 等于 a[0]<br />
a[2] = &#8217;21&#8242; 每两个拆分理解为: 1个&#8217;2&#8242; 等于 a[1]<br />
a[3] = &#8217;1211&#8242; 每两个拆分理解为: 1个&#8217;2&#8242; + 2个&#8217;1&#8242; 等于 a[2]<br />
a[4] = &#8217;111221&#8242; 第两个字符拆分理解为: 1个&#8217;1&#8242; + 1个&#8217;2&#8242; + 2个&#8217;1&#8242; 等于 a[3]<br />
&#8230;
</p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">规律出来了, 剩下的可以交给 Python :)</span></p>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">import re

def repl(match_obj):
    return '%s%s' % (len(match_obj.group()), match_obj.groups()[0])

if __name__ == '__main__':
    a = '1'
    reg = re.compile(r'(\d)\1*')
    for i in range(30):
        a = reg.sub(repl, a)
    print('a[30] = %s\n' % a)
    print('len(a[30]) = %d' % len(a))</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">可知, len(a[30]) 的答案是5808. 下一关的网址就是: <a href="http://www.pythonchallenge.com/pc/return/5808.html" target="_blank">http://www.pythonchallenge.com/pc/return/5808.html</a></span></p>
<p>Have fun~~</p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/re/'>re</a>, <a href='http://mrwlwan.wordpress.com/tag/sub/'>sub</a>, <a href='http://mrwlwan.wordpress.com/tag/the-python-challenge/'>the python challenge</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/512/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=512&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/09/08/the-python-challenge-lv-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>The Python Challenge Lv.9</title>
		<link>http://mrwlwan.wordpress.com/2011/09/07/the-python-challenge-lv-9/</link>
		<comments>http://mrwlwan.wordpress.com/2011/09/07/the-python-challenge-lv-9/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 04:12:02 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[ImageDraw]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[the python challenge]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=508</guid>
		<description><![CDATA[Lv.9 利用第8关得到的账号和密码成功进入第9关, 显示一幅河岸的图片, 图片上有若干黑点, 网页没有其它文字提示. 再看看网页的标题, &#8220;connect the dots&#8221;???难道是将图片上的黑点连接起来? 先别帮着动手, 看看网页的源代码吧, 毕竟以往的经验说明提示经常出现在网页源代码里:) 果然, 网页源代码的注释里出现两组数字&#8212;-first 和 second, 而且提示 &#8220;first+second&#8221;. 再回看标题&#8221;connect the dots&#8221;, 难道是将这些数字代表的坐标点连接起来?? 利用 PIL 的 ImageDraw 进行画图: 打开生成的图片 good_ok.jpg, 明显看到那些点连接起来是一头牛, cow? 于是尝试打开http://www.pythonchallenge.com/pc/return/cow.html,显示: hmm. it&#8217;s a male. 呵呵,原来是头公牛&#8212;-bull, 于是成功得到下一关的网址: http://www.pythonchallenge.com/pc/return/bull.html Have fun~~ Filed under: Python Tagged: ImageDraw, PIL, Python, the python challenge<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=508&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><a href="http://www.pythonchallenge.com/pc/return/good.html" target="_blank">Lv.9</a></h1>
<p><span style="font-size:1.3em;line-height:1.5em;">利用<a href="http://mrwlwan.wordpress.com/2011/09/01/the-python-challenge-lv-8/" target="_blank">第8关</a>得到的账号和密码成功进入第9关, 显示一幅河岸的图片, 图片上有若干黑点, 网页没有其它文字提示. 再看看网页的标题, &#8220;connect the dots&#8221;???难道是将图片上的黑点连接起来?</span></p>
<p><span style="font-size:1.3em;line-height:1.5em;">先别帮着动手, 看看网页的源代码吧, 毕竟以往的经验说明提示经常出现在网页源代码里:) 果然,  网页源代码的注释里出现两组数字&#8212;-first 和 second, 而且提示 &#8220;first+second&#8221;. 再回看标题&#8221;connect the dots&#8221;, 难道是将这些数字代表的坐标点连接起来?? 利用 PIL 的 ImageDraw 进行画图:</span></p>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">from PIL import Image, ImageDraw

first_points = (    146,399,163,403,170,393,169,391,166,386,170,381,170,371,170,355,169,346,167,335,170,329,170,320,170,310,171,301,173,290,178,289,182,287,188,286,190,286,192,291,194,296,195,305,194,307,191,312,190,316,190,321,192,331,193,338,196,341,197,346,199,352,198,360,197,366,197,373,196,380,197,383,196,387,192,389,191,392,190,396,189,400,194,401,201,402,208,403,213,402,216,401,219,397,219,393,216,390,215,385,215,379,213,373,213,365,212,360,210,353,210,347,212,338,213,329,214,319,215,311,215,306,216,296,218,290,221,283,225,282,233,284,238,287,243,290,250,291,255,294,261,293,265,291,271,291,273,289,278,287,279,285,281,280,284,278,284,276,287,277,289,283,291,286,294,291,296,295,299,300,301,304,304,320,305,327,306,332,307,341,306,349,303,354,301,364,301,371,297,375,292,384,291,386,302,393,324,391,333,387,328,375,329,367,329,353,330,341,331,328,336,319,338,310,341,304,341,285,341,278,343,269,344,262,346,259,346,251,349,259,349,264,349,273,349,280,349,288,349,295,349,298,354,293,356,286,354,279,352,268,352,257,351,249,350,234,351,211,352,197,354,185,353,171,351,154,348,147,342,137,339,132,330,122,327,120,314,116,304,117,293,118,284,118,281,122,275,128,265,129,257,131,244,133,239,134,228,136,221,137,214,138,209,135,201,132,192,130,184,131,175,129,170,131,159,134,157,134,160,130,170,125,176,114,176,102,173,103,172,108,171,111,163,115,156,116,149,117,142,116,136,115,129,115,124,115,120,115,115,117,113,120,109,122,102,122,100,121,95,121,89,115,87,110,82,109,84,118,89,123,93,129,100,130,108,132,110,133,110,136,107,138,105,140,95,138,86,141,79,149,77,155,81,162,90,165,97,167,99,171,109,171,107,161,111,156,113,170,115,185,118,208,117,223,121,239,128,251,133,259,136,266,139,276,143,290,148,310,151,332,155,348,156,353,153,366,149,379,147,394,146,399
)
second_points = (    156,141,165,135,169,131,176,130,187,134,191,140,191,146,186,150,179,155,175,157,168,157,163,157,159,157,158,164,159,175,159,181,157,191,154,197,153,205,153,210,152,212,147,215,146,218,143,220,132,220,125,217,119,209,116,196,115,185,114,172,114,167,112,161,109,165,107,170,99,171,97,167,89,164,81,162,77,155,81,148,87,140,96,138,105,141,110,136,111,126,113,129,118,117,128,114,137,115,146,114,155,115,158,121,157,128,156,134,157,136,156,136
)

if __name__ == '__main__':
    img = Image.new('RGBA', (500,500))
    draw = ImageDraw.Draw(img)
    draw.polygon(first_points)
    draw.polygon(second_points)
    img.save('good_ok.jpg')
</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">打开生成的图片 good_ok.jpg, 明显看到那些点连接起来是一头牛, cow? 于是尝试打开<a href="http://www.pythonchallenge.com/pc/return/cow.html" target="_blank">http://www.pythonchallenge.com/pc/return/cow.html</a>,显示:</span></p>
<blockquote><p>hmm. it&#8217;s a male. </p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">呵呵,原来是头公牛&#8212;-bull, 于是成功得到下一关的网址: <a href="http://www.pythonchallenge.com/pc/return/bull.html" target="_blank">http://www.pythonchallenge.com/pc/return/bull.html</a></span></p>
<p><span style="font-size:1.3em;line-height:1.5em;">Have fun~~</span></p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/imagedraw/'>ImageDraw</a>, <a href='http://mrwlwan.wordpress.com/tag/pil/'>PIL</a>, <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/the-python-challenge/'>the python challenge</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/508/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/508/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/508/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/508/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/508/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/508/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/508/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=508&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/09/07/the-python-challenge-lv-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>Firefox：修复 omni.jar</title>
		<link>http://mrwlwan.wordpress.com/2011/09/06/firefox%ef%bc%9a%e4%bf%ae%e5%a4%8d-omni-jar/</link>
		<comments>http://mrwlwan.wordpress.com/2011/09/06/firefox%ef%bc%9a%e4%bf%ae%e5%a4%8d-omni-jar/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 04:25:52 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[info-zip]]></category>
		<category><![CDATA[omni.jar]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=504</guid>
		<description><![CDATA[自从 Firefox 4 开始, Firefox 许多核心文件都放在一个名为 omni.jar 的文件里, 但是, 目前很多流行的解压工具都无法正常浏览或者解压它. 究其原因, 它是一个非标准的 zip 格式文件, 有人曾经就此向 Mozill 提交过 bug, 不过直到目前为止, Firefox 9.0a1 测试版, 问题依旧存在.现提供一个解决方法: 利用 Info-zip 来修复 omni.jar 文件的 header: zip.exe -FF omni.jar &#8211;out omni_fix.jar 运行上面命令, 将会生成一个修复好的omni_fix.jar 文件, 将其重命名为 omni.jar 即可, 这时, 你就可以用你熟悉的解压工具打开它了. 备注: Info-zip 的下载地址: ftp://ftp.info-zip.org/pub/infozip/win32/zip300xn.zip Have fun~~ Filed under: Firefox Tagged: Firefox, info-zip, omni.jar<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=504&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:1.3em;line-height:1.5em;">自从 Firefox 4 开始, Firefox 许多核心文件都放在一个名为 omni.jar 的文件里, 但是, 目前很多流行的解压工具都无法正常浏览或者解压它. 究其原因, 它是一个非标准的 zip 格式文件, 有人曾经就此<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=595473" target="_blank">向 Mozill 提交过 bug</a>, 不过直到目前为止, Firefox 9.0a1 测试版, 问题依旧存在.现提供一个解决方法: 利用 <a href="http://www.info-zip.org/" target="_blank">Info-zip</a> 来修复 omni.jar 文件的 header:</span></p>
<blockquote><p>zip.exe -FF omni.jar &#8211;out omni_fix.jar</p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">运行上面命令, 将会生成一个修复好的omni_fix.jar 文件, 将其重命名为 omni.jar 即可, 这时, 你就可以用你熟悉的解压工具打开它了.</span></p>
<p><strong><span style="font-size:1.3em;line-height:1.5em;">备注: </span></strong><br />
<span style="font-size:1.3em;line-height:1.5em;">Info-zip 的下载地址: </span><a href="ftp://ftp.info-zip.org/pub/infozip/win32/zip300xn.zip" target="_blank">ftp://ftp.info-zip.org/pub/infozip/win32/zip300xn.zip</a></p>
<p>Have fun~~</p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/firefox/'>Firefox</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/firefox/'>Firefox</a>, <a href='http://mrwlwan.wordpress.com/tag/info-zip/'>info-zip</a>, <a href='http://mrwlwan.wordpress.com/tag/omni-jar/'>omni.jar</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/504/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=504&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/09/06/firefox%ef%bc%9a%e4%bf%ae%e5%a4%8d-omni-jar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>Fibonacci(斐波那契)序列的4种求解算法</title>
		<link>http://mrwlwan.wordpress.com/2011/09/02/4%e7%a7%8d-fibonacci%e6%96%90%e6%b3%a2%e9%82%a3%e5%a5%91%e5%ba%8f%e5%88%97%e7%9a%84%e7%ae%97%e6%b3%95/</link>
		<comments>http://mrwlwan.wordpress.com/2011/09/02/4%e7%a7%8d-fibonacci%e6%96%90%e6%b3%a2%e9%82%a3%e5%a5%91%e5%ba%8f%e5%88%97%e7%9a%84%e7%ae%97%e6%b3%95/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 04:50:03 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Fibonacci]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[斐波那契]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=490</guid>
		<description><![CDATA[Fibonacci(斐波那契)序列, 其求解算法, 很多编程书都会以其作为例子. 1. 最普遍的算法是利用递归: 看上去非常美妙,但其时间复杂度是非常惊人的: O(2^n) :( 2. 另一个常见的算法, 就是用循环取代递归算法: 其时间复杂度为: O(n), 相比第1种递归算法, 已经是一种&#8221;飞跃&#8221;了 :&#62; 它也有其变种, 就是利用 Python 的 generator, 其原理是一样, 只是应用的场合不一样罢了 3. 最后一种算法是利用矩阵运算来加速: 需要用到 numpy 库, 其时间复杂度为: O(logn). 4. 通过 Fibonacci 序列的通用公式, 可以利用简单的数学方法求其近似值: 虽然这是4种算法中速度最快的, 但当n越大,其计算精度偏差越大 :&#62; Filed under: Python Tagged: Fibonacci, numpy, Python, 斐波那契<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=490&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:1.3em;line-height:1.5em;"><a href="http://zh.wikipedia.org/zh/斐波那契数列" target="_blank">Fibonacci(斐波那契)序列</a>, 其求解算法, 很多编程书都会以其作为例子.</span></p>
<p><span style="font-size:1.3em;line-height:1.5em;">1. 最普遍的算法是利用递归:</span>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">def fibonacci(n):
    return n&gt;=2 and fibonacci(n-2)+fibonacci(n-1) or n</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">看上去非常美妙,但其时间复杂度是非常惊人的: <strong>O(2^n)</strong></span> :(</p>
<p><span style="font-size:1.3em;line-height:1.5em;">2. 另一个常见的算法, 就是用循环取代递归算法:</span>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">def fibonacci(n):
    a, b = 0, 1
    for i in range(n):
        a, b = b, a+b
    return a</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">其时间复杂度为: <strong>O(n)</strong>, 相比第1种递归算法, 已经是一种&#8221;飞跃&#8221;了 :&gt; 它也有其变种, 就是利用 Python 的 generator, 其原理是一样, 只是应用的场合不一样罢了</span>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">def fibonacci():
    a, b = 0, 1
    while 1:
        yield a
        a, b = b, a+b

f = fibonacci()
n = 100
for i in range(n+1):
    print(next(f))</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">3. 最后一种算法是利用矩阵运算来加速:</span>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">import numpy

fibonacci_matrix = numpy.matrix([[1,1],[1,0]], dtype=numpy.ndarray)
def fibonacci(n):
    return fibonacci_matrix**(n-1)[0, 0]</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">需要用到 <a href="http://numpy.scipy.org" target="_blank">numpy</a> 库, 其时间复杂度为: <strong>O(logn)</strong>.</span></p>
<p><span style="font-size:1.3em;line-height:1.5em;">4. 通过 Fibonacci 序列的通用公式, 可以利用简单的数学方法求其近似值: </span>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">from math import sqrt

def fibonacci(n):
    return int(((1+sqrt(5))/2)**n/sqrt(5))</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">虽然这是4种算法中速度最快的, 但当n越大,其计算精度偏差越大 :&gt;</span></p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/fibonacci/'>Fibonacci</a>, <a href='http://mrwlwan.wordpress.com/tag/numpy/'>numpy</a>, <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/%e6%96%90%e6%b3%a2%e9%82%a3%e5%a5%91/'>斐波那契</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/490/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/490/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/490/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=490&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/09/02/4%e7%a7%8d-fibonacci%e6%96%90%e6%b3%a2%e9%82%a3%e5%a5%91%e5%ba%8f%e5%88%97%e7%9a%84%e7%ae%97%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>The Python Challenge Lv.8</title>
		<link>http://mrwlwan.wordpress.com/2011/09/01/the-python-challenge-lv-8/</link>
		<comments>http://mrwlwan.wordpress.com/2011/09/01/the-python-challenge-lv-8/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 04:12:59 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[bz2]]></category>
		<category><![CDATA[the python challenge]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=485</guid>
		<description><![CDATA[Lv.8 一打开第8关的网页, 显示的是一张蜜蜂辛勤(Working hard)采花图片, 将鼠标放在蜜蜂上, 发现蜜蜂的区域是一个链接: http://www.pythonchallenge.com/pc/return/good.html 打开那链接,发觉是需账号认证才能打开的. 用户名是和密码是什么呢? 将网页退后到第8关的网页, 查看其源代码, 发现了二行特别的注释: 看来, 将上面的注释解密就行了. 留意那两行注释, 都是以&#8221;BZh91AY&#38;SY&#8221;开头的,还有图片是蜜蜂?Bee?猜想是不是用 bz2 加密的呢? 立即试试: 通往下一关: http://www.pythonchallenge.com/pc/return/good.html 的用户名是huge, 密码是file. Have fun:) Filed under: Python Tagged: bz2, Python, the python challenge<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=485&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><a href="http://www.pythonchallenge.com/pc/def/integrity.html" target="_blank">Lv.8</a></h1>
<p><span style="font-size:1.3em;line-height:1.5em;">一打开第8关的网页, 显示的是一张蜜蜂辛勤(Working hard)采花图片, 将鼠标放在蜜蜂上, 发现蜜蜂的区域是一个链接: <a href="http://www.pythonchallenge.com/pc/return/good.html" target="_blank">http://www.pythonchallenge.com/pc/return/good.html</a></span><br />
<span style="font-size:1.3em;line-height:1.5em;">打开那链接,发觉是需账号认证才能打开的. 用户名是和密码是什么呢? 将网页退后到第8关的网页, 查看其源代码, 发现了二行特别的注释:</span></p>
<pre class="brush: plain; wrap-lines: true;">un: 'BZh91AY&amp;SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07&lt;]\xc9\x14\xe1BA\x06\xbe\x084'
pw: 'BZh91AY&amp;SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13&lt;]\xc9\x14\xe1BBP\x91\xf08'</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">看来, 将上面的注释解密就行了. 留意那两行注释, 都是以&#8221;BZh91AY&amp;SY&#8221;开头的,还有图片是蜜蜂?Bee?猜想是不是用 bz2 加密的呢? 立即试试:</span></p>
<pre class="brush: python; wrap-lines: true;">
import bz2

if __name__ == '__main__':
    un = b'BZh91AY&amp;SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07&lt;]\xc9\x14\xe1BA\x06\xbe\x084'
    pw = b'BZh91AY&amp;SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13&lt;]\xc9\x14\xe1BBP\x91\xf08'
    print('Username: %s' % bz2.decompress(un).decode('ascii'))
    print('Password: %s' % bz2.decompress(pw).decode('ascii'))
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;">通往下一关: <a href="http://www.pythonchallenge.com/pc/return/good.html" target="_blank">http://www.pythonchallenge.com/pc/return/good.html</a> 的用户名是huge, 密码是file.</span></p>
<p>Have fun:)</p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/bz2/'>bz2</a>, <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/the-python-challenge/'>the python challenge</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/485/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=485&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/09/01/the-python-challenge-lv-8/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>The Python Challenge Lv.7</title>
		<link>http://mrwlwan.wordpress.com/2011/08/31/the-python-challenge-lv-7/</link>
		<comments>http://mrwlwan.wordpress.com/2011/08/31/the-python-challenge-lv-7/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 04:21:38 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[getpixel]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[the python challenge]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=471</guid>
		<description><![CDATA[Lv.7 打开网页,显示的是一张河岸的图片, 特别的是, 图片中央有一细条&#8221;灰色带&#8221;.将图片另存为&#8221;oxygen.png&#8221;, 用图片编辑器打开, 放大到最大的倍数.可以看到,那色带是一长方形的灰度色带(left: 0px, top: 43px, right: 607px, bottom: 51px), 它又是由大小为7px * 9px的小长方形灰度块(除了第1块是 5px * 9px)组成的. 灰度的值是0~255, 正好跟 Ascii 相对应, 思路出来了, 下面交给 Python 来处理: 注意, 处理图片需要用到 PIL 库, Python 3的 Windows 编译版可以到以下网址下载: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil 运行结果: smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121] [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=471&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><a href="http://www.pythonchallenge.com/pc/def/oxygen.html" target="_blank">Lv.7</a></h1>
<p><span style="font-size:1.3em;line-height:1.5em;"><br />
打开网页,显示的是一张河岸的图片, 特别的是, 图片中央有一细条&#8221;灰色带&#8221;.将图片另存为&#8221;oxygen.png&#8221;, 用图片编辑器打开, 放大到最大的倍数.可以看到,那色带是一长方形的灰度色带(left: 0px, top: 43px, right: 607px, bottom: 51px), 它又是由大小为7px * 9px的小长方形灰度块(除了第1块是 5px * 9px)组成的. 灰度的值是0~255, 正好跟 Ascii 相对应, 思路出来了, 下面交给 Python 来处理:</span></p>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">
from PIL import Image

if __name__ == '__main__':
    img = Image.open('oxygen.png')
    for p in range(4, 607, 7):
        print(chr(int(sum(img.getpixel((p,43))[:3])/3)), end='')
    print()
</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;"><br />
注意, 处理图片需要用到 PIL 库, Python 3的 Windows 编译版可以到以下网址下载: <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil" target="_blank">http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil</a></span><br />
<span style="font-size:1.3em;line-height:1.5em;"><br />
运行结果:</span></p>
<blockquote><p>smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]</p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;"><br />
可知要对 &#8220;[105, 110, 116, 101, 103, 114, 105, 116, 121]&#8220;进行映射就得到答案了;现将上面的代码改写,一次性的得到答案:)</span></p>
<pre style="overflow:auto;word-wrap:break-word;white-space:pre-wrap;">
<pre class="brush: python; wrap-lines: true;">
from PIL import Image
import re

if __name__ == '__main__':
    img = Image.open('oxygen.png')
    map_string = ''.join(map(lambda p: chr(int(sum(img.getpixel((p, 43))[:3])/3)), range(4, 607, 7)))
    #print(map_string)
    print('\nKey Words: ', end='')
    for i in re.finditer(r'\d+', map_string):
        print(chr(int(i.group())), end='')
    else:
        print()
</pre>
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;"><br />
显示结果:</span></p>
<blockquote><p>Key Words: integrity</p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">可知下一关的网址是: <a href="http://www.pythonchallenge.com/pc/def/integrity.html" target="_blank">http://www.pythonchallenge.com/pc/def/integrity.html</a></span></p>
<p><span style="font-size:1.3em;line-height:1.5em;">Have fun :)</span></p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/getpixel/'>getpixel</a>, <a href='http://mrwlwan.wordpress.com/tag/pil/'>PIL</a>, <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/the-python-challenge/'>the python challenge</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/471/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=471&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/08/31/the-python-challenge-lv-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>PIL: 批量生成缩略图</title>
		<link>http://mrwlwan.wordpress.com/2011/08/29/pil-%e6%89%b9%e9%87%8f%e7%94%9f%e6%88%90%e7%bc%a9%e7%95%a5%e5%9b%be/</link>
		<comments>http://mrwlwan.wordpress.com/2011/08/29/pil-%e6%89%b9%e9%87%8f%e7%94%9f%e6%88%90%e7%bc%a9%e7%95%a5%e5%9b%be/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 04:33:47 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[缩略图]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[Python 3]]></category>
		<category><![CDATA[thumbnail]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=467</guid>
		<description><![CDATA[很久之前, 在 Python 2.5 下写了一个基于 PIL 的批量生成缩略图的脚本, 现将其修改移植到 Python 3下, 其中作了不少修改和完善: 脚本依然是基于 PIL 的, 但目前 PIL 的官方还未发布兼容 Python 3 的 Windows 编译版, 幸运的是, 网上可以下载到非官方的编译版本: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil 脚本默认递归搜索当前文件夹及子文件夹的图像文件, 然后将生成的缩略图文件放在当前文件夹的&#8221;缩略图&#8221;文件夹下.当然, 也可修改脚本的参数, 或者在命令行下指定参数: python3 thumbnial_batch.py -r 0.5 -d &#8216;thumbs&#8217; -p &#8216;thumb&#8217; -s 源文件夹 目标文件夹 参数 -r: 指定压缩图片大小的比率;参数 -d: 指定保存缩略图的文件夹名称; 参数 -p: 指定生成的缩略图的文件名添加的前缀; 参数 -s: 只搜索源文件夹,不进行递归搜索子文件夹. Have fun :&#62; Filed [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=467&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:1.3em;line-height:1.5em;">很久之前, 在 Python 2.5 下写了一个基于 PIL 的<a href="http://mrwlwan.wordpress.com/2008/11/24/python-%e7%ae%80%e5%8d%95%e7%9a%84%e5%9b%be%e7%89%87%e6%89%b9%e9%87%8f%e7%bc%a9%e6%94%be%e8%84%9a%e6%9c%ac/" target="_blank">批量生成缩略图的脚本</a>, 现将其修改移植到 Python 3下, 其中作了不少修改和完善:</span></p>
<pre class="brush: python; wrap-lines: true;">
#!bin/pyhton3
# coding=utf8

import os, sys, getopt, functools
from PIL import Image

# 源图片路径
SRC = os.getcwd()
# 目标保存图片的路径
DEST = os.getcwd()
# 缩略图保存的文件夹名称, 最终缩略图的保存路径为 sys.path.join(DEST, THUMB_DIR).
THUMB_DIR = '缩略图'
# 缩放比例
RATE = 0.15
# 缩略图的命名, 在原文件名前加上 THUMB_PREFFIX, 如&quot;thumb_&quot;
THUMB_PREFFIX = ''
# 是否递归搜索子文件夹
IS_RECURSIVE = True

def coroutine(func):
    &quot;&quot;&quot; 用于协程的装饰器 &quot;&quot;&quot;
    @functools.wraps('func')
    def start(*args, **kwargs):
        g = func(*args, **kwargs)
        next(g)
        return g
    return start

@coroutine
def thumb(save_path, rate, thumb_preffix=''):
    img_exts = ('.jpg', '.jepg', '.gif', '.png', '.bmp')
    saved_filenames = set()
    repeatd_times = 0
    while 1:
        img_path = (yield)
        img_ext = os.path.splitext(img_path)[1].lower()
        if not img_ext in img_exts:
            continue
        print(img_path)
        save_filename = '%s%s%s' % (thumb_preffix, os.path.splitext(os.path.split(img_path)[1])[0], img_ext)
        if save_filename in saved_filenames:
            repeatd_times += 1
            save_filename = '%s_%s%s' % (os.path.splitext(save_filename)[0], repeatd_times, img_ext)
        img = Image.open(img_path)
        img.thumbnail((int(img.size[0]*rate), int(img.size[1]*rate)), 1)
        img.save(os.path.join(save_path, save_filename))
        saved_filenames.add(save_filename)
        print('%s %s' % ('Thumb', save_filename))

def process(src, func, is_recursive=True):
    files = []
    dirs = []
    for f in os.listdir(src):
        path = os.path.join(src, f)
        if os.path.isfile(path):
            files.append(path)
        elif is_recursive and os.path.isdir(path) and f.lower() not in ('缩略图', 'thumb', 'thumbs', 'thumbnail', 'thumbnails'):
            dirs.append(path)
    for path in files:
        func.send(path)
    for path in dirs:
        process(path, func, is_recursive)

if __name__ == '__main__':
    src, dest, thumb_dir, rate, thumb_preffix, is_recursive = SRC, DEST, THUMB_DIR, RATE, THUMB_PREFFIX, IS_RECURSIVE
    opts, args = getopt.getopt(sys.argv[1:], 'r:d:p:s')
    print(opts, args)
    for k,v in opts:
        if k == '-r':
            rate = float(v)
        elif k == '-d':
            thumb_dir = v
        elif k == '-p':
            thumb_preffix = v
        elif k == '-s':
            is_recursive = False
    if args:
        args.append(os.getcwd())
        src, dest = args[:2]

    print(src, dest, thumb_dir, rate, thumb_preffix, is_recursive)
    sys.exit()
    save_path = os.path.join(dest, thumb_dir)
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    my_thumb = thumb(save_path, rate, thumb_preffix)
    process(src, my_thumb, is_recursive)
    print('\nDone!')
    input()
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;"> 脚本依然是基于 <a href="http://www.pythonware.com/products/pil/" target="_blank">PIL</a> 的, 但目前 PIL 的官方还未发布兼容 Python 3 的 Windows 编译版, 幸运的是, 网上可以下载到非官方的编译版本:</span></p>
<blockquote><p><a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil" target="_blank">http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil</a></p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">脚本默认递归搜索当前文件夹及子文件夹的图像文件, 然后将生成的缩略图文件放在当前文件夹的&#8221;缩略图&#8221;文件夹下.当然, 也可修改脚本的参数, 或者在命令行下指定参数:</span></p>
<blockquote><p>python3 thumbnial_batch.py -r 0.5 -d &#8216;thumbs&#8217; -p &#8216;thumb&#8217; -s 源文件夹 目标文件夹</p></blockquote>
<p><span style="font-size:1.3em;line-height:1.5em;">参数 -r: 指定压缩图片大小的比率;参数 -d: 指定保存缩略图的文件夹名称; 参数 -p: 指定生成的缩略图的文件名添加的前缀; 参数 -s: 只搜索源文件夹,不进行递归搜索子文件夹.</span><br />
Have fun :&gt;</p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/%e7%bc%a9%e7%95%a5%e5%9b%be/'>缩略图</a>, <a href='http://mrwlwan.wordpress.com/tag/pil/'>PIL</a>, <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/python-3/'>Python 3</a>, <a href='http://mrwlwan.wordpress.com/tag/thumbnail/'>thumbnail</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/467/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=467&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/08/29/pil-%e6%89%b9%e9%87%8f%e7%94%9f%e6%88%90%e7%bc%a9%e7%95%a5%e5%9b%be/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>Syncfiles for Python 3: 文件夹同步</title>
		<link>http://mrwlwan.wordpress.com/2011/08/28/syncfiles-for-python-3-%e6%96%87%e4%bb%b6%e5%a4%b9%e5%90%8c%e6%ad%a5/</link>
		<comments>http://mrwlwan.wordpress.com/2011/08/28/syncfiles-for-python-3-%e6%96%87%e4%bb%b6%e5%a4%b9%e5%90%8c%e6%ad%a5/#comments</comments>
		<pubDate>Sun, 28 Aug 2011 12:02:45 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[dircmp]]></category>
		<category><![CDATA[filecmp]]></category>
		<category><![CDATA[Python 3]]></category>
		<category><![CDATA[Pyton]]></category>
		<category><![CDATA[subdirs]]></category>
		<category><![CDATA[syncfiles]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=448</guid>
		<description><![CDATA[很久之前在 Python 2.5 下写了个同步两个文件夹的小脚本,  挺有用的, 方便了不少繁琐的资料同步操作, 成了每天必运行的小程序. 今天, 将其更新成兼容 Python 3. 其中有不少修改——之前主要是利用 os.listdir 遍历文件夹,利用 filecmp.dircmp 比较文件夹; 现在,依然用 filecmp.dircmp 来进行比较,但移除了 os.listdir, 因为后来发觉 filecmp.dircmp 来身就提供了 subdirs 方法遍历子文件夹, 而且更为便利, 修改后的脚本的行数也了少不少 :P 脚本设置了默认的 src 和 dest 参数(源文件夹和目标文件夹), 也可以在运行脚本时指定: python3 syncfiles_py3k.py your_src your_dest Have fun~~ Filed under: Python Tagged: dircmp, filecmp, Python 3, Pyton, subdirs, syncfiles<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=448&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:1.3em;line-height:1.5em;"> 很久之前在 Python 2.5 下写了个同步两个文件夹的小脚本,  挺有用的, 方便了不少繁琐的资料同步操作, 成了每天必运行的小程序. 今天, 将其更新成兼容 Python 3. 其中有不少修改——之前主要是利用 os.listdir 遍历文件夹,利用 filecmp.dircmp 比较文件夹; 现在,依然用 filecmp.dircmp 来进行比较,但移除了 os.listdir, 因为后来发觉 filecmp.dircmp 来身就提供了 subdirs 方法遍历子文件夹, 而且更为便利, 修改后的脚本的行数也了少不少 :P</span></p>
<pre class="brush: python; wrap-lines: true;">
#!/bin/python
#coding=utf-8

import filecmp, shutil, os, sys

SRC = r'C:/a'
DEST = r'C:/b'

IGNORE = ['Thumbs.db']

def get_cmp_paths(dir_cmp, filenames):
    return ((os.path.join(dir_cmp.left, f), os.path.join(dir_cmp.right, f)) for f in filenames)

def sync(dir_cmp):
    print(dir_cmp.left)
    for f_left, f_right in get_cmp_paths(dir_cmp, dir_cmp.right_only):
        if os.path.isfile(f_right):
            os.remove(f_right)
        else:
            shutil.rmtree(f_right)
        print('删除 %s' % f_right)
    for f_left, f_right in get_cmp_paths(dir_cmp, dir_cmp.left_only+dir_cmp.diff_files):
        if os.path.isfile(f_left):
            shutil.copy2(f_left, f_right)
        else:
            shutil.copytree(f_left, f_right)
        print('复制 %s' % f_left)
    for sub_cmp_dir in dir_cmp.subdirs.values():
        sync(sub_cmp_dir)

def sync_files(src, dest, ignore=IGNORE):
    if os.path.isfile(src) or os.path.isfile(dest):
        print('只能对文件夹进行同步, 请正确输入源文件夹和目标文件夹...')
        return
    dir_cmp = filecmp.dircmp(src, dest, ignore=IGNORE)
    sync(dir_cmp)
    print('同步完成!')

if __name__ == '__main__':
    src, dest = SRC, DEST
    if len(sys.argv) == 3:
        src, dest = sys.argv[1:3]
    sync_files(src, dest)
    input()
</pre>
<p><span style="font-size:1.3em;line-height:1.5em;"><br />
脚本设置了默认的 src 和 dest 参数(源文件夹和目标文件夹), 也可以在运行脚本时指定:</span></p>
<blockquote><p>python3 syncfiles_py3k.py your_src your_dest</p></blockquote>
<p><span style="font-size:1.3em;line-height:1.2em;">Have fun~~</p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/dircmp/'>dircmp</a>, <a href='http://mrwlwan.wordpress.com/tag/filecmp/'>filecmp</a>, <a href='http://mrwlwan.wordpress.com/tag/python-3/'>Python 3</a>, <a href='http://mrwlwan.wordpress.com/tag/pyton/'>Pyton</a>, <a href='http://mrwlwan.wordpress.com/tag/subdirs/'>subdirs</a>, <a href='http://mrwlwan.wordpress.com/tag/syncfiles/'>syncfiles</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/448/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/448/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/448/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/448/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/448/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/448/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/448/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/448/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/448/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/448/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/448/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/448/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/448/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/448/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=448&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/08/28/syncfiles-for-python-3-%e6%96%87%e4%bb%b6%e5%a4%b9%e5%90%8c%e6%ad%a5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>第三方 Python 库的非官方 Windows 编译版</title>
		<link>http://mrwlwan.wordpress.com/2011/08/27/%e7%ac%ac%e4%b8%89%e6%96%b9-python-extension-packages-%e7%9a%84-%e9%9d%9e%e5%ae%98%e6%96%b9-windows-%e7%89%88/</link>
		<comments>http://mrwlwan.wordpress.com/2011/08/27/%e7%ac%ac%e4%b8%89%e6%96%b9-python-extension-packages-%e7%9a%84-%e9%9d%9e%e5%ae%98%e6%96%b9-windows-%e7%89%88/#comments</comments>
		<pubDate>Sat, 27 Aug 2011 11:50:22 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[Pyhton 3]]></category>
		<category><![CDATA[Unofficial Windows Binaries for Python Extension Packages]]></category>
		<category><![CDATA[Windows 编译]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=438</guid>
		<description><![CDATA[在 Python 3k 正式发布不久, 本人就开始过渡到 Python 3k 了. 对于 Python 3 不向后兼容,现在已经适应了, 唯一的问题是, 到目前为止, 还有不少第三方模块未兼容到 Python 3, 象 PIL 这个被誉为 Python 的&#8221;标准&#8221;图像处理库, 虽然有计划向 Python 3 兼容,但等了又等, 到现在还未完全落实&#8212;- 查看其 repository,发现它的最新开发版已经支持 Python 3了,只是有部分功能未完善.可惜,目前官方未发布其开发版本的 windows 编译版,幸运的是, 有一个 &#8220;Unofficial Windows Binaries for Python Extension&#8220;——虽然是非官方的, 但网上有很多人推荐, 里面有很多非常难找的第三方 Python 库的 Windows 编译版,当然包括了 PIL 1.1.7 For Windows Have fun~~ Filed under: Python [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=438&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:1.3em;line-height:1.5em;">在 Python 3k 正式发布不久, 本人就开始过渡到 Python 3k 了. 对于 Python 3 不向后兼容,现在已经适应了, 唯一的问题是, 到目前为止, 还有不少第三方模块未兼容到 Python 3, 象 PIL 这个被誉为 Python 的&#8221;标准&#8221;图像处理库, 虽然有计划向 Python 3 兼容,但等了又等, 到现在还未完全落实&#8212;- 查看其 <a href="http://hg.effbot.org/pil-2009-raclette" target="_blank">repository</a>,发现它的最新开发版已经支持 Python 3了,只是有部分功能未完善.可惜,目前官方未发布其开发版本的 windows 编译版,幸运的是, 有一个 &#8220;<a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/" target="_blank">Unofficial Windows Binaries for Python Extension</a>&#8220;——虽然是非官方的, 但网上有很多人推荐, 里面有很多非常难找的第三方 Python 库的 Windows 编译版,当然包括了 <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil" target="_blank">PIL 1.1.7 For Windows</a></span></p>
<p>Have fun~~</p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/pil/'>PIL</a>, <a href='http://mrwlwan.wordpress.com/tag/pyhton-3/'>Pyhton 3</a>, <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/unofficial-windows-binaries-for-python-extension-packages/'>Unofficial Windows Binaries for Python Extension Packages</a>, <a href='http://mrwlwan.wordpress.com/tag/windows-%e7%bc%96%e8%af%91/'>Windows 编译</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/438/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=438&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/08/27/%e7%ac%ac%e4%b8%89%e6%96%b9-python-extension-packages-%e7%9a%84-%e9%9d%9e%e5%ae%98%e6%96%b9-windows-%e7%89%88/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
		<item>
		<title>批量修改 Firefox 扩展的最大版本号</title>
		<link>http://mrwlwan.wordpress.com/2011/08/26/%e6%89%b9%e9%87%8f%e4%bf%ae%e6%94%b9-firefox-%e6%89%a9%e5%b1%95%e7%9a%84%e6%9c%80%e5%a4%a7%e7%89%88%e6%9c%ac%e5%8f%b7/</link>
		<comments>http://mrwlwan.wordpress.com/2011/08/26/%e6%89%b9%e9%87%8f%e4%bf%ae%e6%94%b9-firefox-%e6%89%a9%e5%b1%95%e7%9a%84%e6%9c%80%e5%a4%a7%e7%89%88%e6%9c%ac%e5%8f%b7/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 04:19:06 +0000</pubDate>
		<dc:creator>Ross Wan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[install.rdf]]></category>
		<category><![CDATA[xpi]]></category>
		<category><![CDATA[zipfile]]></category>
		<category><![CDATA[扩展]]></category>
		<category><![CDATA[最大版本号]]></category>

		<guid isPermaLink="false">http://mrwlwan.wordpress.com/?p=433</guid>
		<description><![CDATA[本人一直都是 Firefox Nightbuild 版本的&#8221;忠实&#8221;使用者,因为可以提前使用最新的功能,性能也有很大的提升,还可以帮 Mozilla 测试新版本,提交 Bugs 报告.尤其现在的 Firefox 9, 在内存方面有很的改进,值得期待. 回归正题, Firefox Nightbuild 版有它的好处, 问题是很多扩展未能兼容, 但有一部分扩展是兼容的,只是它支持的最大版本号未修改过来,这样,就需要人工手动修改其扩展(xpi文件)的install.rdf. (当然,除了修改最大版本号,还有很多方法可以让其兼容,网上都有介绍) 下面的 Python 脚本可以达到批量修改xpi文件的目的: 将脚本放于 Firefox 配置文件夹的 extensions 目录下运行即可.也可以带1个参数运行,用于指定目标最大版本号,如: python3 xpi_rdf_batch.py 200 值得注意的是,目前它仅可以修改 xpi 文件, 对于 unpack 的扩展不能修改. Have fun :) Filed under: Python Tagged: Firefox, install.rdf, Python, xpi, zipfile, 扩展, 最大版本号<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=433&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>本人一直都是 Firefox Nightbuild 版本的&#8221;忠实&#8221;使用者,因为可以提前使用最新的功能,性能也有很大的提升,还可以帮 Mozilla 测试新版本,提交 Bugs 报告.尤其现在的 Firefox 9, 在内存方面有很的改进,值得期待.</p>
<p>回归正题, Firefox Nightbuild 版有它的好处, 问题是很多扩展未能兼容, 但有一部分扩展是兼容的,只是它支持的最大版本号未修改过来,这样,就需要人工手动修改其扩展(xpi文件)的install.rdf. (当然,除了修改最大版本号,还有很多方法可以让其兼容,网上都有介绍)</p>
<p>下面的 Python 脚本可以达到批量修改xpi文件的目的:</p>
<pre class="brush: python; wrap-lines: true;">
#!bin/python3
# coding=utf8

import re, zipfile, os, sys

maxversion = 100
ff_maxversion_reg = re.compile(br'(ec8030f7-c20a-464f-9b0e-13a3a9e97384.*?em:maxVersion.*?)([^&gt;&lt; =&quot;/]+)', re.S+re.I)

if __name__ == '__main__':
    if len(sys.argv) &gt; 1:
        maxversion = int(sys.argv[1])
    print('Target Maxversion: %s\n' % maxversion)
    sys.exit()

    for filename in os.listdir():
        print('%s ' % filename, end='')
        if os.path.isdir(filename) or not filename.lower().endswith('.xpi'):
            print('skip.')
            continue
        zin = zipfile.ZipFile(filename)
        rdf = zin.read('install.rdf')
        version = 0
        for item in ff_maxversion_reg.finditer(rdf):
            match_obj = re.search(br'\d+', item.groups()[1])
            if match_obj and int(match_obj.group()) &gt; version:
                version = int(match_obj.group())
        if version &gt;= maxversion:
            zin.close()
            print('skip.')
            continue

        zout = zipfile.ZipFile('new.xpi','w')
        rdf = ff_maxversion_reg.sub(br'\g&lt;1&gt;'+ str(maxversion).encode('utf8'), rdf)
        zout.writestr('install.rdf', rdf)
        for item in zin.infolist():
            if item.filename.lower() == 'install.rdf':
                continue
            else:
                buffer = zin.read(item.filename)
                zout.writestr(item, buffer)
        zin.close()
        zout.close()
        os.remove(filename)
        os.rename('new.xpi', filename)
        print('done!')
</pre>
<p>将脚本放于 Firefox 配置文件夹的 extensions 目录下运行即可.也可以带1个参数运行,用于指定目标最大版本号,如:</pre>
<blockquote><p>python3 xpi_rdf_batch.py 200</p></blockquote>
<p>值得注意的是,目前它仅可以修改 xpi 文件, 对于 unpack 的扩展不能修改.</p>
<p>Have fun :)</p>
<br />Filed under: <a href='http://mrwlwan.wordpress.com/category/python/'>Python</a> Tagged: <a href='http://mrwlwan.wordpress.com/tag/firefox/'>Firefox</a>, <a href='http://mrwlwan.wordpress.com/tag/install-rdf/'>install.rdf</a>, <a href='http://mrwlwan.wordpress.com/tag/python/'>Python</a>, <a href='http://mrwlwan.wordpress.com/tag/xpi/'>xpi</a>, <a href='http://mrwlwan.wordpress.com/tag/zipfile/'>zipfile</a>, <a href='http://mrwlwan.wordpress.com/tag/%e6%89%a9%e5%b1%95/'>扩展</a>, <a href='http://mrwlwan.wordpress.com/tag/%e6%9c%80%e5%a4%a7%e7%89%88%e6%9c%ac%e5%8f%b7/'>最大版本号</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mrwlwan.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mrwlwan.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mrwlwan.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mrwlwan.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mrwlwan.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mrwlwan.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mrwlwan.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mrwlwan.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mrwlwan.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mrwlwan.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mrwlwan.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mrwlwan.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mrwlwan.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mrwlwan.wordpress.com/433/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mrwlwan.wordpress.com&amp;blog=1448775&amp;post=433&amp;subd=mrwlwan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mrwlwan.wordpress.com/2011/08/26/%e6%89%b9%e9%87%8f%e4%bf%ae%e6%94%b9-firefox-%e6%89%a9%e5%b1%95%e7%9a%84%e6%9c%80%e5%a4%a7%e7%89%88%e6%9c%ac%e5%8f%b7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dd10defb7c99b243726f7b892be03ffc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ross Wan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
