锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲av永久无码精品秋霞电影影院,午夜在线亚洲男人午在线,亚洲精品无码久久千人斩http://m.tkk7.com/zellux/category/21872.html榪瘋尗鐨勫ぇ浜岋細璇諱功 鎬濊?絳夊緟zh-cnTue, 13 May 2008 00:38:47 GMTTue, 13 May 2008 00:38:47 GMT60灝哣IM Calender涓殑鏃ヨ鍙戝竷鍒癰logger.com鐨勮剼鏈?/title><link>http://m.tkk7.com/zellux/archive/2008/05/12/200093.html</link><dc:creator>ZelluX</dc:creator><author>ZelluX</author><pubDate>Mon, 12 May 2008 14:04:00 GMT</pubDate><guid>http://m.tkk7.com/zellux/archive/2008/05/12/200093.html</guid><wfw:comment>http://m.tkk7.com/zellux/comments/200093.html</wfw:comment><comments>http://m.tkk7.com/zellux/archive/2008/05/12/200093.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/zellux/comments/commentRss/200093.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/zellux/services/trackbacks/200093.html</trackback:ping><description><![CDATA[VIM Calender鏄釜寰堝ソ鐢ㄧ殑鍐欐棩璁扮殑鎻掍歡(<a >http://www.vim.org/scripts/script.php?script_id=52</a>)<br /><br />姘存湪涓婄殑rmrf鍐欎簡涓涓悓姝IM Calender鍜孏oogle Calender鐨勮剼鏈?<a >http://code.google.com/p/diaryvgc/downloads/list</a>)<br /><br />鎯沖埌blogger.com鏀寔閫氳繃鍙戦侀偖浠跺彂甯冩棩蹇楋紝浜庢槸鎴戜篃鍐欎簡涓妸VIM Calender涓殑鏃ヨ鍙戝竷鍒癰logger.com鐨勮剼鏈?br /><br />榪欎釜鑴氭湰鎶婂彂甯冩儏鍐佃褰曞湪diary/poster.log涓紝浠ュ悗姣忔鎵ц鍙細鍙戝竷鏈鏂扮殑鏃ュ織錛屽悓鏃惰冭檻鍒板綋澶╃殑鏃ヨ鍙兘浼氳淇敼錛坆logger.com浼間箮涓嶆敮鎸侀氳繃email淇敼鏃ュ織錛夛紝鎵浠ュ綋澶╃殑鏃ヨ涓嶄細琚彂甯冦?br /><br />浣跨敤鐨勬椂鍊欎慨鏀瑰紑澶村嚑琛岀殑閰嶇疆淇℃伅鍗沖彲<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">聽A聽script聽for聽posting聽diaries聽created聽by聽VIM聽Calender聽to聽blogger.com</span><span style="COLOR: #008000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />#</span><span style="COLOR: #008000">聽Author:聽Wang聽Yuanxuan聽<zellux@gmail.com></span><span style="COLOR: #008000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">聽smtplib,聽os,聽re,聽datetime<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000">聽email.mime.text聽</span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">聽MIMEText<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />fromaddr聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000"><a href="mailto:xxxxx@fudan.edu.cn'">xxxxx</a></span><span style="COLOR: #800000">@fudan.edu.cn</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />toaddr聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000"><a href="mailto:xxxx.xxxx@blogger.com'">xxxx</a></span><span style="COLOR: #800000">.xxxx@blogger.com</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />smtpserver聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mail.fudan.edu.cn</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />diarydir聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">/home/user_name/diary</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />username聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'xxxxxx'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />password聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'xxxxxx'</span><span style="COLOR: #800000"></span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />logpath聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽diarydir聽</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">/poster.log</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000">聽PostMail(title,聽content):<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽msg聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽MIMEText(content聽</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\r\n#end\r\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽msg[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Subject</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽title<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽msg[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">From</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽fromaddr<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽msg[</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">To</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽toaddr<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽server聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽smtplib.SMTP(smtpserver)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽server.login(username,聽password)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">聽server.set_debuglevel(1)</span><span style="COLOR: #008000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">聽聽聽聽server.sendmail(fromaddr,聽[toaddr],聽msg.as_string())<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽server.quit()<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">聽Load聽log聽file.聽Create聽a聽new聽one聽if聽not聽exist.</span><span style="COLOR: #008000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">posted聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽[]<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">聽os.path.isfile(logpath):<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽temp聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽open(logpath,聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">r</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽posted聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽[line[:</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]聽</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">聽line聽</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">聽temp.readlines()]<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽log聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽open(logpath,聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">a</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">A聽new聽poster聽log聽has聽been聽created聽at聽</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">聽logpath<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽log聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽open(logpath,聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />pattern聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽r</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">(\d{4})/(\d{1,2})/(\d{1,2}).cal$</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />scanner聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽re.compile(pattern)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">聽(top,聽dirname,聽filenames)聽</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">聽os.walk(diarydir):<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">聽filename聽</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">聽filenames:<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽fullpath聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽os.path.join(top,聽filename)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">聽scanner.search(fullpath):<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽(year,聽month,聽day)聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽scanner.search(fullpath).groups()<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽filedate聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽datetime.date(int(year),聽int(month),聽int(day))<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽title聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽filedate.isoformat()<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">聽filedate聽</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">聽datetime.date.today():<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">聽fullpath聽</span><span style="COLOR: #0000ff">not</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">聽posted:<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽log.write(fullpath聽</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽text聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽open(fullpath).read()<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽PostMail(title,聽text)<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The聽diary聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">聽title聽</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">聽</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">聽has聽been聽posted</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" />log.close()<br /><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /></span></div><img src ="http://m.tkk7.com/zellux/aggbug/200093.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/zellux/" target="_blank">ZelluX</a> 2008-05-12 22:04 <a href="http://m.tkk7.com/zellux/archive/2008/05/12/200093.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>[zz]True closure in Pythonhttp://m.tkk7.com/zellux/archive/2008/02/11/179611.htmlZelluXZelluXMon, 11 Feb 2008 08:18:00 GMThttp://m.tkk7.com/zellux/archive/2008/02/11/179611.htmlhttp://m.tkk7.com/zellux/comments/179611.htmlhttp://m.tkk7.com/zellux/archive/2008/02/11/179611.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/179611.htmlhttp://m.tkk7.com/zellux/services/trackbacks/179611.html 涓轟粈涔堟按鏈ㄤ笂鐨勫笘瀛愭瘡琛屾湯灝鵑兘鏄敤絀烘牸濉厖鐨勶紝姣忔杞澆榪樿鍏堟斁鍒皏im閲岄潰澶勭悊涓涓嬨傘傘?br />
by ilovecpp

璁㏄ython鏀寔true closure鏈夊闅撅紵
鍙渶淇敼11琛屼唬鐮併?/p>

濡傛灉浣犱笉鐭ラ亾浠涔堟槸true closure錛岃繖閲岀畝鍗曡В閲婁竴涓嬨侾ython鏀寔lexicalscope錛?/p>

>>> def add_n(n):
...     def f(m):
...             return n+m
...     return f
>>> add_2 = add_n(2)
>>> add_2(0)
2
>>> add_2(2)
4

f寮曠敤浜嗗灞傚嚱鏁癮dd_n鐨勫眬閮ㄥ彉閲弉銆傛湁瓚g殑鏄紝f寮曠敤n鐨勬椂鍊欙紝add_n宸茬粡緇撴潫錛宯浼間箮涓嶅瓨鍦ㄤ簡銆俧鎵浠ヨ兘姝e父宸ヤ綔錛屾槸鍥犱負鍒涘緩瀹冪殑鏃跺欏氨鎶妌浣滀負f鐨勪笂涓嬫枃(closure)淇濆瓨浜嗕笅鏉ワ紝騫朵笉闅廰dd_n緇撴潫鑰屾秷澶便?br /> 浣嗘槸錛孭ython鐨刲exical scope鍜孲cheme/Smalltalk/Ruby榪樻湁涓鐐瑰尯鍒細涓嶈兘鍦ㄥ唴灞傚嚱鏁頒腑rebind澶栧眰鍑芥暟鐨勫眬閮ㄥ彉閲忋?br /> >>> def f():
...     def g():
...             n=1
...     n=0
...     g()
...     return n
...
>>> f()
0

榪欐槸鍥犱負Python娌℃湁鍙橀噺澹版槑錛?n=1 鑷姩浣縩鎴愪負g鐨勫眬閮ㄥ彉閲忥紝涔熷氨鏃犳硶rebind f涓殑n浜嗐傚彲浠ヨPython鐨刢losure鏄彧璇葷殑銆傚鏋滀綘鍚埌鏈変漢璇?Python涓嶆敮鎸乼rue closure"錛屽氨鏄寚榪欎釜銆傚叾瀹烇紝Python VM鑳藉鏀寔true closure銆傚洜涓猴紝Python鏀寔鍐呭眰鍑芥暟鐪嬭澶栧眰鍑芥暟鐨刵ame rebinding:

