Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Python±ê×¼¿â tracebackÄ£¿é

Example 2-18 չʾÁË traceback Ä£¿éÔÊÐíÄãÔÚ³ÌÐòÀï´òÓ¡Òì³£µÄ¸ú×Ù·µ»Ø
(Traceback)ÐÅÏ¢, ÀàËÆÎ´²¶»ñÒ쳣ʱ½âÊÍÆ÷Ëù×öµÄ. Èç Example 2-18 Ëùʾ. 2.11.0.1. Example
2-18. ʹÓà traceback Ä£¿é´òÓ¡¸ú×Ù·µ»ØÐÅÏ¢ File: traceback-example-1.py # note!
import
Example 2-18 չʾÁË traceback Ä£¿éÔÊÐíÄãÔÚ³ÌÐòÀï´òÓ¡Òì³£µÄ¸ú×Ù·µ»Ø (Traceback)ÐÅÏ¢, ÀàËÆÎ´²¶»ñÒ쳣ʱ½âÊÍÆ÷Ëù×öµÄ. Èç Example 2-18 Ëùʾ.
2.11.0.1. Example 2-18. ʹÓà traceback Ä£¿é´òÓ¡¸ú×Ù·µ»ØÐÅÏ¢
File: traceback-example-1.py
# note! importing the traceback module messes up the
# exception state, so you better do that here and not
# in the exception handler
# ×¢Òâ! µ¼Èë traceback »áÇåÀíµôÒ쳣״̬, ËùÒÔ
# ×îºÃ±ðÔÚÒì³£´¦Àí´úÂëÖе¼Èë¸ÃÄ£¿é
import traceback
try:
raise SyntaxError, "example"
except:
traceback.print_exc()
Traceback (innermost last):
File "traceback-example-1.py", line 7, in ?
SyntaxError: example
Example 2-19 ʹÓà StringIO Ä£¿é½«¸ú×Ù·µ»ØÐÅÏ¢·ÅÔÚ×Ö·û´®ÖÐ.
2.11.0.2. Example 2-19. ʹÓà traceback Ä£¿é½«¸ú×Ù·µ»ØÐÅÏ¢¸´ÖƵ½×Ö·û´®
File: traceback-example-2.py
import traceback
import StringIO
try:
raise IOError, "an i/o error occurred"
except:
fp = StringIO.StringIO()
traceback.print_exc(file=fp)
message = fp.getvalue()
print "failure! the error was:", repr(message)
failure! the error was: 'Traceback (innermost last):\012 File
"traceback-example-2.py", line 5, in ?\012IOError: an i/o error
occurred\012'
Äã¿ÉÒÔʹÓà extract_tb º¯Êý¸ñʽ»¯¸ú×Ù·µ»ØÐÅÏ¢, µÃµ½°üº¬´íÎóÐÅÏ¢µÄÁбí, Èç Example 2-20 Ëùʾ.
2.11.0.3. Example 2-20. ʹÓà traceback Module Ä£¿é±àÂë Traceback ¶ÔÏó
File: traceback-example-3.py
import traceback
import sys
def function():
raise IOError, "an i/o error occurred"
try:
function()
except:
info = sys.exc_info()
for file, lineno, function, text in traceback.extract_tb(info[2]):
print file, "line", lineno, "in", function
print "=>


Ïà¹ØÎĵµ£º

һƪ²»´íµÄPythonÈëÃŽ̳Ì

Ô­ÎÄ
 http://www.hetland.org/python/instant-hacking.php
Instant Hacking[Òë
ÎÄ]
ÒëÕߣº ¿Ï¶¨À´¹ý                                ......

pythonµÄÁé»î

ÏîÄ¿ÐèÒª£¬¸Õ¸Õ½Ó´¥python¡£
½ñÌì¿´Êé¿´µ½a>b==c £¬a,b,cΪinteger
ÔÚC/C++/C#ÖУ¬a>bΪboolean,²»¿ÉÓëinteger±È½ÏÏàµÈ
µ«python a>b==cµÈЧÓÚ((a>b)&&(b==c))
ÔÚpythonÖеÄд·¨ÊÇa>b and b==c ......

Ò»¸ö¼õÉÙ¶à½ø³ÌÏÂpythonÄÚ´æÕ¼ÓõÄС¼¼ÇÉ

¡¡¡¡ÔÚpythonÖÐÈç¹ûҪʹÓÃfork»òÕßmultiprocessÀ´×ö¶à½ø³Ì£¬¼ÙÈçimportµÄ¿âºÜ¶à»òÕߺܴó£¬ÀýÈçwxpython£¬²é¿´¸÷½ø³ÌµÄÄÚ´æÕ¼ÓÃÄã»á·¢ÏÖÁ½¸ö½ø³ÌµÄÄÚ´æÕ¼Óûù±¾ÉÏÏàͬ£¬»»¾ä»°Ëµ£¬Èç¹ûµÚ¶þ¸ö½ø³Ì¸ù±¾²»ÐèÒªÓõ½wxµÄÄÚÈÝ£¬ËüÒ²»á±»¼ÓÔØµ½ÄÚ´æµ±ÖÐÈ¥¡£¾ÍÄÃwxÀ´×öÀý×Ó£¬Ã»ÓÐimport t wxÕ¼Óôó¸Å8M+Äڴ棬һµ©import wx£¬ÄÚ´æÂíÉ ......

ת¶ÎPython´úÂë

#==================================================
import wx
import wx.media

class MyFrame(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,-1,title,pos=(150,150),size&;nbsp;=(640, 480),style=wx.MAXIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX| ......

ÓÃPythonʵÏÖÍøÕ¾²âÊÔ

²âÊÔÍøÕ¾£ºBaiduÖ÷Ò³ÒÔ¼°Ïà¹ØµÄÒ³Ãæ
Ö÷Ì⣺ºÚºÐ²âÊÔ£¬¹¦ÄܲâÊÔ£¬ÐÔÄÜ/ѹÁ¦²âÊÔ£¬°²È«²âÊÔ£¬ Êý¾Ý×Öµä¹ÜÀí
1. ²âÊÔÒ³ÃæÊÇ·ñ¿É·ÃÎʵģ»
1.1 HTTP request status = 200, Ò³ÃæÏÂÔØÕýÈ·
from urllib import urlopen
# set up
url = None
# test
url = urlopen('http://www.baidu.com')
assert url.getcode() == 200
# ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