草庐IT

exception-specification

全部标签

python - 如何修复 ValueError : read of closed file exception?

这个简单的Python3脚本:importurllib.requesthost="scholar.google.com"link="/scholar.bib?q=info:K7uZdMSvdQ0J:scholar.google.com/&output=citation&hl=en&as_sdt=1,14&ct=citation&cd=0"url="http://"+host+linkfilename="cite0.bib"print(url)urllib.request.urlretrieve(url,filename)引发此异常:Traceback(mostrecentcalllas

python - 当客户端过早断开连接时,如何对 Flask 上的破损管道错误进行异常(exception)处理?

我正在使用flask进行开发,而不是生产,我有一个ajax请求的View,如下所示:@application.route('/xyz//',methods=['GET'])defgetAjax(var):...returnrender_template(...)我还在使用threaded=true进行开发。每当我调用该ajax请求然后关闭请求它的选项卡时,我都会收到错误消息:Traceback(mostrecentcalllast):File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/S

python - 我得到 "TypeError: exceptions must derive from BaseException"即使我确实定义了它

根据python文档,Exception派生自BaseExceptions,我应该将它用于用户定义的异常。所以我有:classVisaIOError(Exception):def__init__(self,error_code):abbreviation,description=_completion_and_error_messages[error_code]Error.__init__(self,abbreviation+":"+description)self.error_code=error_code和raise(visa_exceptions.VisaIOError,stat

python - PIL : Convert RGB image to a specific 8-bit palette?

使用Python图像库,我可以调用img.convert("P",palette=Image.ADAPTIVE)或img.convert("P",palette=Image.WEB)但有没有办法转换成任意调色板?p=[]foriinrange(0,256):p.append(i,0,0)img.convert("P",palette=p)它将在哪里将每个像素映射到图像中找到的最接近的颜色?还是Image.WEB仅支持此功能? 最佳答案 在查看convert()的源代码时,我发现它引用了im.quantize。quantize可以采用

Python - requests.exceptions.SSLError - dh key 太小

我正在使用Python和请求抓取一些内部页面。我已经关闭了SSL验证和警告。requests.packages.urllib3.disable_warnings()page=requests.get(url,verify=False)在某些服务器上,我收到无法通过的SSL错误。Traceback(mostrecentcalllast):File"scraper.py",line6,inpage=requests.get(url,verify=False)File"/cygdrive/c/Users/jfeocco/VirtualEnv/scraping/lib/python3.4/si

python - 为什么建议从 Exception 派生而不是 Python 中的 BaseException 类?

Python2documentation说“鼓励程序员从Exception类或其子类之一派生新的异常,而不是从BaseException”。没有进一步解释原因。我很好奇为什么会这样推荐?是否只是为了保留exceptionshierarchy正如Python开发人员所设想的那样?>>>dir(BaseException)==dir(Exception)True 最佳答案 从BaseException派生的异常是:GeneratorExit、KeyboardInterrupt、SystemExit。根据文档:GeneratorExit:

python - 其他选项而不是使用 try-except

当文本文件中的第2行有'nope'时,它将忽略该行并继续下一行。有没有不使用try和except的另一种写法?我可以使用ifelse语句来执行此操作吗?文本文件示例:0102nope1325nope代码:e=open('e.txt')alist=[]forlineine:start=int(line.split()[0])target=int(line.split()[1])try:ifline.split()[2]=='nope':continueexceptIndexError:alist.append([start,target]) 最佳答案

Python 单元测试 : cancel all tests if a specific test fails

我正在使用unittest来测试我的Flask应用程序,并使用nose来实际运行测试。我的第一组测试是为了确保测试环境干净,并防止在Flask应用程序配置的数据库上运行测试。我确信我已经干净地设置了测试环境,但我希望在不运行所有测试的情况下对此有一些保证。importunittestclassMyTestCase(unittest.TestCase):defsetUp(self):#setsomestuffuppassdeftearDown(self):#dotheteardownpassclassTestEnvironmentTest(MyTestCase):deftest_envi

python异常<类型 'exceptions.ImportError'>没有名为gdb :的模块

我刚刚在运行linux的服务器计算机上从主目录中的源代码编译了gdb7.8。我之前一直在使用gdb7.6,除了gdb本身的稳定性问题(升级的原因)之外,一切正常。自从gdb升级后,当我运行cgdb0.6.7时,我立即收到以下消息:PythonExceptionNomodulenamedgdb:warning:CouldnotloadthePythongdbmodulefrom`/home/username/bin/gdb//python'.LimitedPythonsupportisavailablefromthe_gdbmodule.Suggestpassing--data-dire

Python 请求.exceptions.SSLError : EOF occurred in violation of protocol

我将从提供RESTfulJSONAPI的ABBG13网关检索一些信息。API通过https端点由网关托管。基本认证机制用于认证。然而所有的交通通过SSL层。在linux上使用命令:curl-s-k-XGET-uuser:passwordhttps://host/meters/a_serial/power一切顺利!我正在尝试使用Requests2.8.1和以下代码在Python2.7.10中为Windows编写脚本:importrequestsrequests.get('https://host/meters/a_serial/power',auth=('user','password'