>>> def f():
...     def g():
...             yield n
...             yield n
...     x = g()
...     n = 0
...     print x.next()
...     n = 1
...     print x.next()
...
>>> f()
0
1

瀵逛簬Python鐨刢losure瀹炵幇(flat closure)錛?澶栧眰鍑芥暟rebind name"鍜?鍐呭眰鍑芥暟rebind name"鍏跺疄娌℃湁鍖哄埆銆傛垜浠煡閬撶敤global鍏抽敭瀛楀彲浠ebind module scopename銆傚鏋滃鍔犱竴涓被浼肩殑outer鍏抽敭瀛楋紝灝卞彲浠ユ敮鎸乺ebind outer scope name銆傜湡姝g殑闄愬埗鏄疓uido涓嶆効鎰忎負鏀寔true closure澧炲姞鍏抽敭瀛椼?/p>

涔熷彲浠ヤ笉澧炲姞鍏抽敭瀛楋紝鑰屾槸鎶奼lobal n鐨勮涔夋敼涓?濡傛灉outer scope瀹氫箟浜唍錛宺ebind outer scope n錛涘惁鍒檙ebind module scope n"銆傜畝鍗曡搗瑙侊紝鎴戞病鏈変慨鏀筆ython鐨刡uilt-in compiler錛岃屾槸淇敼浜哻ompiler module(鐢≒ython瀹炵幇鐨凱ython compiler)銆備綘鍙渶鎶婁笅闈㈣繖涓猵atch鎵撳埌compiler/symbols.py(Python 2.5.1)灝卞彲浠ヤ綋楠宼rue closure浜嗭細

C:\Python\Lib>diff -u compiler/symbols.py.orig compiler/symbols.py
--- compiler/symbols.py.orig    Thu Aug 17 10:28:56 2006
+++ compiler/symbols.py Mon Feb 11 12:03:01 2008
@@ -21,6 +21,7 @@
         self.params = {}
         self.frees = {}
         self.cells = {}
+        self.outers = {}
         self.children = []
         # nested is true if the class could contain free variables,
         # i.e. if it is nested within another function.
@@ -54,8 +55,10 @@
         if self.params.has_key(name):
             raise SyntaxError, "%s in %s is global and parameter" % \
                   (name, self.name)
-        self.globals[name] = 1
-        self.module.add_def(name)
+        if self.nested:
+            self.outers[name] = 1
+        else:
+            self.globals[name] = 1

     def add_param(self, name):
         name = self.mangle(name)
@@ -90,6 +93,8 @@
         """
         if self.globals.has_key(name):
             return SC_GLOBAL
+        if self.outers.has_key(name):
+            return SC_FREE
         if self.cells.has_key(name):
             return SC_CELL
         if self.defs.has_key(name):
@@ -107,6 +112,7 @@
             return ()
         free = {}
         free.update(self.frees)
+        free.update(self.outers)
         for name in self.uses.keys():
             if not (self.defs.has_key(name) or
                     self.globals.has_key(name)):
@@ -134,6 +140,9 @@
         free.
         """
         self.globals[name] = 1
+        if self.outers.has_key(name):
+            self.module.add_def(name)
+            del self.outers[name]
         if self.frees.has_key(name):
             del self.frees[name]
         for child in self.children:

鍥犱負鎴戜滑娌℃湁淇敼built-in compiler錛屾墍浠ョ▼搴忚鍐欏湪瀛楃涓查噷錛岀敤compiler.compile緙栬瘧錛岀敤exec鎵ц錛?br /> >>> from compiler import compile
>>> s = '''
... def counter():
...     n = 0
...     def inc():
...             global n
...             n += 1
...     def dec():
...             global n
...             n -= 1
...     def get():
...             return n
...     return inc, dec, get
... '''
>>> exec compile(s, '', 'exec')
>>> inc, dec, get = counter()
>>> get()
0
>>> inc()
>>> get()
1
>>> dec()
>>> get()
0

鍚庤

1 鎼炶繖涓笢瑗跨殑緙樿搗鏄疭elfless Python(http://www.voidspace.org.uk/python/weblog/arch_d7_2006_12_16.shtml#e583)銆傚緢鏈夎叮鐨刡ytecode hack錛岀粰涓涓被涓殑鎵鏈夊嚱鏁拌ˉ涓妔elf鍙傛暟銆傛棦鐒禤ythonVM鏀寔true closure錛岃兘涓嶈兘鐢ㄧ被浼肩殑鎵嬫硶璁㏄ython鏀寔true closure鍛紵涓嶈繃寰堝揩灝辨槑鐧借繖涓湪bytecode灞傞潰涓嶅ソ寮勶紝榪樻槸寰椾慨鏀圭紪璇戝櫒銆備笉榪囨敼璧鋒潵榪樼湡鏄嚭涔庢剰鏂欏湴綆鍗曘?/p>

2 Guido鏃╁凡鏄庣‘琛ㄧず涓嶈兘鏀瑰彉global鐨勮涔夛紙鍥犱負浼氬獎鍝嶇幇鏈変唬鐮侊級錛屾墍浠ヨ繖涓彧鏄帺鐜╄屽凡錛屼笉鐢ㄦ寚鏈涙垚涓虹幇瀹炪傚綋鐒朵綘鍙互鍙彂甯僢ytecode錛屽ぇ姒傝繕鑳芥妸鍙嶇紪璇戝櫒鎼炴寕鎺夈?-)
3 鎴戝彲浠ョ悊瑙uido鐨勫喅瀹氥傞櫎闈炰綘涔嬪墠涓鐩村湪鐢⊿cheme錛屽惁鍒欐垜瑙夊緱鍍忎笂闈ounter渚嬪瓙閭g涓緇勫叡浜姸鎬佺殑鍑芥暟榪樻槸鍐欐垚class涓哄ソ錛岃嚦灝戝叡浜姸鎬佹槸浠涔堜竴鐩簡鐒躲侺exical scope澶猧mplicit錛岀敤鍦ㄥ紑澶碼dd_n閭g鍦版柟鎸烘柟渚匡紝鍐嶅鏉傚氨涓嶅ソ浜嗐?/p>

鍙堬細寰堟姳姝?騫曞悗鐨勬晠浜?鎷栦簡榪欎箞涔呫傚啓璧鋒潵鎵嶅彂鐜拌嚜宸辮繕鏄笉鎳俤escriptor銆?br /> 涓嶈繃鎴戣偗瀹氫笉浼氳瀹冪儌灝劇殑銆?/p>

 



