<?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>Computer, Electron and Technology &#187; python</title>
	<atom:link href="http://www.donevii.com/tags/python/feed" rel="self" type="application/rss+xml" />
	<link>http://www.donevii.com</link>
	<description>DoneVII CET &#38; CPPLITE</description>
	<lastBuildDate>Wed, 02 Jun 2010 10:45:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>snow leopard 下通过 py 脚本下载 google music</title>
		<link>http://www.donevii.com/post/922.html</link>
		<comments>http://www.donevii.com/post/922.html#comments</comments>
		<pubDate>Thu, 17 Sep 2009 02:49:26 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=922</guid>
		<description><![CDATA[最近在国内没办法再用 spotify 听歌了，迫于无奈只好转向 google music 。 因为 firefox 尴尬的内存占用，并且考虑把歌同步到 iPhone 上来听，开始着手找下载 google music 的小工具，发现骨头做的 gmbox ... ]]></description>
			<content:encoded><![CDATA[<p>最近在国内没办法再用 spotify 听歌了，迫于无奈只好转向 google music 。</p>
<p>因为 firefox 尴尬的内存占用，并且考虑把歌同步到 iPhone 上来听，开始着手找下载 google music 的小工具，发现骨头做的 <a href="http://li2z.cn/2009/09/13/gmbox-0-2/">gmbox</a> 不错，可惜现在是只支持 <a href="http://www.donevii.com/post/tag/windows" class="st_tag internal_tag" rel="tag" title="Posts tagged with windows">windows</a> 和 <a href="http://www.donevii.com/post/tag/linux" class="st_tag internal_tag" rel="tag" title="Posts tagged with linux">linux</a> ，只好用 <a href="http://forum.ubuntu.com.cn/viewtopic.php?f=73&#038;p=1205852">gmusic.py</a> 这个脚本。</p>
<p>运行 gmusic.py 首先要安装 python3 环境，去 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">python</a> 官方网站载下 python3.1 然后 configure &#8211;enable-shared &#038;&#038; make &#038;&#038; sudo make install &#038;&#038; ln -s /usr/local/bin/python3 /usr/bin/python3 既可</p>
<p>用 gmusic.py 下载速度在我的 istat 里显示大概是 800K 左右，非常不错。</p>

	<h4>相关文章</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.donevii.com/post/75.html" title="挣脱Windows的枷锁：Linux屏幕录像 (2006-10-10)">挣脱Windows的枷锁：Linux屏幕录像</a> (0)</li>
	<li><a href="http://www.donevii.com/post/78.html" title="[转]C/C++/Perl/汇编/Java效率比较 (2006-10-11)">[转]C/C++/Perl/汇编/Java效率比较</a> (0)</li>
	<li><a href="http://www.donevii.com/post/330.html" title="[超长篇] Inject Your Code to a Portable Executable File (2007-05-24)">[超长篇] Inject Your Code to a Portable Executable File</a> (0)</li>
	<li><a href="http://www.donevii.com/post/204.html" title="PHP &#038; memcached (2006-11-15)">PHP &#038; memcached</a> (0)</li>
	<li><a href="http://www.donevii.com/post/124.html" title="C++资源之不完全导引 (2006-10-26)">C++资源之不完全导引</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/922.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python编程速度技巧</title>
		<link>http://www.donevii.com/post/58.html</link>
		<comments>http://www.donevii.com/post/58.html#comments</comments>
		<pubDate>Wed, 30 Aug 2006 13:46:42 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[类]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=58</guid>
		<description><![CDATA[1.1. 最常见 一个最常见的速度陷坑(至少是俺在没看到网上这篇介绍时陷进去 过好些次的) 是: 许多短字串并成长字串时, 大家通常会用: Toggle line numbers    1 shortStrs = [ str0, str1, ..., strN]    2 #N+1... ]]></description>
			<content:encoded><![CDATA[<p>1.1. 最常见<br />
一个最常见的速度陷坑(至少是俺在没看到网上这篇介绍时陷进去<br />
过好些次的) 是: 许多短字串并成长字串时, 大家通常会用:</p>
<p>Toggle line numbers<br />
   1 shortStrs = [ str0, str1, ..., strN]<br />
   2 #N+1个字串所组成的数列<br />
   3 longStr = &#8221;<br />
   4 for s in shortStrs: longStr += s<br />
因为<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>里字串是不可变的, 所以每次 longStr += s 都是将原 来的 longStr 与 str 拷贝成一个新字串, 再赋给longStr. 随着 longStr的不断增长, 所要拷贝的内容越来越长. 最后导至str0被 拷贝N+1次, str1是N次, &#8230; .</p>
<p>那咋办呢 ? 咱们来看看Skip Montanaro先生的解说: <a href="http://musi-cal.mojam.com/~skip/python/fastpython.html">http://musi-cal.mojam.com/~skip/python/fastpython.html</a> 及可参考一下Guido van Rossum本人的: <a href="http://www.python.org/doc/essays/list2str.html">http://www.python.org/doc/essays/list2str.html</a></p>
<p>1.1.1. 找出速度瓶颈<br />
1)首先在大家应先学会怎么去找出速度瓶颈: <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>自带有profile<br />
模块:</p>
<p>Toggle line numbers<br />
   1 import profile<br />
   2 profile.run (&#8216;想要检查的函数名()&#8217;)<br />
就会打印出那个函数里调用了几次其它函数, 各用了多少时间, 总共用了多少时间等信息 &#8212; Nice ? 详请参阅&lt;&lt;库参考&gt;&gt;中的 profile模块的论述.</p>
<p>当然脑袋笨一点或是聪明一点的, 也可以用time模块中的time() 来显示系统时间, 减去上次的time()就是与它的间隔秒数了.</p>
<p>1.1.2. 字串相并<br />
就头上的例子而言, 用 :</p>
<p>Toggle line numbers<br />
   1 longStr =&#8221;.join(shortStrs)<br />
立马搞定,  但如果shortStrs里面不都是字串, 而包含了些数 字呢 ? 直接用join就会出错. 不怕, 这样来:</p>
<p>Toggle line numbers<br />
   1 shortStrs = [str(s) for s in shortStrs[i]]<br />
   2 longStr = &#8221;.join(shortStrs)<br />
也即先将数列中所有内容都转化为字串, 再用join.</p>
<p>对少数几个字串相并, 应避免用: all = str0 + str1 + str2 + str3 而用: all = &#8216;%s%s%s%s&#8217; % (str0, str1, str2, str3)</p>
<p>1.1.3. 数列排序<br />
list.sort ()<br />
你可以按特定的函数来: list.sort( 函数 ), 只要这个函数接受 两参数, 并按特定规则返回1, 0, -1就可以. &#8212; 很方便吧? 但 会大大减慢运行速度. 下面的方法, 俺举例子来说明可能更容易 明白.</p>
<p>比方说你的数列是 l = ['az', 'by'], 你想以第二个字母来排序. 先取出你的关键词, 并与每个字串组成一个元组: new = map (lambda s: (s[1], s), l )</p>
<p>于是new变成[('z', 'az'), ('y', 'by')], 再把new排一下序: new.sort()</p>
<p>则new就变成 [('y', 'by'), ('z', 'az')], 再返回每个元组中 的第二个字串: sorted = map (lambda t: t[1], new)</p>
<p>于是sorted 就是: ['by', 'az']了. 这里的lambda与map用得很 好.</p>
<p>Python2.4以后, sort和sorted的使用可以参考这片 Wiki: HowToSort</p>
<p>1.1.4. 循环<br />
比如for循环. 当循环体很简单时, 则循环的调用前头(overhead) 会显得很臃肿, 此时map又可以帮忙了. 比如你想把一个长数列 l=['a', 'b', ...]中的每个字串变成大写, 可能会用:</p>
<p>Toggle line numbers<br />
   1 import string<br />
   2 newL = []<br />
   3 for s in l: newL.append( string.upper(s) )<br />
用map就可以省去for循环的前头:</p>
<p>Toggle line numbers<br />
   1 import string<br />
   2 newL = map (string.upper, l)<br />
Guido的文章讲得很详细.</p>
<p>1.1.5. 局域变量 及 &#8216;.&#8217;<br />
象上面, 若用 append = newL.append, 及换种import方法:</p>
<p>Toggle line numbers<br />
   1 import string<br />
   2 append = newL.append<br />
   3 for s in l: append (string.upper(s))<br />
会比在for中运行newL.append快一些, 为啥? 局域变量容易寻找.</p>
<p>俺自己就不比较时间了, Skip Montanaro的结果是:</p>
<p>基本循环: 3.47秒<br />
去点用局域变量: 1.79秒<br />
使用map: 0.54秒</p>
<p>1.1.6. try的使用<br />
比如你想计算一个字串数列: l = ['I', 'You', '<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>', 'Perl', ...] 中每个词出现的次数, 你可能会:</p>
<p>Toggle line numbers<br />
   1 count = {}<br />
   2 for s in l:<br />
   3     if not count.has_key(s): count[s] = 0<br />
   4     else: count[s] += 1<br />
由于每次都得在count中寻找是否已有同名关键词, 会很费时间. 而用try:</p>
<p>Toggle line numbers<br />
   1 count ={}<br />
   2 for s in l:<br />
   3     try: count[s] += 1<br />
   4     except KeyError: count[s] = 0<br />
就好得多. 当然若经常出现例外时, 就不要用try了.</p>
<p>1.1.7. import语句<br />
这好理解. 就是避免在函数定义中来import一个模块, 应全在 全局块中来import</p>
<p>1.1.8. 大量数据处理<br />
由于<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>中的函数调用前头(overhead)比较重, 所以处理大量 数据时, 应:</p>
<p>Toggle line numbers<br />
   1 def f():<br />
   2 for d in hugeData: &#8230;<br />
   3 f()<br />
而不要:</p>
<p>Toggle line numbers<br />
   1 def f(d): &#8230;<br />
   2 for d in hugeData: f(d)<br />
这点好象对其它语言也适用, 差不多是放之四海而皆准, 不过对 解释性语言就更重要了.</p>
<p>1.1.9. 减少周期性检查<br />
这是<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>的本征功能: 周期性检查有没有其它绪(thread)或系 统信号(signal)等要处理.</p>
<p>可以用sys模块中的setcheckinterval 来设置每次检查的时间间隔.</p>
<p>缺省是10, 即每10个虚拟指令 (virtual instruction)检查一次.</p>
<p>当你不用绪并且也懒得搭理 系统信号时, 将检查周期设长会增加速度, 有时还会很显著.</p>
<p>&#8212;编/译完毕. 看来<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>是易学难精了, 象围棋?</p>
<p>2. 我们自个儿的体悟<br />
请有心得者分享!</p>
<p>在“大量数据处理”小节里，是不是说，不要再循环体内部调用函数，应该把函数放到外面？从Python2.2开始,&#8221;找出速度瓶颈&#8221;,已经可以使用hotshot模块了.据说对程序运行效率的影响要比profile小. &#8212; jacobfan<br />
&#8220;由于<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>中的函数调用前头(overhead)比较重, 所以处理大量 数据时, 应: &#8221; 这句译文中,overhead翻译成&#8221;前头&#8221;好象不妥.翻译成&#8221;由于<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>中函数调用的开销比较大,&#8230;&#8221;要好些 &#8212; jacobfan<br />
数组排序中讲的方法真的会快点吗? 真的快到我们值得放弃直接用sort得到得可读性吗?值得怀疑 &#8212; hoxide<br />
Python2.4以后 sort和sorted的使用更加灵活，link已经加到文中，我没有比较过效率。-yichun<br />
关于 “try的使用”：<br />
其实setdefault方法就是为这个目的设的：</p>
<p>Toggle line numbers<br />
   1 count = {}<br />
   2 for s in l:<br />
   3     count.setdefault(s, 0) += 1<br />
这个其实能做更多。通常遇到的问题是要把类似的东西group起来，所以你可能想用：</p>
<p>Toggle line numbers<br />
   1 count = {}<br />
   2 for s in l:<br />
   3     count.setdefault(s, []).append(s)<br />
但是这样你只能把同样的东西hash起来，而不是一类东西。比如说你有一个dict构成的list叫sequence，需要按这些dict的某个key value分类，你还要对分类后的每个类别里面的这些dict各作一定的操作，你就需要用到Raymond实现的这个groupby，你就可以写:</p>
<p>totals = dict((key, group)<br />
                  for key, group in groupby(sequence, lambda x: x.get(&#8216;Age&#8217;)))</p>

	<h4>相关文章</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.donevii.com/post/24.html" title="使用Boost.Python构建混合系统 (2006-08-20)">使用Boost.Python构建混合系统</a> (0)</li>
	<li><a href="http://www.donevii.com/post/78.html" title="[转]C/C++/Perl/汇编/Java效率比较 (2006-10-11)">[转]C/C++/Perl/汇编/Java效率比较</a> (0)</li>
	<li><a href="http://www.donevii.com/post/36.html" title="Python编程技巧 &#8211; 使用状态机 (2006-08-23)">Python编程技巧 &#8211; 使用状态机</a> (0)</li>
	<li><a href="http://www.donevii.com/post/89.html" title="用rsync对网站进行镜像备份 (2006-10-18)">用rsync对网站进行镜像备份</a> (0)</li>
	<li><a href="http://www.donevii.com/post/64.html" title="模型-视图-控制器 (2006-09-01)">模型-视图-控制器</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/58.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python编程技巧 &#8211; 使用状态机</title>
		<link>http://www.donevii.com/post/36.html</link>
		<comments>http://www.donevii.com/post/36.html#comments</comments>
		<pubDate>Wed, 23 Aug 2006 03:16:08 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[开发]]></category>
		<category><![CDATA[测试]]></category>
		<category><![CDATA[类]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=36</guid>
		<description><![CDATA[状态机从理论上说是几乎与计算机和编程相关的每件事的基础。从实用角度来看，状态机还有助于解决许多常见问题（特别适用于 Python 程序员）。本文中，David Mertz 讨论了何时以及如何使用 Py... ]]></description>
			<content:encoded><![CDATA[<p>状态机从理论上说是几乎与计算机和编程相关的每件事的基础。从实用角度来看，状态机还有助于解决许多常见问题（特别适用于 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 程序员）。本文中，David Mertz 讨论了何时以及如何使用 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 编码状态机的实际例子。</p>
<p>    <strong>什么是 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a>？</strong></p>
<p>    <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 是由 Guido van Rossum 开发的免费高级解释型语言。其语法简单易懂，而其面向对象的语义功能强大（但又灵活）。<a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 可以广泛使用并具有高度的可移植性。</p>
<p><strong>    什么是状态机？</strong></p>
<p>    关于状态机的一个极度确切的描述是它是一个有向图形，由一组节点和一组相应的转移函数组成。状态机通过响应一系列事件而“运行”。每个事件都在属于“当前”节点的转移函数的控制范围内，其中函数的范围是节点的一个子集。函数返回“下一个”（也许是同一个）节点。这些节点中至少有一个必须是终态。当到达终态，状态机停止。</p>
<p>    但一个抽象的数学描述（就像我刚给出的）并不能真正说明在什么情况下使用状态机可以解决实际编程问题。另一种策略就是将状态机定义成一种强制性编程语言，其中节点也是源码行。从实用角度看，这个定义尽管精确，但它和第一种描述一样，都是纸上谈兵、毫不实用。（对于说明型、函数型或基于约束的语言，例如，Haskell、Scheme 或 Prolog，不一定会发生这种情况。）</p>
<p>    让我们尝试使用更适合身边实际任务的例子来进行讨论。逻辑上，每个规则表达式都等价于一个状态机，而每个规则表达式的语法分析器都实现这个状态机。实际上，大多数程序员编写状态机时，并没有真正考虑到这一点。</p>
<p>    在以下这个例子中，我们将研究状态机的真正探索性定义。通常，我们有一些不同的方法来响应一组有限数量的事件。某些情况下，响应只取决于事件本身。但在其它情况下，适当的操作取决于以前的事件。</p>
<p>    本文中讨论的状态机是高级机器，其目的是演示一类问题的编程解决方案。如果有必要按响应事件行为的类别来讨论编程问题，那么您的解决方案很可能是显式状态机。</p>
<p><span class="atitle"><font size="2" face="Arial"><strong>   文本处理状态机</strong></font></span></p>
<p>最可能调用显式状态机的一个编程问题涉及到处理文本文件。处理文本文件通常包括读取信息单元（通常叫做字符或行），然后对刚读取的单元执行适当操作。某些情况下，这个处理是“无状态的”（即每个这样的单元都包含了足够的信息，可以正确确定要执行什么操作）。在其它情况下，即使文本文件不是完全无状态，数据也只有有限的上下文（例如，操作取决于不比行号更多的信息）。但是，在其它常见文本处理问题中，输入文件是极具“状态”的。每一块数据的含义取决于它前面的字符串（也许是它后面的字符串）。报告、大型机数据输入、可读文本、编程源文件和其它种类的文本文件都是有状态的。一个简单例子是可能出现在 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 源文件中的一行代码：</p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><font face="Lucida Console">myObject = SomeClass(this, that, other)      </font></code></pre>
</td>
</tr>
</table>
<p>这行表示，如果恰好有以下几行围绕着这一行，则有部分内容不同：</p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><font face="Lucida Console">"""How to use SomeClass:myObject = SomeClass(this, that, other)"""      </font></code></pre>
</td>
</tr>
</table>
<p>我们应知道我们处于“块引用” <strong>状态</strong> 以确定这行代码是一部分注释而不是 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 操作。</p>
<p><span class="atitle"><font size="4" face="Arial">何时不使用状态机</font></span></p>
<p>当开始为任何有状态的文本文件编写处理器的任务时，问一问自己，您希望在文件中找到什么类型的输入项。每种类型的输入项都是一种状态的候选项。这些类型共有几种。如果数字很大或者不确定，则状态机也许不是正确的解决方法。（在这种情况下，某些数据库解决方案可能更适合。）</p>
<p>还请考虑您是否需要使用状态机。许多情况下，最好从更简单的方法入手。也许会发现即使文本文件是有状态的，也有一种简单的方法可以分块读取它（其中每一块是一种类型的输入值）。实际上，在单一状态块中，仅当文本类型之间的转移需要基于内容的计算时，才有必要实现状态机。</p>
<p>下面这个简单的例子说明了需要使用状态机的情况。请考虑用于将一列数字划分成几块的两个规则。在第一个规则中，列表中的零表示块之间的间断。第二个规则中，当一个块中的元素总和超过 100 时，会发生块之间的间断。由于它使用累加器变量来确定是否达到了阈值，您不能“马上”看到子列表的边界。因此，第二个规则也许更适合于类似于状态机的机制。</p>
<p>稍微有些状态、但由 <em>不</em> 太适合用状态机处理的文本文件的例子是 <a href="http://www.donevii.com/post/tag/windows" class="st_tag internal_tag" rel="tag" title="Posts tagged with windows">Windows</a> 风格的 .ini 文件。这种文件包括节头、注释和许多赋值。例如：</p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><font face="Lucida Console">; set the colorscheme and userlevel[colorscheme]background=redforeground=bluetitle=green[userlevel]login=2title=1      </font></code></pre>
</td>
</tr>
</table>
<p>我们的例子没有实际含义，但它表明了 .ini 格式一些有趣的特性。</p>
<ul>
<li>就某种意义而言，每一行的类型由它的第一个字符确定（可能是分号、左花括号或字母）。</li>
<li>从另一种角度看，这种格式是“有状态的”，因为关键字 &#8220;title&#8221; 大概表示如果它出现在每一节中，那么就有独立的内容。</li>
</ul>
<p>您可以编写一个有 COLORSCHEME 状态和 USERLEVEL 状态的文本处理器程序，这个程序仍处理每个状态的赋值。但这好象不是处理此问题的 <em>正确</em> 方法。例如，可以使用 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 代码在这个文本文件中只创建自然块，如：</p>
<p><strong>处理 .INI 文件的分块 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 代码</strong></p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><font face="Lucida Console">stringtxt = open( 'hypothetical.ini').read()</font></code><code class="section"><font face="Lucida Console">sects = string.split(txt,'[')</font><span class="boldcode"><strong><font face="Lucida Console">for </font></strong></span><font face="Lucida Console">sect </font><span class="boldcode"><strong><font face="Lucida Console">in </font></strong></span><font face="Lucida Console">sects:    # do something with sect, like get its name    # (the stuff up to ']') and read its assignments               </font></code></pre>
</td>
</tr>
</table>
<p>或者，如果愿意，可以使用单个 <code><font face="新宋体">current_section</font></code> 变量来确定位置：</p>
<p><strong>处理 .INI 文件的计算 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 代码</strong></p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><span class="boldcode"><strong><font face="Lucida Console">for </font></strong></span><font face="Lucida Console">line </font><span class="boldcode"><strong><font face="Lucida Console">in </font></strong></span><font face="Lucida Console">open('hypothetical.ini').readlines():</font><span class="boldcode"><strong><font face="Lucida Console">    if </font></strong></span><font face="Lucida Console">line[0] == '[': current_section = line(1:-2)     </font><span class="boldcode"><strong><font face="Lucida Console">elif </font></strong></span><font face="Lucida Console">line[0] == ';':</font><span class="boldcode"><strong><font face="Lucida Console">        pass </font></strong></span><font face="Lucida Console"># ignore comments            </font><span class="boldcode"><strong><font face="Lucida Console">    else</font></strong></span><font face="Lucida Console">:         apply_value(current_section, line)            </font></code></pre>
</td>
</tr>
</table>
<p><span class="atitle"><font size="4" face="Arial">何时使用状态机</font></span></p>
<p>现在，我们已经决定了如果文本文件“太简单”就不使用状态机，让我们再研究 <em>需要</em>使用状态机的情况。本专栏中 最近一篇文章 讨论了实用程序 Txt2Html，它将“智能 ASCII”（包括本文）转换成 <a href="http://www.donevii.com/post/tag/html" class="st_tag internal_tag" rel="tag" title="Posts tagged with html">HTML</a>。让我们扼要重述。</p>
<p>“智能 ASCII”是一种文本格式，它使用一些间隔约定来区分文本块的类型，如头、常规文本、引语和代码样本。虽然读者或作者能容易地通过查看分析这些文本块类型之间的转移，但却没有简单的方法可以让计算机将“智能 ASCII”文件分割成组成它的文本块。不像 .ini 文件示例，文本块类型可以任何顺序出现。在任何情况下都没有单一定界符来分隔块（空行 <em>通常</em> 分隔文本块，但代码样本中的空行却不一定结束代码样本，并且文本块不需要用空行来分隔）。由于需要以不同方式重新格式化每个文本块以生成正确的 <a href="http://www.donevii.com/post/tag/html" class="st_tag internal_tag" rel="tag" title="Posts tagged with html">HTML</a> 输出，状态机似乎就是自然的解决方案。</p>
<p>Txt2Html 阅读器的一般功能如下：</p>
<ol>
<li>在初始状态中启动。</li>
<li>读取一行输入。</li>
<li>根据输入和当前状态，转移到新状态或按适合当前状态的方式处理该行。</li>
</ol>
<p>这个例子是关于您会遇到的最简单的情况，但它说明了我们描述过的以下模式：</p>
<p><strong><a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 中一个简单的状态机输入循环</strong><br clear="all" /></p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><span class="boldcode"><strong><font face="Lucida Console">global </font></strong></span><font face="Lucida Console">state, blocks, bl_num, newblock#-- Initialize the globalsstate = "HEADER"blocks = [""]bl_num = 0newblock = 1</font><span class="boldcode"><strong><font face="Lucida Console">for </font></strong></span><font face="Lucida Console">line </font><span class="boldcode"><strong><font face="Lucida Console">in </font></strong></span><font face="Lucida Console">fhin.readlines():</font><span class="boldcode"><strong><font face="Lucida Console">    if </font></strong></span><font face="Lucida Console">state == "HEADER":        # blank line means new block of header</font><span class="boldcode"><strong><font face="Lucida Console">       if </font></strong></span><font face="Lucida Console">blankln.match(line): newblock = 1</font><span class="boldcode"><strong><font face="Lucida Console">       elif </font></strong></span><font face="Lucida Console">textln.match(line): startText(line)</font></code><code class="section"><span class="boldcode"><strong><font face="Lucida Console">       elif </font></strong></span><font face="Lucida Console">codeln.match(line): startCode(line)</font><span class="boldcode"><strong><font face="Lucida Console">       else </font></strong></span><font face="Lucida Console">:</font><span class="boldcode"><strong><font face="Lucida Console">           if </font></strong></span><font face="Lucida Console">newblock: startHead(line)</font><span class="boldcode"><strong><font face="Lucida Console">           else </font></strong></span><font face="Lucida Console">: blocks[bl_num] = blocks[bl_num] + line</font><span class="boldcode"><strong><font face="Lucida Console">           elif </font></strong></span><font face="Lucida Console">state == "TEXT":                 # blank line means new block of text</font><span class="boldcode"><strong><font face="Lucida Console">               if </font></strong></span><font face="Lucida Console">blankln.match(line): newblock = 1</font></code><code class="section"><span class="boldcode"><strong><font face="Lucida Console">               elif </font></strong></span><font face="Lucida Console">headln.match(line): startHead(line)</font></code><code class="section"><span class="boldcode"><strong><font face="Lucida Console">               elif </font></strong></span><font face="Lucida Console">codeln.match(line): startCode(line)</font><span class="boldcode"><strong><font face="Lucida Console">               else </font></strong></span><font face="Lucida Console">:     </font><span class="boldcode"><strong><font face="Lucida Console">               if </font></strong></span><font face="Lucida Console">newblock: startText(line)     </font><span class="boldcode"><strong><font face="Lucida Console">               else </font></strong></span><font face="Lucida Console">: blocks[bl_num] = blocks[bl_num] + line     </font><span class="boldcode"><strong><font face="Lucida Console">               elif </font></strong></span><font face="Lucida Console">state == "CODE":                          # blank line does not change state      </font><span class="boldcode"><strong><font face="Lucida Console">                  if </font></strong></span><font face="Lucida Console">blankln.match(line): blocks[bl_num] = blocks[bl_num] + line         </font><span class="boldcode"><strong><font face="Lucida Console">               elif </font></strong></span><font face="Lucida Console">headln.match(line): startHead(line)         </font><span class="boldcode"><strong><font face="Lucida Console">               elif </font></strong></span><font face="Lucida Console">textln.match(line): startText(line) </font></code><code class="section"><font face="Lucida Console">        </font><span class="boldcode"><strong><font face="Lucida Console">               else </font></strong></span><font face="Lucida Console">: blocks[bl_num] = blocks[bl_num] + line         </font><span class="boldcode"><strong><font face="Lucida Console">               else </font></strong></span><font face="Lucida Console">:        </font><span class="boldcode"><strong><font face="Lucida Console">                     raise </font></strong></span><font face="Lucida Console">ValueError, "unexpected input block state: "+state            </font></code></pre>
</td>
</tr>
</table>
<p>可以用 Txt2Html 下载从中取出该代码的源文件（请参阅 参考资料 ）。请注意：变量 <code><font face="新宋体">state</font></code> 声明为 <code><font face="新宋体">global</font></code> ，在函数（如 <code><font face="新宋体">startText()</font></code> ）中更改它的值。转移条件，如 <code><font face="新宋体">textln.match()</font></code> ，是规则表达式模式，但它们可能也是定制函数。实际上，以后会在程序中执行格式化。状态机只将文本文件分析成 <code><font face="新宋体">blocks</font></code> 列表中带标签的块。</p>
<p><span class="atitle"><font size="4" face="Arial">抽象状态机类</font></span></p>
<p>在表单和函数中使用 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 实现抽象状态机很容易。这使程序的状态机模型比前一个例子中的简单条件块显得更突出（初看，其中的条件与其它条件没有什么不同）。而且，以下类及其关联处理程序在隔离状态中操作方面完成得很好。许多情况下，这改善了封装和可读性。</p>
<p><strong>文件：statemachine.py</strong><br clear="all" /></p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><span class="boldcode"><strong><font face="Lucida Console">from </font></strong></span><font face="Lucida Console">string </font><span class="boldcode"><strong><font face="Lucida Console">import </font></strong></span><font face="Lucida Console">upper</font><span class="boldcode"><strong><font face="Lucida Console"><a href="http://www.donevii.com/post/tag/class" class="st_tag internal_tag" rel="tag" title="Posts tagged with class">class</a> </font></strong><strong><font face="Lucida Console">StateMachine </font></strong></span><font face="Lucida Console">:    </font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">__init__</font></strong></span><font face="Lucida Console">(self):        self.handlers = {}        self.startState = None       </font></code><code class="section"><font face="Lucida Console"> self.endStates = []    </font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">add_state</font></strong></span><font face="Lucida Console">(self, name, handler, end_state=0):        name = upper(name)       </font></code><code class="section"><font face="Lucida Console"> self.handlers[name] = handler</font><span class="boldcode"><strong><font face="Lucida Console">       if </font></strong></span><font face="Lucida Console">end_state: self.endStates.append(name)</font></code><code class="section"><font face="Lucida Console">    </font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">set_start</font></strong></span><font face="Lucida Console">(self, name):        self.startState = upper(name)    </font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">run</font></strong></span><font face="Lucida Console">(self, cargo):        </font><span class="boldcode"><strong><font face="Lucida Console">try </font></strong></span><font face="Lucida Console">: handler = self.handlers[self.startState]        </font><span class="boldcode"><strong><font face="Lucida Console">except </font></strong></span><font face="Lucida Console">:             </font><span class="boldcode"><strong><font face="Lucida Console">raise </font></strong></span><font face="Lucida Console">"InitializationError", "must call .set_start() before .run()"</font><span class="boldcode"><strong><font face="Lucida Console">       if </font></strong></span><span class="boldcode"><strong><font face="Lucida Console">not </font></strong></span><font face="Lucida Console">self.endStates:</font><span class="boldcode"><strong><font face="Lucida Console">          raise </font></strong></span><font face="Lucida Console">"InitializationError", "at least one state must be an end_state"    </font><span class="boldcode"><strong><font face="Lucida Console">    while </font></strong></span><font face="Lucida Console">1: (newState, cargo) = handler(cargo)</font><span class="boldcode"><strong><font face="Lucida Console">          if </font></strong></span><font face="Lucida Console">upper(newState)</font><span class="boldcode"><strong><font face="Lucida Console"> in </font></strong></span><font face="Lucida Console">self.endStates:</font><span class="boldcode"><strong><font face="Lucida Console"> break</font></strong></span><span class="boldcode"><strong><font face="Lucida Console">          else </font></strong></span><font face="Lucida Console">: handler = self.handlers[upper(newState)]            </font></code></pre>
</td>
</tr>
</table>
<p><code><font face="新宋体">   StateMachine</font></code> 类实际上正是抽象状态机所需要的。因为使用 <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> 传递函数对象是如此简单，与其它语言中的相似类比较，这个类所需使用行数非常少。</p>
<p>   要真正 <em>使用</em> <code><font face="新宋体">StateMachine</font></code> 类，需要为每个要使用的状态创建一些处理程序。处理程序必须符合模式。它循环处理事件，直到要转移到另一个状态，此时处理程序应该将一个字节组（它包括新状态名称以及新的状态处理程序需要的任何 cargo）传递回去。</p>
<p>   在 <code><font face="新宋体">StateMachine</font></code> 类中将 <code><font face="新宋体">cargo</font></code> 用作变量的做法将封装状态处理程序所需的数据（该状态处理程序不必调用它的 <code><font face="新宋体">cargo</font></code> 变量）。状态处理程序使用 <code><font face="新宋体">cargo</font></code> 来传递下一个处理程序所需的内容，于是新的处理程序可以接管前一个处理程序的遗留工作。 <code><font face="新宋体">cargo</font></code> 通常包括文件句柄，它允许下一个处理程序可以在前一个处理程序停止后读取更多数据。 <code><font face="新宋体">cargo</font></code> 还可能是数据库连接、复杂的类实例或带有几个项的列表。</p>
<p>   现在，让我们研究测试样本。在本例中（在以下代码示例中概述），cargo 只是不断将反馈传送给迭代函数的一个数字。只要 <code><font face="新宋体">val</font></code> 处于某个范围内，则 <code><font face="新宋体">val</font></code> 的下一个值永远只是 <code><font face="新宋体">math_func(val)</font></code> 。一旦函数返回了超出范围的值，那么该值将传送到另一个处理程序，或者状态机在调用了一个什么也不做的终态处理程序后就退出。示例说明了一件事： <em>事件</em>不必是输入事件。它也可以是计算事件（这种情况很少）。状态处理程序相互之间的区别只是在输出它们处理的事件时使用不同的标记。该函数比较简单，没必要使用状态机。但它很好地说明了概念。代码也许比解释更易于理解！</p>
<p> <strong>文件：statemachine_test.py<br clear="all" /></strong></p>
<table border="1" bgColor="#eeeeee" width="100%" cellPadding="5" cellSpacing="0">
<tr>
<td>
<pre><code class="section"><span class="boldcode"><strong><font face="Lucida Console">from </font></strong></span><font face="Lucida Console">statemachine </font><span class="boldcode"><strong><font face="Lucida Console">import </font></strong></span><font face="Lucida Console">StateMachine</font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">ones_counter</font></strong></span><font face="Lucida Console">(val):    </font><span class="boldcode"><strong><font face="Lucida Console">print </font></strong></span><font face="Lucida Console">"ONES State: ",    </font><span class="boldcode"><strong><font face="Lucida Console">while </font></strong></span><font face="Lucida Console">1:        </font><span class="boldcode"><strong><font face="Lucida Console">if </font></strong></span><font face="Lucida Console">val &lt;= 0 </font><span class="boldcode"><strong><font face="Lucida Console">or </font></strong></span><font face="Lucida Console">val &gt;= 30:            newState = "Out_of_Range";            </font><span class="boldcode"><strong><font face="Lucida Console">break       elif</font></strong></span><font face="Lucida Console"> 20 &lt;= val &lt; 30:            newState = "TWENTIES";</font><span class="boldcode"><strong><font face="Lucida Console">          break       elif </font></strong></span><font face="Lucida Console">10 &lt;= val &lt; 20:            newState = "TENS";</font><span class="boldcode"><strong><font face="Lucida Console">          break       else </font></strong></span><font face="Lucida Console">:</font><span class="boldcode"><strong><font face="Lucida Console">          print </font></strong></span><font face="Lucida Console">" @ %2.1f+" % val, val = math_func(val)</font><span class="boldcode"><strong><font face="Lucida Console">          print </font></strong></span><font face="Lucida Console">" &gt;&gt;"    </font><span class="boldcode"><strong><font face="Lucida Console">return </font></strong></span><font face="Lucida Console">(newState, val)</font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">tens_counter </font></strong></span><font face="Lucida Console">(val):    </font><span class="boldcode"><strong><font face="Lucida Console">print </font></strong></span><font face="Lucida Console">"TENS State: ",    </font><span class="boldcode"><strong><font face="Lucida Console">while 1</font></strong></span><font face="Lucida Console">:        </font><span class="boldcode"><strong><font face="Lucida Console">if </font></strong></span><font face="Lucida Console">val &lt;= 0 </font><span class="boldcode"><strong><font face="Lucida Console">or </font></strong></span><font face="Lucida Console">val &gt;= 30: newState =  "Out_of_Range"; </font><span class="boldcode"><strong><font face="Lucida Console">break       elif </font></strong></span><font face="Lucida Console">1 &lt;= val &lt; 10: newState = "ONES";</font><span class="boldcode"><strong><font face="Lucida Console"> break       elif </font></strong></span><font face="Lucida Console">20 &lt;= val &lt; 30: newState = "TWENTIES"; </font><span class="boldcode"><strong><font face="Lucida Console">break     </font></strong></span></code><code class="section"><span class="boldcode"><strong><font face="Lucida Console">  else </font></strong></span><font face="Lucida Console">:               </font><span class="boldcode"><strong><font face="Lucida Console">print </font></strong></span><font face="Lucida Console">" #%2.1f+" % val, val = math_func(val)</font></code><code class="section"><span class="boldcode"><strong><font face="Lucida Console">            print </font></strong></span><font face="Lucida Console">" &gt;&gt;"</font><span class="boldcode"><strong><font face="Lucida Console">    return</font></strong></span><font face="Lucida Console">(newState, val)</font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">twenties_counter</font></strong></span><font face="Lucida Console">(val):</font><span class="boldcode"><strong><font face="Lucida Console">    print </font></strong></span><font face="Lucida Console">"TWENTIES State:",</font><span class="boldcode"><strong><font face="Lucida Console">    while </font></strong></span><font face="Lucida Console">1:        </font><span class="boldcode"><strong><font face="Lucida Console">if </font></strong></span><font face="Lucida Console">val &lt;= 0 </font><span class="boldcode"><strong><font face="Lucida Console">or </font></strong></span><font face="Lucida Console">val &gt;= 30: newState = "Out_of_Range"; </font><span class="boldcode"><strong><font face="Lucida Console">break       elif </font></strong></span><font face="Lucida Console">1 &lt;= val &lt; 10: newState = "ONES"; </font><span class="boldcode"><strong><font face="Lucida Console">break       elif </font></strong></span><font face="Lucida Console">10 &lt;= val &lt; 20: newState = "TENS";</font><span class="boldcode"><strong><font face="Lucida Console"> break     </font></strong></span></code><code class="section"><span class="boldcode"><strong><font face="Lucida Console">  else </font></strong></span><font face="Lucida Console">:             </font><span class="boldcode"><strong><font face="Lucida Console">print </font></strong></span><font face="Lucida Console">" *%2.1f+" % val, val = math_func(val)</font><span class="boldcode"><strong><font face="Lucida Console">           print </font></strong></span><font face="Lucida Console">" &gt;&gt;"    </font><span class="boldcode"><strong><font face="Lucida Console">return </font></strong></span><font face="Lucida Console">(newState, val)</font><span class="boldcode"><strong><font face="Lucida Console">def </font></strong><strong><font face="Lucida Console">math_func</font></strong></span><font face="Lucida Console">(n):    </font><span class="boldcode"><strong><font face="Lucida Console">from </font></strong></span><font face="Lucida Console">math </font><span class="boldcode"><strong><font face="Lucida Console">import </font></strong></span><font face="Lucida Console">sin    </font><span class="boldcode"><strong><font face="Lucida Console">return </font></strong></span><font face="Lucida Console">abs(sin(n))*31</font><span class="boldcode"><strong><font face="Lucida Console">if </font></strong></span><font face="Lucida Console">__name__== "__main__":    m = StateMachine()   </font></code><code class="section"><font face="Lucida Console"> m.add_state("ONES", ones_counter)    m.add_state("TENS", tens_counter)    m.add_state("TWENTIES", twenties_counter)    m.add_state("OUT_OF_RANGE", None, end_state=1)    m.set_start("ONES")    m.run(1)            </font></code></pre>
</td>
</tr>
</table>

	<h4>相关文章</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.donevii.com/post/78.html" title="[转]C/C++/Perl/汇编/Java效率比较 (2006-10-11)">[转]C/C++/Perl/汇编/Java效率比较</a> (0)</li>
	<li><a href="http://www.donevii.com/post/387.html" title="[转]嵌入式系统 Boot Loader 技术内幕 (2008-01-14)">[转]嵌入式系统 Boot Loader 技术内幕</a> (0)</li>
	<li><a href="http://www.donevii.com/post/110.html" title="翻译：On having layout (2006-10-24)">翻译：On having layout</a> (0)</li>
	<li><a href="http://www.donevii.com/post/283.html" title="免费软件套装 (2007-01-08)">免费软件套装</a> (0)</li>
	<li><a href="http://www.donevii.com/post/24.html" title="使用Boost.Python构建混合系统 (2006-08-20)">使用Boost.Python构建混合系统</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/36.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用Python分析XML</title>
		<link>http://www.donevii.com/post/16.html</link>
		<comments>http://www.donevii.com/post/16.html#comments</comments>
		<pubDate>Thu, 17 Aug 2006 11:37:40 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[class]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=16</guid>
		<description><![CDATA[Working with XML Documents and Python &#8211; Organizing a Book Collection Hello world级的xml分析教程(基于pyhton) http://www.devshed.com/c/a/Python/Working-with-XML-Documents-and-Python/1/ 有时间的话, 给大家翻译过来. 转至http://www.... ]]></description>
			<content:encoded><![CDATA[<p><span class="contentpagetitle"><strong>Working with XML Documents and <a href="http://www.donevii.com/post/tag/python" class="st_tag internal_tag" rel="tag" title="Posts tagged with python">Python</a> &#8211; Organizing a Book Collection</strong></span></p>
<p><span class="contentpagetitle"></span>Hello world级的xml分析教程(基于pyhton)</p>
<p><a href="http://www.devshed.com/c/a/Python/Working-with-XML-Documents-and-Python/1/">http://www.devshed.com/c/a/Python/Working-with-XML-Documents-and-Python/1/</a></p>
<p>有时间的话, 给大家翻译过来.</p>
<p>转至<a href="http://www.devshed.com">http://www.devshed.com</a></p>

	<h4>相关文章</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.donevii.com/post/64.html" title="模型-视图-控制器 (2006-09-01)">模型-视图-控制器</a> (0)</li>
	<li><a href="http://www.donevii.com/post/125.html" title="搜索关键字 (2006-10-26)">搜索关键字</a> (0)</li>
	<li><a href="http://www.donevii.com/post/232.html" title="各系Rails大点兵 (2006-11-28)">各系Rails大点兵</a> (0)</li>
	<li><a href="http://www.donevii.com/post/24.html" title="使用Boost.Python构建混合系统 (2006-08-20)">使用Boost.Python构建混合系统</a> (0)</li>
	<li><a href="http://www.donevii.com/post/78.html" title="[转]C/C++/Perl/汇编/Java效率比较 (2006-10-11)">[转]C/C++/Perl/汇编/Java效率比较</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/16.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
