草庐IT

Assert_Raise

全部标签

python - Pandas assert_frame_equal 行为

我正在尝试通过pandas测试assert_frame_equal来比较两个DataFrame。这些帧包含float,我想将其与某些用户定义的精度进行比较。assert_frame_equal中的check_less_precise参数似乎表明我可以指定要比较的小数点后的位数。引用API引用页面-check_less_precise:Specifycomparisonprecision.Onlyusedwhencheck_exactisFalse.5digits(False)or3digits(True)afterdecimalpointsarecompared.Ifint,thens

python - 使用 assert 与 raise Exception 的含义是什么

与以下有什么显着差异吗?raiseException("someexception")assertFalse,"someexception" 最佳答案 启动Python时,可以使用-O标志禁用断言。因此,仅将断言用于完整性检查,而不是用于检查程序逻辑的一部分。除此之外,当然还有断言引发AssertionError的区别,您真的不应该捕获它。当您引发异常时,您可以使异常类型适合于错误并稍后捕获它。 关于python-使用assert与raiseException的含义是什么,我们在Stac

python - 函数失败 : Raise Exception, 或返回 FALSE?什么是更好的方法?

我想知道你们是如何处理函数失败的。您是引发异常还是返回错误消息?例如我有一个应该连接到外部com对象的函数。如果com-object尚未通过另一个程序启动,则无法建立连接。通知主程序的首选python方式是什么?我应该使用详细的错误消息引发异常,还是应该简单地返回错误消息?谢谢! 最佳答案 python绝对是站在异常这一边的。我总能找到thisarticle成为一个很好的解释。 关于python-函数失败:RaiseException,或返回FALSE?什么是更好的方法?,我们在Stac

python - 请求——总是调用 raise_for_status

我想删除重复的x.raise_for_status()行:x=requests.get(url1)x.raise_for_status()y=requests.delete(url2)y.raise_for_status()z=requests.post(url3,data={'foo':'bar'})z.raise_for_status()如何自动调用raise_for_status()? 最佳答案 使用钩子(Hook)创建session:session=requests.Session()session.hooks={'resp

python - Pandas :带有 np.seterr(所有 ='raise')和缺失数据的 FloatingPointError

当我想查看涉及缺失数据的数据时,我收到了FloatingPointError。importnumpyasnpimportpandasaspdnp.seterr(all='raise')s=pd.Series([np.nan,np.nan,np.nan],index=[1,2,3]);print(s);print(s.head())我正在使用最新版本的pandas,通过安装condainstall-fpandas在pkillpython和condaremovepandas之后。这是回溯:Out[4]:--------------------------------------------

python - 如何在单元测试中使用 assert_frame_equal

unittest包的新功能。我正在尝试通过以下代码验证函数返回的DataFrame。即使我将assert_frame_equal的输入硬编码为相等(pd.DataFrame([0,0,0,0])),单元测试仍然失败。有人愿意解释为什么会这样吗?importunittestfrompandas.util.testingimportassert_frame_equalclassTestSplitWeight(unittest.TestCase):deftest_allZero(self):#splitWeight(pd.DataFrame([0,0,0,0]),10)self.assert

python - 为什么 Pylint 在此 raise 语句中给出错误 E0702,引发 NoneType?

假设我有以下代码。deffoo():foobar=NoneiffoobarisnotNone:raisefoobar当我通过pylint运行此代码时,出现以下错误:E0702:4:foo:RaisingNoneTypewhileonlyclasses,instancesorstringareallowed这是pylint中的错误吗?我的pylint太旧了吗?pylint0.18.0,astng0.19.1,common0.45.0Python2.5.1(r251:54863,Aug252008,09:23:26)注意:我知道这段代码没有任何意义,它被提炼到最基本的部分以暴露手头的问题,

没有参数的 Python 'raise' : what is "the last exception that was active in the current scope"?

Python的文档说:Ifnoexpressionsarepresent,raisere-raisesthelastexceptionthatwasactiveinthecurrentscope.(Python3:https://docs.python.org/3/reference/simple_stmts.html#raise;Python2.7:https://docs.python.org/2.7/reference/simple_stmts.html#raise。)但是,“最后激活”的概念似乎已经改变。见证以下代码示例:#from__future__importprint_f

python - 使 Mock.assert_called_with() 与 args vs kwargs 不可知

单元测试应该测试功能并尽量不了解实现细节。Mock.assert_called_with()是一个方便的函数,但据我所知它将*args与*args和**kwargs到**kwargs。因此:#classtobemockedduringtestclassSomeClass():deffunc(self,a,b,c=5):#...#codeundertestsomaclass_instance.func(1,b=2,c=3)#testcodethatworkssomeclass_mock.func.assert_called_with(1,b=2,c=3)#testcodethatwon'

python - 在没有 -0 标志的情况下禁用 python 的 assert()

我正在从不同的软件中运行一个python脚本(它提供了一个python接口(interface)来操作它的数据结构)。我正在优化我的代码以提高速度,并希望了解我的断言对性能有何影响。我无法使用python-O。我还有哪些其他选项可以以编程方式禁用python代码中的所有断言?变量__debug__(由-O标志清除)不能分配给:( 最佳答案 Thedocssay,Thevalueforthebuilt-invariable[__debug__]isdeterminedwhentheinterpreterstarts.因此,如果您无法控