ZelluX 2008-02-11 16:18 鍙戣〃璇勮
]]>
鍦≒ython鐨刦or寰幆涓鏁?/title><link>http://m.tkk7.com/zellux/archive/2007/12/25/170396.html</link><dc:creator>ZelluX</dc:creator><author>ZelluX</author><pubDate>Tue, 25 Dec 2007 13:29:00 GMT</pubDate><guid>http://m.tkk7.com/zellux/archive/2007/12/25/170396.html</guid><wfw:comment>http://m.tkk7.com/zellux/comments/170396.html</wfw:comment><comments>http://m.tkk7.com/zellux/archive/2007/12/25/170396.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/zellux/comments/commentRss/170396.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/zellux/services/trackbacks/170396.html</trackback:ping><description><![CDATA[<p>鍒╃敤enumerate<br /> <br /> for i, obj in enumerate(list):<br />     print i, obj</p> <p>Help on class enumerate in module __builtin__:</p> <p>class enumerate(object)<br />  |  enumerate(iterable) -> iterator for index, value of iterable<br />  |<br />  |  Return an enumerate object.  iterable must be an other object that supports<br />  |  iteration.  The enumerate object yields pairs containing a count (from<br />  |  zero) and a value yielded by the iterable argument.  enumerate is useful<br />  |  for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...<br />  |<br />  |  Methods defined here:<br />  |<br />  |  __getattribute__(...)<br />  |      x.__getattribute__('name') <==> x.name<br />  |<br />  |  __iter__(...)<br />  |      x.__iter__() <==> iter(x)<br />  |<br />  |  next(...)<br />  |      x.next() -> the next value, or raise StopIteration<br />  |<br />  |  ----------------------------------------------------------------------<br />  |  Data and other attributes defined here:<br />  |<br />  |  __new__ = <built-in method __new__ of type object at 0xb7f35d20><br />  |      T.__new__(S, ...) -> a new object with type S, a subtype of T<br /> </p> <img src ="http://m.tkk7.com/zellux/aggbug/170396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/zellux/" target="_blank">ZelluX</a> 2007-12-25 21:29 <a href="http://m.tkk7.com/zellux/archive/2007/12/25/170396.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python涓璂ictionary綾誨瀷鐨勬帓搴?/title><link>http://m.tkk7.com/zellux/archive/2007/12/04/165260.html</link><dc:creator>ZelluX</dc:creator><author>ZelluX</author><pubDate>Tue, 04 Dec 2007 10:48:00 GMT</pubDate><guid>http://m.tkk7.com/zellux/archive/2007/12/04/165260.html</guid><wfw:comment>http://m.tkk7.com/zellux/comments/165260.html</wfw:comment><comments>http://m.tkk7.com/zellux/archive/2007/12/04/165260.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/zellux/comments/commentRss/165260.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/zellux/services/trackbacks/165260.html</trackback:ping><description><![CDATA[lambda鐪熸槸鐜嬮亾鍟?br /> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #008000">#</span><span style="color: #008000">!/usr/bin/env python</span><span style="color: #008000"><br /> <img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #000000">d</span><span style="color: #000000">=</span><span style="color: #000000">{</span><span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">:</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #800000">'</span><span style="color: #800000">b</span><span style="color: #800000">'</span><span style="color: #000000">:</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #800000">'</span><span style="color: #800000">c</span><span style="color: #800000">'</span><span style="color: #000000">:</span><span style="color: #000000">4</span><span style="color: #000000">}<br /> <img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000"> sorted(d.items(), key</span><span style="color: #000000">=</span><span style="color: #0000ff">lambda</span><span style="color: #000000"> (k,v): (v,k))</span></div> <br /> <p>Help on built-in function sorted in module __builtin__:</p> <p>sorted(...)<br />     sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list</p> <p> </p> <img src ="http://m.tkk7.com/zellux/aggbug/165260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/zellux/" target="_blank">ZelluX</a> 2007-12-04 18:48 <a href="http://m.tkk7.com/zellux/archive/2007/12/04/165260.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>銆愯漿杞姐慞ython 涓殑鍑芥暟寮忕紪紼?(1)http://m.tkk7.com/zellux/archive/2007/11/23/162699.htmlZelluXZelluXFri, 23 Nov 2007 13:15:00 GMThttp://m.tkk7.com/zellux/archive/2007/11/23/162699.htmlhttp://m.tkk7.com/zellux/comments/162699.htmlhttp://m.tkk7.com/zellux/archive/2007/11/23/162699.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/162699.htmlhttp://m.tkk7.com/zellux/services/trackbacks/162699.html闃呰鍏ㄦ枃

ZelluX 2007-11-23 21:15 鍙戣〃璇勮
]]>
texttable - module for creating simple ASCII tableshttp://m.tkk7.com/zellux/archive/2007/09/10/144122.htmlZelluXZelluXMon, 10 Sep 2007 15:40:00 GMThttp://m.tkk7.com/zellux/archive/2007/09/10/144122.htmlhttp://m.tkk7.com/zellux/comments/144122.htmlhttp://m.tkk7.com/zellux/archive/2007/09/10/144122.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/144122.htmlhttp://m.tkk7.com/zellux/services/trackbacks/144122.html http://jefke.free.fr/soft/texttable/
dl:  http://jefke.free.fr/soft/texttable/texttable.py
NAME
    texttable - module for creating simple ASCII tables
FILE
    /usr/lib/python2.3/site-packages/texttable.py
DESCRIPTION
    Example:
        table = Texttable()
        table.header(["Name", "Age"])
        table.set_cols_align(["l", "r"])
        table.add_row(["Xavier\nHuon", 32])
        table.add_row(["Baptiste\nClement", 1])
        table.draw()
    Result:
        +----------+-----+
        |   Name   | Age |
        +==========+=====+
        | Xavier   |  32 |
        | Huon     |     |
        +----------+-----+
        | Baptiste |   1 |
        | Clement  |     |
        +----------+-----+
CLASSES
    exceptions.Exception
        ArraySizeError
    Texttable
    class ArraySizeError(exceptions.Exception)
     |  Exception raised when specified rows don't fit the required size
     |
     |  Methods defined here:
     |
     |  __init__(self, msg)
     |
     |  __str__(self)
     |
     |  ----------------------------------------------------------------------
     |  Methods inherited from exceptions.Exception:
     |
     |  __getitem__(...)
    class Texttable
     |  Methods defined here:
     |
     |  __init__(self, max_width=80)
     |      Constructor
     |      - max_width is an integer, specifying the maximum width of the t
able
     |      - if set to 0, size is unlimited, therefore cells won't be wrapp
ed
     |
     |  add_row(self, array)
     |      Add a row in the rows stack
     |
     |      Cells can contain newlines.
     |
     |  draw(self)
     |      Draw the table
     |
     |  header(self, array)
     |      Specify the header of the table
     |
     |  reset(self)
     |      Reset the instance:
     |      - reset rows and header
     |
     |  set_chars(self, array)
     |      Set the characters used to draw lines between rows and
     |      columns.
     |
     |      The array should contain 4 fields:
     |
     |          [horizontal, vertical, corner, header]
     |
     |      Default is set to:
     |
     |          ['-', '|', '+', '=']
     |
     |  set_cols_align(self, array)
     |      Set the desired columns alignment
     |
     |      The elements of the array should be either "l", "c" or "r"
     |       - "l": column flushed left
     |       - "c": column centered
     |       - "r": column flushed right
     |
     |  set_cols_width(self, array)
     |      Set the desired columns width
     |
     |      The elements of the array should be integers, specifying the
     |      width of each column. For example:
     |
     |           [10, 20, 5]
     |
     |  set_deco(self, deco)
     |      Set the table decoration. 'deco' can be a combinaison of:
     |
     |      Texttable.BORDER: Border around the table
     |      Texttable.HEADER: Horizontal line below the header
     |      Texttable.HLINES: Horizontal lines between rows
     |      Texttable.VLINES: Vertical lines between columns
     |
     |      Example:
     |
     |          Texttable.BORDER | Texttable.HEADER
     |
     |      All of them are enabled by default.
     |
     |  --------------------------------------------------------------------
--
     |  Data and other attributes defined here:
     |
     |  BORDER = 1
     |
     |  HEADER = 4
     |
     |  HLINES = 8
     |
     |  VLINES = 16
DATA
    __all__ = ['Texttable', 'ArraySizeError']
    __author__ = 'Gerome Fournier <jefke(at)free.fr>'
    __credits__ = 'Jeff Kowalczyk:\n    - textwrap improved import\n    - ..
.
    __license__ = 'GPL'
    __revision__ = '$Id: texttable.py,v 1.3 2003/10/05 13:53:39 jef Exp je..
.
    __version__ = '0.3'
VERSION
    0.3
AUTHOR
    Gerome Fournier <jefke(at)free.fr>
CREDITS
    Jeff Kowalczyk:
        - textwrap improved import
        - comment concerning header output


ZelluX 2007-09-10 23:40 鍙戣〃璇勮
]]>
鐕曟洣鐗堝弸淇℃伅緇熻鑴氭湰http://m.tkk7.com/zellux/archive/2007/09/10/143943.htmlZelluXZelluXMon, 10 Sep 2007 04:27:00 GMThttp://m.tkk7.com/zellux/archive/2007/09/10/143943.htmlhttp://m.tkk7.com/zellux/comments/143943.htmlhttp://m.tkk7.com/zellux/archive/2007/09/10/143943.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/143943.htmlhttp://m.tkk7.com/zellux/services/trackbacks/143943.html
import urllib, urllib2, cookielib

class MyConnector:
    
def __init__(self):
        
pass
    
    
def login(self, url):
        cookie 
= cookielib.CookieJar()
        opener 
= urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
        urllib2.install_opener(opener)
        str 
= urllib.urlencode({'id''guest''passwd'''})
        self.sock 
= urllib2.urlopen(url, str)
    
    
def getHTML(self, url):
        self.sock 
= urllib2.urlopen(url)
        
return self.sock.read()

yanxiparser.py
from sgmllib import SGMLParser
import re

class YanxiURLParser(SGMLParser):
    
def reset(self):
        self.result 
= []
        SGMLParser.reset(self)
    
    
def start_a(self, attrs):
        
for (k, v) in attrs:
            
