捕获OptimizeWarning作为例外

时间:2015-07-08 18:38:47

标签: python exception scipy warnings

我只是试图抓住Public Sub ExportToXL() Const SheetSize = 65000 'Number of records per Excel sheet Dim appExcel As Excel.Application Dim wkbWorkBook As Excel.Workbook Dim wksWorkSheet As Excel.Worksheet Dim rngYCursor As Excel.Range, rngXCursor As Excel.Range Dim ADAExcelExpt As String Dim conn As New ADODB.Connection Dim RS As ADODB.Recordset 'Dim RS As New ADODB.Recordset Dim i As Long, lngPN As Long Set appExcel = CreateObject("Excel.Application") With appExcel .Visible = True .UserControl = True Set wkbWorkBook = .Workbooks.Add End With With wkbWorkBook.Worksheets While .Count > 1 .Item(1).Delete Wend Set wksWorkSheet = .Item(1) End With With wksWorkSheet lngPN = 1 .Name = Me.cboDeduc.Text & " " & lngPN Set rngYCursor = .Range("A2") .Range("A1:I1").Value = Array("AGENCY", "DEDCODE", "AFPSN", "RANK", "FULLNAME", "AMOUNT", "DEDTYPE", "DESCRIPTION", "DATE") End With ADAExcelExpt = "E:\Remittance Report\PrintSource.mdb" conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ADAExcelExpt & ";" conn.CursorLocation = adUseClient Set RS = conn.Execute("Select FAGYDES, DedCode, FSERIAL, RANK, FULLNAME, DedAmt, FDEDDESC, FFULDESC, Datefm from tblPrintDeduct") While True For i = 1 To SheetSize Set rngXCursor = rngYCursor If RS.EOF Then GoTo ExitSub For Each fld In RS.Fields rngXCursor.Value = fld.Value Set rngXCursor = rngXCursor.Offset(ColumnOffset:=1) Next Set rngYCursor = rngYCursor.Offset(RowOffset:=1) RS.MoveNext Next i 'MsgBox ("File Exported!") Set wksWorkSheet = wkbWorkBook.Worksheets.Add(After:=wksWorkSheet) With wksWorkSheet lngPN = lngPN + 1 .Name = Me.cboDeduc.Text & " " & lngPN Set rngYCursor = .Range("A2") .Range("A1:I1").Value = Array("AGENCY", "DEDCODE", "AFPSN", "RANK", "FULLNAME", "AMOUNT", "DEDTYPE", "DESCRIPTION", "DATE") End With Wend 'End With ExitSub: RS.Close Set rngXCursor = Nothing Set rngYCursor = Nothing Set RS = Nothing Set wksWorkSheet = Nothing Set wkbWorkBook = Nothing Set appExcel = Nothing End Sub 函数抛出的OptimizeWarning,但我意识到它并未被识别为有效例外。

对于我正在做的事情,这是一个非常简单的想法:

scipy.optimize.curve_fit

我环顾四周the docs,但那里什么都没有。

我是否遗漏了一些明显的东西,或者由于某些原因而没有明确定义?

顺便说一句,这是我得到的完整警告,我想抓住:

from scipy.optimize import curve_fit
try:
    popt, pcov = curve_fit(some parameters)
except OptimizeWarning:
    print 'Maxed out calls.'
    # do something

1 个答案:

答案 0 :(得分:5)

您可以要求Python使用以下代码将此警告作为异常引发:

import warnings
from scipy.optimize import OptimizeWarning

warnings.simplefilter("error", OptimizeWarning)
# Your code here

警告问题

不幸的是,Python中的warnings有一些您需要注意的问题。

多个过滤器

首先,可以有多个过滤器,因此您的警告过滤器可以被其他内容覆盖。这不是太糟糕,可以使用catch_warnings上下文管理器解决:

import warnings
from scipy.optimize import OptimizeWarning

with warnings.catch_warnings():
    warnings.simplefilter("error", OptimizeWarning)
    try:
        # Do your thing 
    except OptimizeWarning:
        # Do your other thing 

一次提升

其次,警告仅在默认情况下提出一次。如果在设置过滤器之前已经提出警告,则可以更改过滤器,它不会再次发出警告。

据我所知,遗憾的是,你无能为力。您希望确保尽早运行warnings.simplefilter("error", OptimizeWarning)