﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-gyn-随笔分类-Python</title><link>http://www.cnitblog.com/gyn/category/7430.html</link><description>Win32下的Perl，无用的select，停滞的Tk，结束吧....</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 05:40:30 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 05:40:30 GMT</pubDate><ttl>60</ttl><item><title>django中logout的空template设置</title><link>http://www.cnitblog.com/gyn/archive/2008/07/12/46634.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 12 Jul 2008 07:08:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/07/12/46634.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/46634.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/07/12/46634.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/46634.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/46634.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">在 django中建立 登出用户功能的时候，除了建立以下程序并在urls中增加映射之外，还需要在registration中建立一个叫做 “logged_out.html” 的文件，这是无可避免的，虽然被HttpResponseRedirect('/')重定向到了主页，但至少也需要存在一个空文件。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">from django.contrib.auth.views import logout </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">from django.http import HttpResponseRedirect </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">def page_logout(request):    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    logout(request)    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    return HttpResponseRedirect('/')</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">
								<o:p>
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/46634.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-07-12 15:08 <a href="http://www.cnitblog.com/gyn/archive/2008/07/12/46634.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对Django中sqlite数据库无法建立外键的解析</title><link>http://www.cnitblog.com/gyn/archive/2008/07/10/46568.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 10 Jul 2008 11:49:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/07/10/46568.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/46568.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/07/10/46568.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/46568.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/46568.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">是参照《Learning Website Developing with Django》里面的django_bookmarks一例照搬照抄下来的，问题出现在学习到为bookmarks应用建立数据库表的时候。这里涉及到三张表，第一张是User，用来存储用户信息；第二张是Link，用来存储网页的url；第三张是bookmarks，用来存储书签，书签除了id和书签名之外，还有两个指向User和Link的外键。按照书里面的指示在booksmark.models里插入以下内容：</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">from django import models</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">from django.contrib.auth.models import User</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">class Link(models.Model):</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">       url = URLField(unique = True)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">class Bookmarks(models.Model):</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">       title = CharField(maxlength = 200)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">       user = ForeignKey(User)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">       link = ForeignKey(Link)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">之后，“manage.py syncdb”将以上程序同步到数据库中，再使用“manage.py sqlall bookmarks”查看一下数据库中关于的bookmarks的表的建立情况。实际的输出与书本上的内容有一些差异，就是对于Link建立的外键，显示的结果只是一个非Null的Integer字段，没有“references”。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">这里让人奇怪的一点是，既然没有建立外键，那么这个Integer的类型是怎么决定下来的？也许一种默认行为。这样的话，就很wired和 magic了。还有一种可能就是manag.py对与数据库构成的揭示是存在bug的，也就是说事实上这个外键是已经建立了，只不过没有被显示出来而已。所以使用标准sqlite3.exe，分析数据库bookmarksdb。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite3 bookmarksdb</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlie&gt; .schema</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">在得到的结果中，果不其然，有</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">“link_id” integer NOT NULL REFERENCES “bookmarks_link” (“id”)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
										<br />
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/46568.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-07-10 19:49 <a href="http://www.cnitblog.com/gyn/archive/2008/07/10/46568.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我对pysqlite的一些了解</title><link>http://www.cnitblog.com/gyn/archive/2008/07/04/46302.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Fri, 04 Jul 2008 10:26:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/07/04/46302.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/46302.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/07/04/46302.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/46302.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/46302.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">大约是一年半或者是两年前的样子，在那段时间里，我似乎特别痴迷于各种数据库以及相关的技术。其间看了不少书，可能是平常工作接触的多是</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlserver</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的缘故，我本人对其反而不是特别的感冒，所以看的多是关于</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">postgresql</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">oracle</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的，尤其是前者。记得当时用的是一台</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">celeron1.7G</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">、内存</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">256MB</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的方正电脑，安装这些软件让电脑变得非常的慢，终于在一次忍无可忍的僵死之后，我卸载了所有这些庞然大物。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">之后台里引进了一台用于上下班记录的打卡机，为了方便统计迟到早退，我用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">perl/tk</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">写了一个采集整理程序，后台用到了</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，这也是我和</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的初次邂逅。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">至于对</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">python</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，不好意思地说，我一直抱着骑墙的态度。如果</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">perl</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">可以解决的，我是断然不会用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">python</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的，这一切直到我对</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">poe</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的幻想破灭为止。原先我是期待用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">poe</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">来作为多线程的替代，可事实还是向着相反的方向发展。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">是一个嵌入式的数据库，也就是说没有独立的维护进程，所有的维护都来自于程序本身。麻雀虽小，五脏俱全，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">实现了多数</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sql-92</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的标准，比如说</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">transaction</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">、</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">trigger</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和复杂的查询等。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">有很多介绍的书籍，记得有一本</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">apress</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">出版的《</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">The difinitive guide to sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">》，有毛</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">500</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">面的样子，用来介绍一个嵌入式数据库，着实有点长了，当然对于初学者是有其存在的价值的。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">而</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">pysqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">则是一个</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">为</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">python</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">提供的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">api</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">接口，它让一切对于</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的操作都变得异常简单。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">userguide</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">并不长，但是基本上都说到点子上了，不过这里还是要对其提一下几个要点。事实上我个人认为，理解了这几点就完全足够了。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">from pysqlite2 import dbapi2 as sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">这就是一切的起源。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">下面是一个涉及到我即将介绍的几点的示例程序，内容很简单，就是将一个</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Point</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象存入</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">建立的内存数据库中，然后原封不动地返回出来。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">from pysqlite2 import dbapi2 as sqlite</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">class Point(object):</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   def __init__(self, x, y):</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      self.x, self.y = x, y</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   def __conform__(self, protocol):</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      if protocol is sqlite.PrepareProtocol:</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">          return "%d;%d" % (self.x, self.y)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">def adapt_converter(point):</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   return Point(*[int(x) for x in point.split(";")])</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite.register_converter('point', adapt_converter)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">con=sqlite.connect(":memory:",detect_types= sqlite.PARSE_COLNAMES)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cur = con.cursor()</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cur.execute('select ? as "a [point]"', (Point(2, 3), ))</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">print type(cur.fetchone()[0])</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">首先要做一些准备工作，因为</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">不是什么</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">python</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象都能够接受的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">,</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">为此需要一些必要的转换。这个例子里，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">就像是一个汽车维修站。在站里，为了维修，汽车被拆成了个个零件，等出站时又被组装成了一辆汽车。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">第一步是声明一个</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Point</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">类。这个类除了初始化函数</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">__init__</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">之外，还有一个两参量的函数</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">__conform__</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">在检查存入数据库对象的时候会检查是否存在该函数，如果存在，则将该函数返回的值替代对象本身存入数据库。可以说这是一个转换器，将</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">python</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象转换为</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">允许接纳对象，即</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Null</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">、</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Integer</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">、</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Real</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">、</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Text</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Blob</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">其中之一。在这里，显然</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">是无法接纳</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Point</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">这个类所建立的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Point(2,3)</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象的，所以通过</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">__conform__</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，实际将存入内容为“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">2;3</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”的文本。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">使用“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite.register_adapter(Point, lambda p = point : “%d;%d” % p.x, p.y)</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”，在</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中注册一个适配器也可以完成转换的功能，但是我个人认为这样比较麻烦，不如写在类里来得清楚。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">在处理完了</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Point</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">类之后，为了使文本“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">2;3</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”在从数据库取出时能转换为</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Point</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象，还需要用“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite.register_converter</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”注册一个叫“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">adapt_converter</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”的转换器。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">这样一条拆卸组装的流水线就完成了。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">实际使用到的只有两样东西，一是数据库连接对象</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">con</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，还有一个就是用来执行命令并返回结果（有必要的话）的游标对象</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cur</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，它是由</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">con</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象产生的。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">为了使</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">意识到它正在处理的表中的某一列是已注册需要转换的“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">phone</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”，我们需要在建立表的时候声明该列的属性，或者在查询的时候显示地表明该列的属性。对于前者，建立</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">con</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象是应将</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">detect_types</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">设置为</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite.PARSE_DELTYPES</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">；后者，则是设置为</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite.PARSE_COLNAMES</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">。这里，因为使用的是一个临时表，所以只能使用后者的设置办法。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">事实上，“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">select ? as ……</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”这句包含了两个方面的内容，首先它将</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Point(2, 3)</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">存入了临时表中，这涉及到了</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Phone</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对象到文本的转换。之后，再将文本数据从临时表中取出，又用到了文本到</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Phone</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的反转换。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">下面是一种浅显而罗嗦的写法：</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cur.execute("create table temp(t)")</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cur.execute("insert into temp values (?)", (Point(2, 3),))</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cur.execute('select ? as "a [point]" from temp')</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">最后，打印从</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中取出的对象类型，内容为“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">&lt;class '__main__.Point'&gt;</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">以上这些，可以总结为两点：有必要的话，做好转换；建立</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">con</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cur</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">。理解了这些，差不多就能用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">pysqlite</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">了。当然如果要做工程、写程序还需要很多的其他知识，至少数据库设计这方面的内容是要看一下的。</span>
						<span style="font-weight: bold; font-size: 10.5pt; font-family: '黑体';">
								<o:p>
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/46302.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-07-04 18:26 <a href="http://www.cnitblog.com/gyn/archive/2008/07/04/46302.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对于pil--handboook中反转图片函数的一点疑问</title><link>http://www.cnitblog.com/gyn/archive/2008/07/02/46249.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Wed, 02 Jul 2008 13:03:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/07/02/46249.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/46249.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/07/02/46249.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/46249.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/46249.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">函数的意思很简单，给定一个Image对象和一个delta参数，然后根据这个delta参数将Image对象割成左右两块提取出来，之后互换两块的位置。</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">大致看了一下程序之后，自己写了一个，然后对照范例，除了变量名不一样之外基本上是吻合的，但是运行之后的结果却很不容乐观。图像不是左右互换，而是仿佛还有第三块夹在中间，这就不是需要的效果了。</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">为了找出原因，又仔细查了一遍函数，之后加了两条显示被分割出来的图片的命令。再次运行的结果却一切正常了，但注释掉这两句显示命令之后，毛病又复现。</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">显然在同一幅图片里进行分割黏贴的操作，pil是存在问题的。所以新建了一个Image对象，用于返回反转之后的图片，问题也得到了顺利的解决。</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">至于为什么会出现这个错误的现象，暂时不是很清楚。 </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">原图：</span>
						<span style="font-weight: bold; font-size: 10.5pt; font-family: '黑体';">
								<o:p>
								</o:p>
						</span>
						<br />
				</p>
		</div>
		<img alt="" src="http://www.cnitblog.com/images/cnitblog_com/gyn/js.jpg" width="450" height="254" />
		<br />
		<p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">
				<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
						<font face="黑体">错误的图：</font>
				</span>
				<br />
		</p>
		<img alt="" src="http://www.cnitblog.com/images/cnitblog_com/gyn/js_r.jpg" width="450" height="254" />
		<br />
		<div class="Section0" style="">
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">正确的图：</span>
						<span style="font-weight: bold; font-size: 10.5pt; font-family: '黑体';">
								<o:p>
								</o:p>
						</span>
						<br />
				</p>
		</div>
		<img alt="" src="http://www.cnitblog.com/images/cnitblog_com/gyn/js_rr.jpg" width="450" height="254" />
		<br />
		<div class="Section0" style="">
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">import Image</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">import ImageTk</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">def roll(im, delta):</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    """Roll an image sideways"""</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    x, y = im.size</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    if delta % x == 0:</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        return image</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    im_l = im.crop((0, 0, delta, y))</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    im_r = im.crop((delta, 0, x, y))</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    # im_l.show()</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    # im_r.show()</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    imi = Image.new(im.mode, im.size)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    imi.paste(im_r, (0, 0, x - delta, y))</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    imi.paste(im_l, (x - delta, 0, x, y))</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    return imi</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">try:</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    image = Image.open(r'd:\s\python\pil\tu\js.jpg')</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    im = roll(image, 250)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    if im is not None:</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        im.save(r'd:\s\python\pil\tu\js_r.jpg')</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">except IOError:</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    print "Can not open file"</span>
						<span style="font-weight: bold; font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
		</div>
		<br />
		<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
		<!--[if gte mso 9]><xml>
<w:worddocument>
<w:view>Normal</w:view>
<w:zoom>0</w:zoom>
<w:punctuationkerning/>
<w:drawinggridverticalspacing>7.8 磅</w:drawinggridverticalspacing>
<w:displayhorizontaldrawinggridevery>0</w:displayhorizontaldrawinggridevery>
<w:displayverticaldrawinggridevery>2</w:displayverticaldrawinggridevery>
<w:validateagainstschemas/>
<w:saveifxmlinvalid>false</w:saveifxmlinvalid>
<w:ignoremixedcontent>false</w:ignoremixedcontent>
<w:alwaysshowplaceholdertext>false</w:alwaysshowplaceholdertext>
<w:compatibility>
<w:spaceforul/>
<w:balancesinglebytedoublebytewidth/>
<w:donotleavebackslashalone/>
<w:ultrailspace/>
<w:donotexpandshiftreturn/>
<w:adjustlineheightintable/>
<w:breakwrappedtables/>
<w:snaptogridincell/>
<w:wraptextwithpunct/>
<w:useasianbreakrules/>
<w:dontgrowautofit/>
<w:usefelayout/>
</w:compatibility>
<w:browserlevel>MicrosoftInternetExplorer4</w:browserlevel>
</w:worddocument>
</xml><![endif]-->
		<!--[if gte mso 9]><xml>
<w:latentstyles deflockedstate="false" latentstylecount="156">
</w:latentstyles>
</xml><![endif]-->
		<style>
				<!--
/* Font Definitions */
@font-face
{font-family:黑体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimHei;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
{font-family:"Comic Sans MS";
panose-1:3 15 7 2 3 3 2 2 2 4;
mso-font-charset:0;
mso-generic-font-family:script;
mso-font-pitch:variable;
mso-font-signature:647 0 0 0 159 0;}
@font-face
{font-family:"\@黑体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:Arial;
mso-fareast-font-family:黑体;
mso-bidi-font-family:"Times New Roman";
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
		</style>
		<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<img src ="http://www.cnitblog.com/gyn/aggbug/46249.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-07-02 21:03 <a href="http://www.cnitblog.com/gyn/archive/2008/07/02/46249.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>给上一篇中的ftp服务增加了用户添加和删除的程序</title><link>http://www.cnitblog.com/gyn/archive/2007/11/11/36141.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 11 Nov 2007 07:37:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/11/11/36141.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/36141.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/11/11/36141.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/36141.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/36141.html</trackback:ping><description><![CDATA[最终放弃GUI 了，太麻烦。命令行好搞啊，呵呵。<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #008080;">&nbsp;1</span><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;os,&nbsp;sys<br></span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;md5<br></span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;pysqlite2&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;dbapi2&nbsp;as&nbsp;sqlite<br></span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;user_insert(username,&nbsp;password,&nbsp;homedir,&nbsp;perm,&nbsp;msg_login,&nbsp;msg_quit):<br></span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;username:&nbsp;username&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">anonymous</span><span style="color: #800000;">'</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;os.path.exists(homedir):&nbsp;homedir&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;r</span><span style="color: #800000;">'</span><span style="color: #800000;">D:\None</span><span style="color: #800000;">'</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;perm&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800000;">''</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">r;</span><span style="color: #800000;">'</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">r;w</span><span style="color: #800000;">'</span><span style="color: #000000;">):&nbsp;perm&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">r;</span><span style="color: #800000;">'</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;msg_login:&nbsp;msg_login&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">Login&nbsp;successful.</span><span style="color: #800000;">'</span><span style="color: #000000;"><br></span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;msg_quit:&nbsp;msg_quit&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">Goodbye.</span><span style="color: #800000;">'</span><span style="color: #000000;"><br></span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;dbpath&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s\\users.db</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;os.getcwd()<br></span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;os.path.isfile(dbpath):<br></span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;con&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;sqlite.connect(dbpath,&nbsp;isolation_level&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;None)<br></span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;con.cursor()<br></span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur.execute(</span><span style="color: #800000;">"</span><span style="color: #800000;">CREATE&nbsp;TABLE&nbsp;users(username&nbsp;test&nbsp;primary&nbsp;key,&nbsp;password&nbsp;text,&nbsp;homedir&nbsp;text,&nbsp;perm&nbsp;text,&nbsp;msg_login&nbsp;text,&nbsp;mag_quit&nbsp;text)</span><span style="color: #800000;">"</span><span style="color: #000000;">)<br></span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur.close()<br></span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;con.close()<br></span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;con&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;sqlite.connect(dbpath,&nbsp;isolation_level&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;None)<br></span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;cur&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;con.cursor()<br></span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;cur.execute(</span><span style="color: #800000;">"</span><span style="color: #800000;">insert&nbsp;into&nbsp;users&nbsp;values(?,&nbsp;?,&nbsp;?,&nbsp;?,&nbsp;?,&nbsp;?)</span><span style="color: #800000;">"</span><span style="color: #000000;">,&nbsp;<br></span><span style="color: #008080;">21</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(username,&nbsp;password,&nbsp;homedir,&nbsp;perm,&nbsp;msg_login,&nbsp;msg_quit))<br></span><span style="color: #008080;">22</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;cur.close()<br></span><span style="color: #008080;">23</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;con.close()<br></span><span style="color: #008080;">24</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: #008080;">25</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">try</span><span style="color: #000000;">:<br></span><span style="color: #008080;">26</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;msvcrt&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;getch<br></span><span style="color: #008080;">27</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">except</span><span style="color: #000000;">&nbsp;ImportError:<br></span><span style="color: #008080;">28</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;getch():<br></span><span style="color: #008080;">29</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;tty,&nbsp;termios<br></span><span style="color: #008080;">30</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fd&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;sts.stdin.fileno()<br></span><span style="color: #008080;">31</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old.settings&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;termios.tcgetattr(fd)<br></span><span style="color: #008080;">32</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">:<br></span><span style="color: #008080;">33</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tty.setraw(fd)<br></span><span style="color: #008080;">34</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;sys.stdin.read(</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">35</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">finally</span><span style="color: #000000;">:<br></span><span style="color: #008080;">36</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;termios.tcsetattr(fd,&nbsp;tremios.TCSADRAIN,&nbsp;old.settings)<br></span><span style="color: #008080;">37</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;ch<br></span><span style="color: #008080;">38</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: #008080;">39</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;asterisk_raw_input(label):<br></span><span style="color: #008080;">40</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;sys.stdout.write(label)<br></span><span style="color: #008080;">41</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;strAttr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[]<br></span><span style="color: #008080;">42</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;True:<br></span><span style="color: #008080;">43</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getch()<br></span><span style="color: #008080;">44</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;tmp&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">\r</span><span style="color: #800000;">'</span><span style="color: #000000;">:<br></span><span style="color: #008080;">45</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strAttr.append(tmp)<br></span><span style="color: #008080;">46</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.stdout.write(</span><span style="color: #800000;">'</span><span style="color: #800000;">*</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;">47</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:&nbsp;<br></span><span style="color: #008080;">48</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.stdout.write(</span><span style="color: #800000;">'</span><span style="color: #800000;">\n</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;">49</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;"><br></span><span style="color: #008080;">50</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">''</span><span style="color: #000000;">.join(strAttr)<br></span><span style="color: #008080;">51</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">52</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">53</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">user_info_list&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[]<br></span><span style="color: #008080;">54</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">-*-&nbsp;pyftpdlib&nbsp;user&nbsp;information.&nbsp;-*-\n-*-&nbsp;Separated&nbsp;by&nbsp;':'&nbsp;-*-\n</span><span style="color: #800000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;">55</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;eachLabel&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800000;">"</span><span style="color: #800000;">Username</span><span style="color: #800000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">Password</span><span style="color: #800000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">Homedir</span><span style="color: #800000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">Perm</span><span style="color: #800000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">Msg_login</span><span style="color: #800000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">Msg_quit</span><span style="color: #800000;">"</span><span style="color: #000000;">):<br></span><span style="color: #008080;">56</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;eachLabel&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">Password</span><span style="color: #800000;">'</span><span style="color: #000000;">:<br></span><span style="color: #008080;">57</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user_info&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;md5.new(asterisk_raw_input(eachLabel&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">:&nbsp;&nbsp;</span><span style="color: #800000;">'</span><span style="color: #000000;">)).hexdigest()<br></span><span style="color: #008080;">58</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br></span><span style="color: #008080;">59</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user_info&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;raw_input(eachLabel&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">:&nbsp;&nbsp;</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;">60</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;user_info_list.append(user_info)<br></span><span style="color: #008080;">61</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">user_insert(</span><span style="color: #000000;">*</span><span style="color: #000000;">user_info_list)<br></span><span style="color: #008080;">62</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">-*-&nbsp;Programme&nbsp;Quit.&nbsp;-*-</span><span style="color: #800000;">"</span></div>
<br>user_add.py<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #008080;">&nbsp;1</span><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;os<br></span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;pysqlite2&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;dbapi2&nbsp;as&nbsp;sqlite<br></span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">user_info&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{}<br></span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">dbpath&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s\\users.db</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;os.getcwd()<br></span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;os.path.isfile(dbpath):<br></span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">-*-&nbsp;No&nbsp;user&nbsp;to&nbsp;delete.&nbsp;-*-</span><span style="color: #800000;">'</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br></span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;con&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;sqlite.connect(dbpath,&nbsp;isolation_level&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;None)<br></span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;cur&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;con.cursor()<br></span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;cur.execute(</span><span style="color: #800000;">'</span><span style="color: #800000;">select&nbsp;username&nbsp;from&nbsp;users</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;cur.fetchall()<br></span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;result:&nbsp;<br></span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">-*-&nbsp;No&nbsp;user&nbsp;to&nbsp;delete.&nbsp;-*-</span><span style="color: #800000;">'</span><span style="color: #000000;"><br></span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br></span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[name[0]&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;name&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;result]<br></span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;index,&nbsp;name&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;zip(range(</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;len(result)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">),&nbsp;result):<br></span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">&lt;%d&gt;&nbsp;%s</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;(index,&nbsp;name)<br></span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user_info[index]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;name<br></span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">''</span><span style="color: #000000;"><br></span><span style="color: #008080;">21</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user_index&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;int(raw_input(</span><span style="color: #800000;">"</span><span style="color: #800000;">Enter&nbsp;the&nbsp;index&nbsp;of&nbsp;user&nbsp;to&nbsp;delete:&nbsp;</span><span style="color: #800000;">"</span><span style="color: #000000;">))<br></span><span style="color: #008080;">22</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;user_index&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;range(0,&nbsp;len(result)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br></span><span style="color: #008080;">23</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user_index&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;raw_input(</span><span style="color: #800000;">"</span><span style="color: #800000;">Wrong&nbsp;index,&nbsp;re_enter:&nbsp;</span><span style="color: #800000;">"</span><span style="color: #000000;">)<br></span><span style="color: #008080;">24</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;user_index:<br></span><span style="color: #008080;">25</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cur.execute(</span><span style="color: #800000;">"</span><span style="color: #800000;">delete&nbsp;from&nbsp;users&nbsp;where&nbsp;username&nbsp;=&nbsp;'%s'</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;user_info[user_index])<br></span><span style="color: #008080;">26</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">-*-&nbsp;'%s'&nbsp;is&nbsp;deleted.&nbsp;-*-</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;user_info[user_index]<br></span><span style="color: #008080;">27</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;cur.close()<br></span><span style="color: #008080;">28</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;con.close()<br></span><span style="color: #008080;">29</span><span style="color: #000000;"><img src="http://www.cnitblog.com/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">-*-&nbsp;Programme&nbsp;Quit.&nbsp;-*-</span><span style="color: #800000;">"</span></div>
user_del.py <img src ="http://www.cnitblog.com/gyn/aggbug/36141.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-11-11 15:37 <a href="http://www.cnitblog.com/gyn/archive/2007/11/11/36141.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用pyftpdlib和pysqlite3建立可存储用户信息FTP服务</title><link>http://www.cnitblog.com/gyn/archive/2007/11/10/36096.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 10 Nov 2007 03:15:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/11/10/36096.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/36096.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/11/10/36096.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/36096.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/36096.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 前一段时间因为版权的原因，停用了SERV-U服务。但是ftp的服务不能停止，所以不得不自己动手写一个。但是由于时间原因，无法完全认真去看协议，所以希望利用现有的模块。这类模块在perl和python中都有，不过我差不多有半年没碰perl了，自然选择了python。除了几个无关紧要的bug，这个模块基本能满足日常使用。只是用户信息的存储不是很保险，如果存到数据库中总能好一些。密码则参照范例中的md5...&nbsp;&nbsp;<a href='http://www.cnitblog.com/gyn/archive/2007/11/10/36096.html'>阅读全文</a><img src ="http://www.cnitblog.com/gyn/aggbug/36096.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-11-10 11:15 <a href="http://www.cnitblog.com/gyn/archive/2007/11/10/36096.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>获取文件夹大小的python代码</title><link>http://www.cnitblog.com/gyn/archive/2007/11/01/35698.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 01 Nov 2007 06:25:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/11/01/35698.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/35698.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/11/01/35698.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/35698.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/35698.html</trackback:ping><description><![CDATA[可能是太简单的缘故，在google中查询不到，所以贴一下免得忘记了：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;os<br></span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;os.path&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;join,&nbsp;getsize<br><br></span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;getdirsize(dir):<br>&nbsp;&nbsp;&nbsp;size&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;0L<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;root,&nbsp;dirs,&nbsp;files&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;os.walk(dir):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;sum([getsize(join(root,&nbsp;name))&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;name&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;files])<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;size<br><br></span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">__name__</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">__main__</span><span style="color: #800000;">'</span><span style="color: #000000;">:<br>&nbsp;&nbsp;&nbsp;filesize&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getdirsize(r</span><span style="color: #800000;">'</span><span style="color: #800000;">c:\windows</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">There&nbsp;are&nbsp;%.3f</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;(size</span><span style="color: #000000;">/</span><span style="color: #000000;">1024</span><span style="color: #000000;">/</span><span style="color: #000000;">1024</span><span style="color: #000000;">),&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">Mbytes&nbsp;in&nbsp;c:\\windows</span><span style="color: #800000;">'</span></div>
<br> <img src ="http://www.cnitblog.com/gyn/aggbug/35698.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-11-01 14:25 <a href="http://www.cnitblog.com/gyn/archive/2007/11/01/35698.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python中unicode解决之道</title><link>http://www.cnitblog.com/gyn/archive/2007/10/24/35289.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Wed, 24 Oct 2007 12:26:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/10/24/35289.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/35289.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/10/24/35289.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/35289.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/35289.html</trackback:ping><description><![CDATA[<p class="MsoNormal" style="text-indent: 17.95pt;">
<span style="font-family: 宋体;">和前一篇的起因一样，也是在使用</span>
<span lang="EN-US">wxPython</span>
<span style="font-family: 宋体;">的时候。因为不是每个人都熟悉英文（事实上，我总认为不熟悉或者说是生疏与英文的人，大多憎恨英文），不可避免的，需要显示一些中文。但是运行的结果显示大体如&#8220;</span>
<span lang="EN-US">Syntax Error: Non ASCII character in &#8216;\xc4&#8217;&#8230;.</span>
<span style="font-family: 宋体;">&#8221;的错误提示。按照</span>
<span lang="EN-US">Dive into Python</span>
<span style="font-family: 宋体;">的解释，这是因为</span>
<span lang="EN-US">python</span>
<span style="font-family: 宋体;">默认使用</span>
<span lang="EN-US">ASCII</span>
<span style="font-family: 宋体;">编解码，而中文显然不是标准的</span>
<span lang="EN-US">ASCII</span>
<span style="font-family: 宋体;">。<br>&nbsp;&nbsp; 按照文中的解决办法，是要在程序的第一或二行键入形如&#8220;</span>
<span lang="EN-US"># -*- coding:
gbk -*-</span>
<span style="font-family: 宋体;">&#8221;或者将其中的</span>
<span lang="EN-US">gbk</span>
<span style="font-family: 宋体;">改为</span>
<span lang="EN-US">cp936</span>
<span style="font-family: 宋体;">，这有什么不同。不过也许因为一些大牛习惯的缘故，使用</span>
<span lang="EN-US">cp936</span>
<span style="font-family: 宋体;">更酷一点。</span>
<span lang="EN-US">Google</span>
<span style="font-family: 宋体;">中的一种简单的写法是&#8220;</span>
<span lang="EN-US">#coding=cp936</span>
<span style="font-family: 宋体;">&#8221;（需要注意的是和</span>
<span lang="EN-US">bash</span>
<span style="font-family: 宋体;">的语法相类似的，&#8220;</span>
<span lang="EN-US">=</span>
<span style="font-family: 宋体;">&#8221;两旁不能有空格），不过这样没有前一种来得顺眼，可按个人爱好任意选择。<br>&nbsp;&nbsp; 这样看来，问题似乎解决了。不过事事难预料，由于程序需要对</span>
<span lang="EN-US">sqlite</span>
<span style="font-family: 宋体;">进行读写，在将这些中文写入数据库的时候也出现了编解码错误的提示。看来</span>
<span lang="EN-US">pysqlite</span>
<span style="font-family: 宋体;">似乎只能处理</span>
<span lang="EN-US">unicode</span>
<span style="font-family: 宋体;">的数据，用惯了</span>
<span lang="EN-US">perl</span>
<span style="font-family: 宋体;">的</span>
<span lang="EN-US">DBI</span>
<span style="font-family: 宋体;">，一时还无法接受这一而再烦人的错误。<br>&nbsp;&nbsp; 解决的办法是将中文用</span>
<span lang="EN-US">unicode</span>
<span style="font-family: 宋体;">实例化，然后在传给</span>
<span lang="EN-US">pysqlite</span>
<span style="font-family: 宋体;">，具体如：</span>
<span lang="EN-US">u&#8217;</span>
<span style="font-family: 宋体;">你好</span>
<span lang="EN-US">&#8217;</span>
<span style="font-family: 宋体;">这样。但是对于变量不能这样简化做，甚至于</span>
<span lang="EN-US">u&#8217;%s&#8217; % niha</span>
<span style="font-family: 宋体;">（其中</span>
<span lang="EN-US">nihao</span>
<span style="font-family: 宋体;">被赋值了&#8216;你好&#8217;）的形式也不行。这里我们需要用</span>
<span lang="EN-US">unicode</span>
<span style="font-family: 宋体;">的完整形式，</span>
<span lang="EN-US">unicode(nihao, &#8216;cp936&#8217;)</span>
<span style="font-family: 宋体;">。</span>
</p><img src ="http://www.cnitblog.com/gyn/aggbug/35289.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-10-24 20:26 <a href="http://www.cnitblog.com/gyn/archive/2007/10/24/35289.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>wxPython中的一个登录对话框（Login Dialog）</title><link>http://www.cnitblog.com/gyn/archive/2007/10/23/35226.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Tue, 23 Oct 2007 08:37:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/10/23/35226.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/35226.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/10/23/35226.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/35226.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/35226.html</trackback:ping><description><![CDATA[在使用wxPython的过程中，临时需要加入一个登录对话框，因为不是标准Dialog，所以在google里搜索了一下，相关的信息只能用凤毛麟角来形容。查询了在wxPython DEMO中标准Dialog实例代码，发现与所需的极为类似，所以稍作修改就被派上用场了。<br>其中密码文本框的style是wx.TE_PASSWORD。类实例在ShowModal返回wx.ID_OK之后，调用GetValue返回用户名和密码，以供调用代码验证之用。<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;-*-&nbsp;coding:&nbsp;gbk&nbsp;-*-</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;wx<br><br></span><span style="color: #008000;">#</span><span style="color: #008000;">-------------------------------------------------------------------------</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;LoginDialog(wx.Dialog):<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">__init__</span><span style="color: #000000;">(self,&nbsp;parent,&nbsp;id,&nbsp;title,&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">350</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">200</span><span style="color: #000000;">),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.DefaultPosition,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.DEFAULT_DIALOG_STYLE,&nbsp;useMetal&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;False):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.PreDialog()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre.Create(parent,&nbsp;id,&nbsp;title,&nbsp;pos,&nbsp;size,&nbsp;style)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.PostCreate(pre)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.user,&nbsp;self.password&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">''</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">''</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.CreateSizer()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;dataEntries(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;((</span><span style="color: #800000;">'</span><span style="color: #800000;">用户&nbsp;&nbsp;</span><span style="color: #800000;">'</span><span style="color: #000000;">,&nbsp;0,&nbsp;self.OnUser),(</span><span style="color: #800000;">'</span><span style="color: #800000;">密码&nbsp;&nbsp;</span><span style="color: #800000;">'</span><span style="color: #000000;">,&nbsp;wx.TE_PASSWORD,&nbsp;self.OnPassword))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;dataButtons(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;((wx.ID_OK,&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">确定</span><span style="color: #800000;">'</span><span style="color: #000000;">),(wx.ID_CANCEL,&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">取消</span><span style="color: #800000;">'</span><span style="color: #000000;">))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;CreateSizer(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.BoxSizer(wx.VERTICAL)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizer.Add(wx.StaticText(self,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">请输入用户名和密码</span><span style="color: #800000;">'</span><span style="color: #000000;">),&nbsp;0,&nbsp;wx.ALIGN_CENTER&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.ALL,&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;eachLabel,&nbsp;eachStyle,&nbsp;eachHandler&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;self.dataEntries():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.CreateEntry(sizer,&nbsp;eachLabel,&nbsp;eachStyle,&nbsp;eachHandler)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizer.Add(wx.StaticLine(self,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;size</span><span style="color: #000000;">=</span><span style="color: #000000;">(</span><span style="color: #000000;">20</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">),&nbsp;style</span><span style="color: #000000;">=</span><span style="color: #000000;">wx.LI_HORIZONTAL),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,&nbsp;wx.GROW&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.ALIGN_CENTER_VERTICAL&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.RIGHT&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.TOP,&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btnsizer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.StdDialogButtonSizer()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;eachId,&nbsp;eachLabel&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;self.dataButtons():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.CreateButton(btnsizer,&nbsp;eachId,&nbsp;eachLabel)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btnsizer.Realize()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizer.Add(btnsizer,&nbsp;0,&nbsp;wx.ALIGN_CENTER_VERTICAL&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.ALL,&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.SetSizer(sizer)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizer.Fit(self)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;CreateEntry(self,&nbsp;sizer,&nbsp;label,&nbsp;style,&nbsp;handler):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;box&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.BoxSizer(wx.HORIZONTAL)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;box.Add(wx.StaticText(self,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;label),&nbsp;0,&nbsp;wx.ALIGN_CENTER&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.ALL,&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.TextCtrl(self,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;</span><span style="color: #800000;">""</span><span style="color: #000000;">,&nbsp;size&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">80</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">),&nbsp;style&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;style)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text.Bind(wx.EVT_TEXT,&nbsp;handler)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;box.Add(text,&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;wx.ALIGN_CENTER&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.ALL,&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizer.Add(box,&nbsp;0,&nbsp;wx.GROW&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.ALIGN_CENTER_VERTICAL&nbsp;</span><span style="color: #000000;">|</span><span style="color: #000000;">&nbsp;wx.ALL,&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;CreateButton(self,&nbsp;btnsizer,&nbsp;id,&nbsp;label):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;wx.Button(self,&nbsp;id,&nbsp;label)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;id&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;wx.ID_OK:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.SetDefault()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btnsizer.AddButton(button)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;OnUser(self,&nbsp;event):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.user&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;event.GetString()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;OnPassword(self,&nbsp;event):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.password&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;event.GetString()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;GetValue(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(self.user,&nbsp;self.password)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008000;">#</span><span style="color: #008000;">-------------------------------------------------------------------------&nbsp;</span></div>
<br><img src ="http://www.cnitblog.com/gyn/aggbug/35226.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-10-23 16:37 <a href="http://www.cnitblog.com/gyn/archive/2007/10/23/35226.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>分别用vbs、perl、python来操作文本</title><link>http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Wed, 15 Nov 2006 03:29:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/19177.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/19177.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/19177.html</trackback:ping><description><![CDATA[都是用来更新一个rm视频参数文件的。最早学asp的时候顺便用vbs写了一个，后来学perl就又写了一个，现在学python，凑活也写一个。基本上算是我的一个入门程序。用途就是用简单正则表达式找到时间段，再将它更新为当天日期，更新后的数据存在temp.txt中。perl最短，python比较干净。<br />vbs：<br />dim regOR<br />set regOR=new regexp<br />regOR.ignorecase=True<br />regOR.global=True<br />regOR.pattern="^([0-9]{1})$"<br />dim mytime<br />mytime=date<br />yy=year(mytime)<br />mm=month(mytime)<br />dd=day(mytime)<br />if regOR.test(mm) then<br />    mm=regOR.replace(mm,"0$1")<br />end if<br />if regOR.test(dd) then<br />    dd=regOR.replace(dd,"0$1")<br />end if<br />mytime=yy &amp; "-" &amp; mm &amp; "-" &amp; dd<br />dest=yy &amp; mm &amp; dd<br />pattern1="(.*[^0-9])([0-9]{4}\-[0-9]{2}\-[0-9]{2})([^0-9].*)"<br />pattern2="(.*[^0-9])([0-9]{8})([^0-9].*)"<br />regOR.pattern=pattern1<br />dim tempstring<br />Set fso = CreateObject("Scripting.FileSystemObject")<br />Set f = fso.CreateTextFile("d:\shixian\temp.txt", True)<br />Set fr = fso.GetFile("d:\shixian\shixian.rpjf")<br />Set ts = fr.OpenAsTextStream(1,-2)<br />do until ts.AtEndOfStream<br />    tempstring=ts.readline<br />    if regOR.test(tempstring) then<br />        tempstring=regOR.replace(tempstring,"$1" &amp; mytime &amp; "$3")<br />    end if<br />    regOR.pattern=pattern2<br />    if regOR.test(tempstring) then<br />        tempstring=regOR.replace(tempstring,"$1" &amp; dest &amp; "$3")<br />    end if<br />    regOR.pattern=pattern1<br />    f.writeline(tempstring)<br />loop<br />ts.close<br />f.close<br />set regOR=nothing<br />set ts=nothing<br />set fr=nothing<br />set f=nothing<br />set fso=nothing<br /><br />perl：<br />open(RPJF,"&lt;c:/code/10161.rpjf") or die("can not open because of $!\n");<br />open(TEMP,"&gt;c:/code/temp.txt") or die("can not open because of $!\n");<br />($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();<br />$mday = sprintf("%.2d",$mday);<br />$mon = sprintf("%.2d",$mon+1);<br />$year += 1900;<br />$timerF = "$year"."$mon"."$mday";<br />$timerS = "$year"."-"."$mon"."-"."$mday";<br />while(&lt;RPJF&gt;){<br />    chomp();<br />    $_=~s/(.*)([0-9]{8})(.*)/$1$timerF$3/;<br />    $_=~s/(.*)([0-9]{4}\-[0-9]{2}\-[0-9]{2})(.*)/$1$timerS$3/;<br />    print TEMP "$_\n";<br />}<br />close(RPJF);<br />close(TEMP);<br /><br />python：<br />import re<br />import time<br /><br />pattern_name = re.compile(r'[0-9]{8}')<br />pattern_info = re.compile(r'[0-9]{4}\-[0-9]{2}\-[0-9]{2}')<br />(year, month, day) = time.localtime()[:3]<br />name = '%d%.2d%.2d' % (year, month, day)<br />info = '%d-%.2d-%.2d' % (year, month, day)<br /><br />try:<br />    file_rpjf = open('shixian.rpjf', 'r')<br />    file_temp = open('temp.txt', 'w')<br />    try:<br />        file_data = file_rpjf.readline()<br />        while file_data != '':<br />            file_data = pattern_name.sub(name, file_data)<br />            file_data = pattern_info.sub(info, file_data)<br />            file_temp.write(file_data)<br />            file_data = file_rpjf.readline()<br />    finally:<br />        file_rpjf.close()<br />        file_temp.close() <br />except IOError:<br />    pass<br /><img src ="http://www.cnitblog.com/gyn/aggbug/19177.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-11-15 11:29 <a href="http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>看看python吧</title><link>http://www.cnitblog.com/gyn/archive/2006/10/28/18544.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 28 Oct 2006 06:36:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/10/28/18544.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/18544.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/10/28/18544.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/18544.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/18544.html</trackback:ping><description><![CDATA[近年来python发展迅猛，无论在gui还是在web开发上渐渐显示出强大的实力。而perl却由于初期错误的开发方向，导致迟迟无法发布perl6。相比与python的蒸蒸日上、优秀的开发平台、活跃的社区，perl给人的感觉就是江河日下。单说wxwidget，perl版的连英文文档都少的可怜，而python早就有boa这样优秀的平台了；而在perl原先引以为傲的web开发领域，现在的perl又有多少发言权，所谓的成功案例永远都是亚马逊在线。难以驾驭大型项目更是perl的顽疾，越来越多的perler转向了python。虽然还是可以用缓慢而迟钝的pugs体验也许永远不会到来的perl6，但是还是会感到一些失落。<br /><img src ="http://www.cnitblog.com/gyn/aggbug/18544.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-10-28 14:36 <a href="http://www.cnitblog.com/gyn/archive/2006/10/28/18544.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>