if (k == 'href' and (v.find('bbsanc'>= 0)):
                self.result.append(v)
                
class YanxiHTMLParser:
    
def parse(self, html):
        uid 
= ufrom = ubirth = ufav = ''
        
        html 
= html.replace(r'&nbsp;'' ')
        html 
= html.replace(r'<br />''')
        
        pattern 
= '\xbe\xcd\xca\xc7(.*)\xc0\xb2'
        matchObject 
= re.search(pattern, html)
        uid 
= matchObject.group(1)
        uid 
= uid.strip()
        
        pattern 
= '\xc0\xb4\xd7\xd4(.*)\xa3(\xac|xa1)'
        matchObject 
= re.search(pattern, html)
        ufrom 
= matchObject.group(1)
        ufrom 
= ufrom.strip()
        
        pattern 
= '\xcf\xb2\xbb\xb6(.*)\n'
        matchObject 
= re.search(pattern, html)
        ufav 
= matchObject.group(1)
        ufav 
= ufav.strip()
        
        pattern 
= '\n(.*)\xca\xc7\xce\xd2\xb5\xc4\xc9\xfa\xc8\xd5'
        matchObject 
= re.search(pattern, html)
        ubirth 
= matchObject.group(1)
        ubirth 
= ubirth.strip()
        
return {"id" : uid, "from" : ufrom, "birth" : ubirth, "fav" : ufav}

runner.py
from connector import MyConnector
from yanxiparser import *

rootURL 
= 'http://yanxibbs.cn'
loginURL 
= 'http://yanxibbs.cn/bbslogin.php'
url1 
= 'http://yanxibbs.cn/cgi-bin/bbs/bbs0an?path=%2Fgroups%2FGROUP%5F3%2F06SS%2Fbyxx%2Fbjcy'
url2 
= 'http://yanxibbs.cn/cgi-bin/bbs/bbs0an?path=%2Fgroups%2FGROUP%5F3%2F06SS%2Fbyxx%2Fbjyr'

conn 
= MyConnector()
conn.login(loginURL)

def printInfo(url):
    html 
= conn.getHTML(url)
    urlParser 
= YanxiURLParser()
    htmlParser 
= YanxiHTMLParser()
    urlParser.feed(html)
    
    
for targetURL in urlParser.result:
        html 
= conn.getHTML(rootURL + targetURL)
        info 
= htmlParser.parse(html)
        
print "%(id)s\t%(from)s\t%(birth)s\t%(fav)s" % info
    
printInfo(url1)
printInfo(url2)


ZelluX 2007-09-10 12:27 鍙戣〃璇勮
]]>
Python 瀛︿範絎旇 - XML 瑙f瀽http://m.tkk7.com/zellux/archive/2007/08/18/137832.htmlZelluXZelluXSat, 18 Aug 2007 08:33:00 GMThttp://m.tkk7.com/zellux/archive/2007/08/18/137832.htmlhttp://m.tkk7.com/zellux/comments/137832.htmlhttp://m.tkk7.com/zellux/archive/2007/08/18/137832.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/137832.htmlhttp://m.tkk7.com/zellux/services/trackbacks/137832.htmlminidom.parse("binary.xml")寰楀埌鐨勫璞℃槸binary.xml鐨勬暣媯礵om鏍戯紝瀹冪殑絎竴涓粨鐐瑰寘鍚簡DOCTYPE鐨勭浉鍏充俊鎭紝瀵逛簬瀹冪殑瀛楄妭鐐圭殑firstNode錛岃矊浼間竴鑸兘鏄┖鐨勩?br>
2. unicode 鐩稿叧
string.encode()
sys.getdefaultencoding()
鎸囧畾.py鏂囦歡緙栫爜鐨勬柟娉曪細
鍦ㄦ瘡涓枃浠跺紑澶村姞鍏ョ紪鐮佸0鏄?br># -*- coding: UTF-8 -*-

3. python鐩綍鐨刲ib/site-packages/sitecustomize.py鏄竴涓壒孌婄殑鑴氭湰錛孭ython浼氬湪鍚姩鐨勬椂鍊欏鍏ュ畠銆?br>
4. 鎼滅儲鍏冪礌錛?br>getElementByTagName()
榪斿洖鐨勬槸涓涓猯ist

5. 鍏冪礌灞炴?br>attributes 鏄竴涓獂ml.dom.minidom.NameNodeMap瀹炰緥錛屽父鐢ㄧ殑鏂規硶濡俴eys() values()錛屽悓鏃朵篃鏈塤_getitem__鏂規硶錛岀被浼間簬dictionary



ZelluX 2007-08-18 16:33 鍙戣〃璇勮
]]>
Python 瀛︿範 - File and Directoryhttp://m.tkk7.com/zellux/archive/2007/08/15/137066.htmlZelluXZelluXWed, 15 Aug 2007 14:34:00 GMThttp://m.tkk7.com/zellux/archive/2007/08/15/137066.htmlhttp://m.tkk7.com/zellux/comments/137066.htmlhttp://m.tkk7.com/zellux/archive/2007/08/15/137066.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/137066.htmlhttp://m.tkk7.com/zellux/services/trackbacks/137066.html1. 鍑犱釜甯哥敤瀵硅薄錛?br>os.system  鎵ц鍛戒護
sys.stdin  sys.stdout
os.path.getsize   鑾峰緱鏂囦歡澶у皬
os.path.isdir
os.mkdir
os.listdir

2. walk()鍑芥暟
寰堝ソ鐢ㄧ殑涓涓嚱鏁?br>os.path.walk(rootdir, f, arg)
rootdir鏄璁塊棶鐨勭洰褰曟爲鐨勬牴錛宖鏄敤鎴峰畾涔夌殑鍑芥暟錛宎rg鏄皟鐢╢鏃剁敤鐨勪竴涓弬鏁般?br>瀵逛簬姣忎竴涓?walk"榪囩▼涓亣鍒扮殑鐩綍directory錛岃璇ョ洰褰曚笅鐨勬枃浠跺垪琛ㄤ負filelist錛寃alk鍑芥暟浼氳皟鐢?br>f(arg, directory, filelist)



ZelluX 2007-08-15 22:34 鍙戣〃璇勮
]]>
Python 瀛︿範絎旇 (5)http://m.tkk7.com/zellux/archive/2007/08/15/136540.htmlZelluXZelluXTue, 14 Aug 2007 16:07:00 GMThttp://m.tkk7.com/zellux/archive/2007/08/15/136540.htmlhttp://m.tkk7.com/zellux/comments/136540.htmlhttp://m.tkk7.com/zellux/archive/2007/08/15/136540.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/136540.htmlhttp://m.tkk7.com/zellux/services/trackbacks/136540.html1. 浜ゆ崲x鍜寉鐨勫?nbsp; [x, y] = [y, x]

2. zip()鏂規硶鎶婂嚑涓猯ists鐨勭i涓厓绱犲悎鎴愪竴涓猼uple錛屾斁鍦ㄤ竴涓柊鐨刲ist涓?br>zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

鍑芥暟寮忕紪紼嬬浉鍏?/strong>

1.  Mapping
map() 鏂規硶瀵瑰簭鍒椾腑鐨勬瘡涓厓绱犺皟鐢ㄦ煇涓嚱鏁幫紝榪斿洖鏂扮敓鎴愮殑緇撴灉搴忓垪銆?br>鏈夌偣綾諱技浜嶳uby鐨凙rray.each do | element |
>>> z = map(len, ["abc", "clounds", "rain"])
>>> z
[3, 6, 4]

2. Filtering
榪囨護鎺夋弧瓚蟲潯浠剁殑鍏冪礌錛岀被浼間笌Array.reject do | element |
>>> x = [5,12,-2,13]
>>> y = filter(lambda z: z > 0, x)
>>> y
[5, 12, 13]

3. List Comprehension
鏉ヤ釜澶嶆潅鐨勪緥瀛?br>>>> y
[[0, 2, 22], [1, 5, 12], [2, 3, 33]]
>>> [a for b in y for a in b[1:]]
[2, 22, 5, 12, 3, 33]

4. Reduction
鍏堢湅紺轟緥
>>> x = reduce(lambda x,y: x+y, range(5))
>>> x
10
鎵ц鐨勯『搴忔槸錛氶鍏堣皟鐢ㄥ嚱鏁板鐞唕ange(5)鍓嶄袱涓鹼紝0 + 1 = 1錛岀劧鍚庤繑鍥炵殑鍊?浣滀負x錛岀戶緇笌range(5)鐨勭涓変釜鍊?2錛屼綔涓簓)銆?br>鏈鍚庣殑緇撴灉鏄?+1+2+3+4鐨勫?br>榪欎釜甯哥敤鏉ヤ唬鏇縡or寰幆



ZelluX 2007-08-15 00:07 鍙戣〃璇勮
]]>
鐢≒ython鍐欎簡涓猆RP鏌ュ垎紼嬪簭http://m.tkk7.com/zellux/archive/2007/08/03/134316.htmlZelluXZelluXFri, 03 Aug 2007 11:50:00 GMThttp://m.tkk7.com/zellux/archive/2007/08/03/134316.htmlhttp://m.tkk7.com/zellux/comments/134316.htmlhttp://m.tkk7.com/zellux/archive/2007/08/03/134316.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/134316.htmlhttp://m.tkk7.com/zellux/services/trackbacks/134316.html鍐欏緱涓嶆庝箞鏍鳳紝鍍廢RPParser閲屽鐞嗘爣絳劇殑鏃跺欑洿鎺ヨ緭鍑轟簡錛屽緢涓嶈鑼冿紝涓嶈繃鎳掑緱鏀逛簡
urpparser.py:
from sgmllib import SGMLParser

class URPParser(SGMLParser):
    
def reset(self):
        self.tdOpen 
= 0
        self.colCount 
= -1
        self.firstRow 
= 1
        self.pieces 
= []
        SGMLParser.reset(self)

    
def start_td(self, attrs):
        
"""
            When encountered with tag td, check whether there's
            an align property in the tag, which will distinguish
            score table from others.
        
"""
            
        
for (k, v) in attrs:
            
if (k == "align"):
                self.tdOpen 
= 1
                
break

    
def end_td(self):
        self.tdOpen 
= 0

    
def handle_data(self, text):
        
if (self.tdOpen > 0):
            
if (len(text.strip()) > 0):
                self.colCount 
+= 1
                
if (self.colCount > 6):
                    self.colCount 
= 0
                    self.firstRow 
= 0
                    
print
                
if (self.firstRow):
                    
return
                
if (self.colCount == 2):
                    
print "\t",
                
else:
                    
print text.strip(),"\t",

gpa.sh:
#!/usr/bin/python
import urllib, cookielib, urllib2

loginURL 
= "http://fdis.fudan.edu.cn:58080/amserver/UI/Login?" +\
           
"goto=http%3A%2F%2Fwww.urp.fudan.edu.cn%3A84%2Feps" +\
           
"tar%2Fapp%2Ffudan%2FframeSub.jsp%3FaffairNO%3D035067"
scoreURL 
= "http://www.urp.fudan.edu.cn:84/epstar/app/fudan/S" +\
           
"coreManger/ScoreViewer/Student/Course.jsp"
logoutURL 
= "http://www.urp.fudan.edu.cn/logout.jsp"

cookie 
= cookielib.CookieJar()
opener 
= urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)
str 
= urllib.urlencode({'Login.Token1''06301000000''Login.Token2'"yourpassword"})
sock1 
= urllib2.urlopen(loginURL, str)
loginHTML 
= sock1.read()
sock1.close()

sock2 
= urllib2.urlopen(scoreURL)
scoreHTML 
= sock2.read()
sock2.close()

sock3 
= urllib2.urlopen(logoutURL)
sock3.close()

from urpparser import URPParser
parser 
= URPParser()
parser.feed(scoreHTML)
print





ZelluX 2007-08-03 19:50 鍙戣〃璇勮
]]>
Python 瀛︿範絎旇 (4)http://m.tkk7.com/zellux/archive/2007/08/02/133886.htmlZelluXZelluXWed, 01 Aug 2007 16:30:00 GMThttp://m.tkk7.com/zellux/archive/2007/08/02/133886.htmlhttp://m.tkk7.com/zellux/comments/133886.htmlhttp://m.tkk7.com/zellux/archive/2007/08/02/133886.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/133886.htmlhttp://m.tkk7.com/zellux/services/trackbacks/133886.html>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
>>> "%(pwd)s" % params
'secret'
榪欎釜涓滆タ鐨勭敤澶勫湪浜庡拰locals鐨勬惌閰嶄嬌鐢紝姣斿鏍蜂緥紼嬪簭涓?br>
def handle_comment(self, text):
    self.pieces.append(
"<!--%(text)s-->" % locals()) 

灝辮鍙栦簡text鍙橀噺鐨勫唴瀹廣?br>涓嶈繃榪欐牱鍜岀洿鎺ョ敤text鍙橀噺鏈変粈涔堝尯鍒憿錛熻矊浼?<!--%s-->" % text涔熷彲浠ュ晩
姘存湪涓婇棶浜嗕竴涓嬶紝寰楀埌鐨勭瓟妗堟槸
鍙戜俊浜? Essien5 (瀹濊礉鏅秪), 淇″尯: Python
鏍? 棰? Re: 鍏充簬locals()鐨勭敤澶?br>鍙戜俊绔? 姘存湪紺懼尯 (Thu Aug  2 11:16:37 2007), 杞俊

濂藉灝辨槸澶氫釜鍙橀噺鏄唬鐮佸緢濂界淮鎶わ紝涓涓瀵瑰簲
'%s%s.......%s'%(a,b,c,d,....,z)
'%(a)s%(b)s......%(z)s'%locals()
絎竴縐嶅啓娉曞墠闈㈢殑%s鍜屽悗闈㈢殑鍙橀噺寰堥毦瀵瑰簲璧鋒潵錛宐ug鐨勬簮娉?br>鍚庝竴涓氨闈炲父鐩磋浜?br>鑰屼笖瑕佸線涓棿鍐嶉殢渚挎彃鍙橀噺涔熸柟渚?/span>

2. 鑷繁鐨勭被緇ф壙浜哠GMLParser鍚庯紝闇瑕佸鐗規畩鏍囪澶勭悊錛屽彲浠ヤ互start_鎴杁o_寮濮嬪懡鍚嶇浉鍏沖嚱鏁般?br>鍙互榪欐牱鍋氱殑鍘熷洜鍦ㄤ簬python鐨勮嚜閱掓満鍒?introspection)

    def finish_starttag(self, tag, attrs):
        
try:
            method 
= getattr(self, 'start_' + tag)
        
except AttributeError:
            
try:
                method 
= getattr(self, 'do_' + tag)
            
except AttributeError:
                self.unknown_starttag(tag, attrs)
                
return -1
            
else:
                self.handle_starttag(tag, method, attrs)
                
return 0
        
else:
            self.stack.append(tag)
            self.handle_starttag(tag, method, attrs)
            
return 1
紼嬪簭棣栧厛灝濊瘯鑾峰緱start_tagname鐨勬柟娉曪紝濡傛灉澶辮觸鍒欑戶緇皾璇曡幏寰梔o_tagname錛屽鏋滀粛鐒朵笉鑳芥壘鍒幫紝鍒欒皟鐢╱nknown_starttag鏂規硶銆?br>鎰熻榪欏拰Java鐨勫弽灝勬満鍒跺緢鐩鎬技錛屼緥濡侸avabean涓殑getter setter鏂規硶錛屼篃鏄氳繃鐗規畩鍛藉悕鐨勫艦寮忚鍏朵粬瀵硅薄浜嗚В鑷繁鐨勩?br>
3. import 璇彞鍙互鍐欏湪浠諱綍鍦版柟銆?br>

ZelluX 2007-08-02 00:30 鍙戣〃璇勮
]]>
Python 瀛︿範絎旇 (3)http://m.tkk7.com/zellux/archive/2007/07/10/129284.htmlZelluXZelluXTue, 10 Jul 2007 03:00:00 GMThttp://m.tkk7.com/zellux/archive/2007/07/10/129284.htmlhttp://m.tkk7.com/zellux/comments/129284.htmlhttp://m.tkk7.com/zellux/archive/2007/07/10/129284.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/129284.htmlhttp://m.tkk7.com/zellux/services/trackbacks/129284.html鐢變簬Python鐨勫垽鏂鍙ヨ繑鍥炲肩殑鐗規畩鎬э紝and-or璇彞鍙互杈懼埌綾諱技涓夊厓榪愮畻絎︾殑鏁堟灉銆?br>bool ? a : b
鍙互鍐欐垚
bool and a or b

2.
print None 涓嶄細杈撳嚭浠諱綍淇℃伅
闇瑕佹樉紺篘one錛岃浣跨敤print str(None)


ZelluX 2007-07-10 11:00 鍙戣〃璇勮
]]>
[zz]Ruby涓ゆ湰涔﹁鍚庢劅http://m.tkk7.com/zellux/archive/2007/06/16/124684.htmlZelluXZelluXSat, 16 Jun 2007 11:54:00 GMThttp://m.tkk7.com/zellux/archive/2007/06/16/124684.htmlhttp://m.tkk7.com/zellux/comments/124684.htmlhttp://m.tkk7.com/zellux/archive/2007/06/16/124684.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/124684.htmlhttp://m.tkk7.com/zellux/services/trackbacks/124684.htmlhttp://www.aurora-x.net/blog/oasis/?p=92
by opengl@rygh

鐜板湪甯傞潰涓婄郴緇熺殑錛岀敱嫻呭強娣辯殑璁睷uby鐨勪功鍑ゆ瘺楹熻錛岃繖鏈槸鍒嗛噺鏈閲嶇殑涓鏈傚師涔︾浜岀増鍑虹増鍒扮幇鍦ㄤ篃榪囧幓涓ゅ勾浜嗭紝鍥藉唴鍒氬垰鍦ㄨ繖涓湀鐢卞崥鏂囧紩榪涚數宸ュ彂琛屼簡璇戞湰銆?br>
800+欏電殑澶ч儴澶達紝涓鍗婃槸鏍稿績搴撳拰鏍囧噯搴撶殑鍙傝冿紝鍙﹀涓鍗婂垎涓轟笁涓儴鍒嗏斺斿熀紜銆佺幆澧冦侀珮綰с傝繖鍏朵腑鎴戣寰楁瘮杈冩湁浠峰肩殑閮ㄥ垎鍦ㄤ簬“楂樼駭榪欎竴鍧?#8221;鏈夊姪浜庤浣犱粠涓涓洿楂樼殑灞傛鏉ョ悊瑙e拰鎺屾彙榪欓棬璇█錛岃繖涓閮ㄥ垎涔熸槸闇瑕佺粡甯稿弬鑰冪殑閮ㄥ垎銆?/p>

灝卞浗鍐呯洰鍓嶅紩榪涚殑鍞竴涓鏈畻鏄Ruby鐨勪功錛岃浠庤繖鏈功寮濮嬪涔燫uby浼拌浼氬悡璺戜笉灝戞綔鍦ㄧ敤鎴楓備綔鑰呮槸澶х墰娌¢敊錛屼笉榪囨暀瀛﹀垯鏄彟涓鍥炰簨浜嗭紙澶у閮藉簲鏈変綋浼氾紝鏈涓婅鐨勬椂鍊欒璁茬殑鏈濂界殑鑰佸笀閫氬父閮戒笉鏄鏈渶鐗涚殑鑰佸笀錛夈傜己鐐規湁涓ゅ寰堟槑鏄撅細

絎竴鏄嚜欏跺悜涓嬶紝瀹冪殑欏哄簭鏄繖鏍風殑錛?/p>

瀵硅薄鍜岀被–>瀹瑰櫒銆侀泦鍚?#8211;>鏍囧噯綾誨瀷–>琛ㄨ揪寮?#8211;>寮傚父鍜屾ā鍧?#8211;>鍩烘湰杈撳叆杈撳嚭

涓涓粠涓婇檷鍒頒綆鍙堥櫋鐒朵笂鍗囩殑榪囩▼銆備綔鑰呰嚜宸變篃璇村湪絎竴鐗堥噷榪欐牱鐨勬晥鏋滃茍涓嶅ソ錛屽洜姝ゅ湪絎簩鐗堥噷鐗規剰澧炲姞浜嗕竴涓粙緇嶆х珷鑺傦紝綺楃暐鐨勫厛鎶婃墍鏈変笢瑗垮垪緇欒鑰呯湅涓閬嶃傝櫧鐒惰搗鍒頒簺浣滅敤錛屼絾鏄疄闄呮晥鏋滄垜鎯沖浜庡垵瀛﹁呮潵璇翠粛鐒朵笉浼氬お濂姐侰++/Java鐨勪功鎴戦兘璇昏繃涓嶅皯錛屼篃娌¤鍝釜鏄竴涓婃潵灝辨妸Class/Object榪欎簺涓滆タ鐢╃粰璇昏呯殑錛屾繪槸浠庡熀鏈被鍨嬨佹帶鍒惰鍙ヨ繃娓°傞櫎闈炰綘宸茬粡瀵筄O榪欏鐩稿綋鐔熸倝浜嗭紝鍚﹀垯涓婃潵榪欎釜闂ㄦ灝辮兘鎶婁竴鍫嗘柊鎵嬫尅鍦ㄩ棬澶栥?/p>

鎺ヤ笅鏉ョ殑錛岃皥涓嶄笂鍗佸垎鏅︽訂錛屼絾涔熶笉鏄粈涔堣鏉ヨ交蹇殑鍐呭錛屽叧閿槸浣滆呯粰鐨勪緥瀛愯緝灝戯紝浣垮緱璞″潡銆佽凱浠h繖浜涚壒鑹查毦浠ユ帉鎻°傚啀鏈変竴鐐癸紝ruby鐨勮娉曢鏍兼湁鐩稿綋閮ㄥ垎榪樹繚鐣欐湁Perl鐨勭棔榪癸紝鑰孭erl鏄嚭浜嗗悕鐨勪互濂囨鍙瘋憲縐頒簬涓栵紝榪欎簺絎﹀彿鍦ㄦ柊鎵嬬溂閲屼笉澶栧ぉ涔︼紝鑰岄珮鎵嬩滑鍒欑埍涓嶉噴鎵嬨?/p>

鎵浠ワ紝瑕佽兘姣旇緝欏哄埄鐨勯氳繃榪欐湰涔︾殑鍏ラ棬涔嬭礬錛屼綘寰椾簨鍏堝叿鏈塐O鐨勫熀紜錛岃嚦灝戜竴闂ㄨ剼鏈璦鐨勭粡楠岋紙Perl鏈浣籌級錛屽嚱鏁板紡緙栫▼鐨勪竴浜涙蹇碉紙鍚﹀垯褰撲綘鐪嬪埌鍧楃殑涓浜涚敤娉曟椂浼氬緢榪鋒儜錛夈傜劧鍚庯紝鍙互鐢≧uby絎ㄦ嫏鐨勫啓涓浜涘皬紼嬪簭浜嗐?/p>

Ruby浣滀負鍔ㄦ佽璦涔嬩竴錛屽畠鐨勬渶澶х壒鐐硅嚜鐒舵槸“鍔ㄦ?#8221;涓や釜瀛楋紝鍏惰憲鍚嶇殑“duck typing”灝辨槸涓澶т綋鐜幫紙鍦ㄦ垜鐪嬫潵錛屽氨鍍忔槸鍩轟簬鎺ュ彛鐨勮皟鐢紝浣嗗嵈騫朵笉鐢ㄤ竴涓粰瀹氱殑鎺ュ彛鍘諱簨鍏堥檺鍒訛級銆傝繖浜涘湪楂樼駭閮ㄥ垎閲岄兘鏈変笓闂ㄨ榪幫紝鏄簲璇ラ噸鐐瑰涔犵殑閮ㄥ垎銆?/p>

閭d箞鍏ラ棬絀剁珶鐢ㄤ粈涔堜功鏇村ソ錛熸垜鎺ㄨ崘銆奅veryday Scripting with Ruby銆嬭繖鏈傚拰銆奝rogramming Ruby銆嬪悓涓涓嚭鐗堢ぞ錛屼粖騫翠竴鏈堜喚鍒氬嚭浜嗗師鐗堛傚畠鏄互鐩稿綋寰簭娓愯繘鐨勬柟寮忓甫棰嗚鑰呰繘鍏uby鐨勪笘鐣岋紝灝ゅ叾鏄綔鑰呯簿蹇冭璁$殑鍑犱釜Project鏄叏涔︿寒鐐癸紙瀛︿範涓闂ㄨ璦鏈濂界殑鏂瑰紡榪樻槸瑕佸姩鎵嬪啓紼嬪簭錛夈?/p>

鏈鍚庢葷粨濡備笅錛岄鍏堢湅銆奅veryday銆嬭繖鏈功錛岃窡鐫涔︿腑鐨勬寚瀵兼憜寮勮繃鎵鏈夌殑Project錛屽茍瀹屾垚鐩稿簲鐨勭粌涔犮傜劧鍚庡啀鐪嬨奝rogrammin銆嬩竴涔︾殑Crystallized閮ㄥ垎銆傚啀涔嬪悗灝變富瑕佹槸褰撲綔API鍙傝冩墜鍐屼簡銆傝姳閽卞幓涔頒腑鏂囩増鎴戜釜浜鴻寰椾笉鏄緢鏈夊繀瑕侊紝鎬誨叡鍥涢儴鍒嗕腑錛屽墠涓ら儴鍒嗘垜瑙夊緱铔浮鑲嬬殑錛岀鍥涢儴鍒嗗湪鐢佃剳涓婃煡鏇存柟渚匡紝鍙湁絎笁閮ㄥ垎鏈夎緝楂樼殑浠峰箋?/p>

ZelluX 2007-06-16 19:54 鍙戣〃璇勮
]]>
Ruby 瀛︿範絎旇 (2)http://m.tkk7.com/zellux/archive/2007/06/16/124681.htmlZelluXZelluXSat, 16 Jun 2007 11:30:00 GMThttp://m.tkk7.com/zellux/archive/2007/06/16/124681.htmlhttp://m.tkk7.com/zellux/comments/124681.htmlhttp://m.tkk7.com/zellux/archive/2007/06/16/124681.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/124681.htmlhttp://m.tkk7.com/zellux/services/trackbacks/124681.html1. Array#reject 鏂規硶閬嶅巻涓涓泦鍚堢殑姣忎釜鍏冪礌錛屽茍鎶婄鍚堟潯浠剁殑鍏冪礌鍒犲幓銆?br/>渚嬪鍘繪帀鏁扮粍涓殑鎵鏈夌礌鏁?br/>nums = nums.reject do | num |
prime?(num)
end
puts nums

2. String#chomp 鏂規硶
str.chomp(separator=$/) => new_str
Returns a new +String+ with the given record separator removed from
the end of _str_ (if present). If +$/+ has not been changed from
the default Ruby record separator, then +chomp+ also removes
carriage return characters (that is it will remove +\n+, +\r+, and
+\r\n+).

"hello".chomp #=> "hello"
"hello\n".chomp #=> "hello"
"hello\r\n".chomp #=> "hello"
"hello\n\r".chomp #=> "hello\n"
"hello\r".chomp #=> "hello"
"hello \n there".chomp #=> "hello \n there"
"hello".chomp("llo") #=> "he"

3. 鍒ゆ柇鏄惁鍦ㄥ懡浠よ榪愯鑴氭湰
if $0 == __FILE__
check_usage
compare_inventory_files(ARGV[0], ARGV[1])
end
綾諱技浜嶫ava綾葷殑main鏂規硶錛屽湪琚叾浠栫被瀵煎叆鏃朵笉浼氳繍琛屽叾涓殑浠g爜銆?/p>

4. Enumerable#any? 鏂規硶鏌ユ壘涓涓泦鍚堜腑鏄惁鏈夋弧瓚蟲潯浠剁殑鍏冪礌
irb(main):004:0> deposits = [1, 0, 10000]
irb(main):005:0> deposits.any? do | deposit |
irb(main):006:1* deposit > 9999
irb(main):007:1> end
=> true

5. 鍏充簬嫻嬭瘯
榪欐湰涔?Everyday Scripting with Ruby)鐨勫緢澶氱▼搴忛兘鏄緷寰祴璇曢┍鍔ㄥ紑鍙戠殑鎬濇兂鍐欏嚭鏉ョ殑錛屾祴璇曞崟鍏冧腑鐨勬柟娉曢氬父鏈変袱縐嶇洰鐨勩?br/>涓縐嶆槸direct test錛岄渶瑕佹祴璇曢偅涓嚱鏁板氨鐩存帴璋冪敤閭d釜鍑芥暟錛屼紶閫掔殑鍙傛暟閮芥槸鐩存帴鍐欏嚭鏉ョ殑銆?br/>鍙︿竴縐嶆槸bootstrapping test錛岃嫻嬭瘯鍑芥暟鐨勫弬鏁頒篃鏄氳繃鐢熸垚榪欎簺鍙傛暟鐨勫嚱鏁扮敓鎴愮殑錛屽嵆涓涓柟娉曟祴璇曚簡澶氫釜瀵硅薄銆?br/>Everyone finds their own balance between testing directly and testing indirectly. You will too.

6. Time#strftime 鏂規硶
t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"



ZelluX 2007-06-16 19:30 鍙戣〃璇勮
]]>
Ruby 鍏ラ棬絎旇 (1)http://m.tkk7.com/zellux/archive/2007/06/14/124303.htmlZelluXZelluXThu, 14 Jun 2007 06:02:00 GMThttp://m.tkk7.com/zellux/archive/2007/06/14/124303.htmlhttp://m.tkk7.com/zellux/comments/124303.htmlhttp://m.tkk7.com/zellux/archive/2007/06/14/124303.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/124303.htmlhttp://m.tkk7.com/zellux/services/trackbacks/124303.html鐪嬬殑鏄疎veryday Scripting with Ruby錛岄鏍煎拰In Action緋誨垪宸笉澶氾紝澶ч噺鐨勫疄渚嬨?br>鐜板湪瀛uby鐨勪富瑕佺洰鐨勪篃鏄痚veryday scripting錛屾柟渚挎暟鎹鐞嗐佺敓鎴愶紝鏂囦歡鎵瑰鐞嗙瓑錛孯oR涔嬬被鐨勬殏鏃朵笉鑰冭檻銆?br>
1. String.inspect 鏂規硶
鏂囨。涓殑璇存硶鏄?br>     str.inspect   => string

     Returns a printable version of _str_, with special characters
     escaped.

        str = "hello"
        str[3] = 8
        str.inspect       #=> "hel\010o"

鍏蜂綋鎯呭喌璇曡瘯 myString.inspect.inspect....灝辮兘浜嗚В涓鐐逛簡

2. Arrays.each 鍜?Arrays.collect
for_each鏂規硶

irb(main):007:0> [1, 2, 3].each do | element |
irb(main):008:1*   puts element
irb(main):009:1> end
1
2
3
=> [1, 2, 3]

鍚庤呬笌鍓嶈呯殑涓嶅悓涔嬪鍦ㄤ簬錛屽湪澶勭悊鏁版嵁鐨勫悓鏃訛紝姣忔澶勭悊鐨勮繑鍥炵粨鏋滈兘浼氫繚瀛樺埌涓涓柊鐨勬暟緇勪腑榪斿洖銆?br>irb(main):036:0> newarray = ["aBC", "B"].collect do |e|
irb(main):037:1* e.downcase
irb(main):038:1> end
=> ["abc", "b"]

3. Messages and Methods
It can be hard to remember the difference between messages and methods. A message is a request sent from some sender object. When the receiver object receives the message, it looks to see whether it has a method with the same name. If so, the Ruby code within the method is run, and the results are returned to the sender. The message is the request; the method fulfills it.
鍛冿紝榪樻槸娌℃湁鎰熸ц璇嗐?br>
4. Delimiting Blocks
鍧楃殑涓ょ琛ㄧず鏂瑰紡錛?br>array.each do | element |
    puts element
end
array.each { | element |
    puts element
}
閫氬父浣跨敤絎竴縐嶏紝浣嗗彲浠ョ敤涓琛屽啓鎴愮殑鎯呭喌涔熷彲浠ヤ嬌鐢ㄧ浜岀錛?br>array.each { | element | puts element }




ZelluX 2007-06-14 14:02 鍙戣〃璇勮
]]>
Python 瀛︿範絎旇 (2)http://m.tkk7.com/zellux/archive/2007/05/11/116773.htmlZelluXZelluXFri, 11 May 2007 08:22:00 GMThttp://m.tkk7.com/zellux/archive/2007/05/11/116773.htmlhttp://m.tkk7.com/zellux/comments/116773.htmlhttp://m.tkk7.com/zellux/archive/2007/05/11/116773.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/116773.htmlhttp://m.tkk7.com/zellux/services/trackbacks/116773.html1. Dictionary 綾誨瀷
1) 綾諱技浜嶫ava涓殑Hashtable綾伙紝Dictionary瀹氫箟浜嗛敭鍜屽肩殑涓瀵逛竴鐨勫叧緋匯?br>2) 瀹氫箟錛歿key:value}
d = {"server":"Tomcat", "database":"MySQL"}
3) key瀵瑰ぇ灝忓啓鏁忔劅銆?br>4) 鍙販鐢ㄥ悇縐嶆暟鎹被鍨嬨?br>5) 鍒犻櫎鍏冪礌 del d["server"]
6) 娓呴櫎鎵鏈夊厓绱?d.clear()

2. List 綾誨瀷
1) 瀹氫箟錛?br>li = ["a", "b", 1]
2) 涓嬫爣浠?寮濮嬨?br>3) 璐熸暟绱㈠紩浠巐ist鐨勫熬閮ㄥ悜鍓嶈鏁版潵瀛樺彇鍏冪礌錛屽嵆
li[-n] == li[len(li) - n]
4) 鍒嗙墖(slice)
>>> li = ['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[1:3]   //浠庣2涓厓绱犲埌絎?涓厓绱?br>['b', 'mpilgrim']
>>> li[1:-1]  //浠庣2涓厓绱犲埌鍊掓暟絎?涓厓绱?br>['b', 'mpilgrim', 'z']
>>> li[:3]
['a', 'b', 'mpilgrim']
>>> li[3:] 
['z', 'example']
>>> li[:]
['a', 'b', 'mpilgrim', 'z', 'example']

5) 澧炲姞鍏冪礌
a) append 鏈熬榪藉姞鍗曚釜鍏冪礌
b) insert 灝嗗崟涓厓绱犳彃鍏ユ煇涓綅緗?br>>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
>>> li.insert(2, "new")           
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
c) extend 鍚堝茍鍙︿竴涓狶ist

6) 鎼滅儲
a) index 鏌ユ壘涓涓肩殑棣栨鍑虹幇騫惰繑鍥炵儲寮曞鹼紝濡傛灉娌℃湁鎵懼埌鍒欏紩鍙戝紓甯革紝涓嶫ava涓嶅悓
b) in 鎿嶄綔絎︽祴璇曚竴涓兼槸鍚﹀湪list鍐?/p>

7) 鍒犻櫎鍏冪礌
a) remove 鍒犻櫎鏌愪釜鍏冪礌錛屽涓嶅瓨鍦ㄥ垯寮曞彂寮傚父
b) pop 寮瑰嚭鏍堥《鍏冪礌錛屽嵆鍒犻櫎鏈鍚庝竴涓煎茍榪斿洖璇ュ?/p>

8) List榪愮畻絎?鐪熸柟渚垮晩 -,=
>>> li = ['a', 'b', 'mpilgrim']
>>> li = li + ['example', 'new']
>>> li
['a', 'b', 'mpilgrim', 'example', 'new']
>>> li += ['two']               
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
>>> li = [1, 2] * 3             
>>> li
[1, 2, 1, 2, 1, 2]



ZelluX 2007-05-11 16:22 鍙戣〃璇勮
]]>
鍒濆鏃剁殑絎旇http://m.tkk7.com/zellux/archive/2007/05/02/114991.htmlZelluXZelluXTue, 01 May 2007 17:09:00 GMThttp://m.tkk7.com/zellux/archive/2007/05/02/114991.htmlhttp://m.tkk7.com/zellux/comments/114991.htmlhttp://m.tkk7.com/zellux/archive/2007/05/02/114991.html#Feedback0http://m.tkk7.com/zellux/comments/commentRss/114991.htmlhttp://m.tkk7.com/zellux/services/trackbacks/114991.html
1. 瀛楃涓詫細
a) 浣跨敤涓夊紩鍙?’’’鎴?#8221;””)鐢ㄦ潵鎸囩ず澶氳鐨勫瓧絎︿覆錛屽叾涓彲浠ヨ嚜鐢辯殑浣跨敤鍗曞紩鍙鋒垨鍙屽紩鍙楓?br>b) 鑷劧瀛楃涓詫細鍦ㄥ瓧絎︿覆鍓嶅姞涓妑鎴朢錛岃〃紺轟笉闇瑕佽漿涔夌鐗規畩澶勭悊銆?br>c) Unicode瀛楃涓詫細涓插墠鍔爑銆?br>d) 鍜孞ava涓鏍鳳紝瀛楃涓蹭笉鍙彉銆?br>e) Python娌℃湁涓撻棬鐨刢har綾誨瀷銆?br>f) 浣跨敤姝e垯琛ㄧず寮忔椂錛屽緩璁嬌鐢ㄨ嚜鐒惰〃杈懼紡澶勭悊銆?br>2. 鏍囪瘑絎︼細蹇呴』浠ュ瓧姣嶆垨涓嬪垝綰胯搗濮嬶紝鍏朵粬閮ㄥ垎鍙互鏈夋暟瀛楋紝澶у皬鍐欐晱鎰熴?br>3. 琛岋細
a) 姣忎釜Python紼嬪簭閮芥湁涓浜涢昏緫琛?logical liens)緇勬垚錛屽悗鑰呴氬父鐢辮嫢騫茬墿鐞嗚(physical lines)緇勬垚銆?br>b) 姣忎釜鐗╃悊琛岄兘鍙互鐢變竴鏉℃敞閲婄粨鏉燂紝涓嶅湪瀛楃涓蹭腑鐨?絎﹀彿琛ㄧず涓鏉℃敞閲娿?br>c) 鍦ㄤ竴涓氦浜掕В閲婄晫闈腑錛屽繀欏婚氳繃杈撳叆絀鴻鏉ョ粨鏉熷琛岃鍙ャ?br>d) ; 鍙互鐢ㄤ簬鍒嗗壊涓鐗╃悊琛屼腑鐨勪笉鍚岄昏緫琛岋紝浣哖ython涓緢灝戝嚭鐜?;
e) 杞箟絎?\ 涔熷彲浠ョ敤浜庤鏈〃紺哄湪涓嬩竴琛岀戶緇?br>4. 緙╄繘錛?br>a) 緙╄繘瀵逛簬Python涓緢閲嶈錛岄敊璇殑緙╄繘寰堟湁鍙兘浼氬紩鍙戦敊璇?br>b) 鍚屼竴灞傛鐨勮鍙ュ繀欏繪湁鐩稿悓鐨勭緝榪涖傛瘡涓緇勮繖鏍風殑璇彞縐頒負涓涓潡銆?br>5. 榪愮畻
a) // 榪斿洖鍟嗙殑鏁存暟閮ㄥ垎錛屽嵆Java涓殑 \
b) 姣旇緝榪愮畻絎﹀彲浠ヨ浠繪剰榪炵粨錛屽3<5<7榪斿洖True銆?br>6. 鎺у埗嫻?br>a) raw_input(‘Input a number: ‘) 灝嗗瓧絎︿覆杈撳叆鍒板睆騫曚笂錛屽茍絳夊緟鐢ㄦ埛鐨勮緭鍏ワ紝榪斿洖瀛楃涓層傛湁鐐瑰儚Basic鐨刬nput銆?br>b) if 璇彞涓璭lif鐩稿綋浜巈lse if銆備竴涓畝鍗曠殑if璇彞錛?br>if True:
    print ‘It is true’
c) Python涓病鏈塻witch璇彞錛屼嬌鐢╥f-elif-else浠f浛銆?br>d) while, for璇彞涓篃鍙互鏈塭lse:錛屾敞鎰忎笌while, for瀵歸綈
e) for i in range(1, 5)
渚濇寰幆i=1, 2, 3, 4
range()榪斿洖涓涓簭鍒楃殑鏁幫紝鍖呮嫭1錛屼笉鍖呮嫭5銆傚彟澶栧彲浠ュ啀澧炲姞涓涓闀垮弬鏁幫紝濡俽ange(0, 4, 2)鐢熸垚0鍜?
f) break璇彞璺沖嚭鍚庯紝else璇彞涓嶈鎵ц銆?br>7. 鍑芥暟
a) 褰㈠弬錛氬嚱鏁板ご涓0鏄庣殑鍙傛暟錛涘疄鍙傦細鎻愪緵緇欏嚱鏁拌皟鐢ㄧ殑鍙傛暟銆?br>b) 鍑芥暟鍧楀茍涓嶄細淇敼鍧楀鍙傛暟鐨勫箋傚彲浠ヤ嬌鐢╣lobal璇彞澹版槑涓哄叏灞鍙橀噺錛屼絾搴斿敖閲忛伩鍏嶃?br>c) 榛樿鍙傛暟鍊鹼細鐩存帴鍦ㄥ嚱鏁板ご涓祴鍊箋備絾鍙湁鍦ㄦ湯灝劇殑鍙傛暟鎵嶅彲浠ユ湁榛樿鍊箋?br>d) 鍏抽敭鍙傛暟銆?br>

ZelluX 2007-05-02 01:09 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 久久夜色精品国产亚洲av| 亚洲国产综合专区电影在线| 一级做α爱过程免费视频| 狠狠色伊人亚洲综合成人| 黄页网站在线看免费| 日韩在线视频播放免费视频完整版| 亚洲日韩v无码中文字幕| 18勿入网站免费永久| 深夜福利在线视频免费| 久久久国产精品亚洲一区| 日本不卡视频免费| 久久国产精品免费看| 亚洲aⅴ天堂av天堂无码麻豆| 久久精品国产亚洲香蕉| 免费在线精品视频| 国产一卡2卡3卡4卡无卡免费视频 国产一卡二卡3卡四卡免费 | 亚洲av无码av制服另类专区| 成人性生免费视频| 久久中文字幕免费视频| 麻豆安全免费网址入口| 亚洲国产成人久久精品app| 亚洲精品无码专区久久久| 免费黄色大片网站| 18禁美女裸体免费网站| 国产免费伦精品一区二区三区| 亚洲av无码片在线观看| 亚洲av日韩av无码| 亚洲一级片内射网站在线观看| 成年女人免费碰碰视频| 精品无码国产污污污免费网站 | 免费成人午夜视频| 男人的好免费观看在线视频| a级片在线免费看| 国产成人精品日本亚洲语音| 亚洲国产日产无码精品| 久久精品国产亚洲av麻豆| 久久久精品国产亚洲成人满18免费网站| 成年午夜视频免费观看视频| 在线观看免费视频资源| 日韩精品免费在线视频| 成人精品一区二区三区不卡免费看|