草庐IT

assert_nothing_raised

全部标签

python - 值错误 : negative number cannot be raised to a fractional power

当我在终端尝试这个时>>>(-3.66/26.32)**0.2我收到以下错误Traceback(mostrecentcalllast):File"",line1,inValueError:negativenumbercannotberaisedtoafractionalpower但是,我可以分两步完成,例如,>>>(-3.66/26.32)-0.13905775075987842>>>-0.13905775075987842**0.2-0.6739676327771593为什么会有这种行为?单行解决这个问题的方法是什么? 最佳答案

python - 不要在异常堆栈中显示 Python raise-line

当我在我的Python库中引发自己的异常时,异常堆栈将引发行本身显示为堆栈的最后一项。这显然不是错误,在概念上是正确的,但是当您在外部使用代码(例如作为模块)时,将重点放在对调试无用的东西上。有没有办法避免这种情况并强制Python将上一个到最后一个堆栈项显示为最后一个,就像标准Python库一样。 最佳答案 适当的警告:修改解释器的行为通常是不受欢迎的。在任何情况下,准确查看引发错误的位置可能有助于调试,尤其是当函数可能因多种不同原因引发错误时。如果您使用traceback模块,并将sys.excepthook替换为自定义函数,这

python - 单元测试 : Assert that a file/path exists

我正在尝试为我的安装程序创建回归测试。回归测试是用Python编写的脚本。测试检查是否已将正确的文件安装在正确的位置。有没有办法断言文件/文件夹存在?我收到以下代码的AssertionError错误:assertos.path.exists(LOCAL_INSTALL_DIR)==1为什么会出现此错误,我该如何解决?我的功能:defcheck_installation_files_exist():assertos.path.exists(LOCAL_INSTALL_DIR)==1assertos.path.exists(INSTALL_DIR)==1correct_install_fi

python - "RuntimeError: generator raised StopIteration"每次我尝试运行应用程序时

我正在尝试运行此代码:importweburls=('/','index')if__name__=="__main__":app=web.application(urls,globals())app.run()但它每次都会给我这个错误C:\Users\aidke\Desktop>pythonapp.pyTraceback(mostrecentcalllast):File"C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py",line526,intakeyieldn

Python 3.7 : dataclass does not raise `TypeError` for `eq=False`

我正在尝试Python3.7中的新dataclassesdataclass装饰器可以传递参数来控制添加到类中的dunder函数。由于某种原因,装饰器似乎没有为eq=False参数引发TypeError。根据文档:eq:Iftrue(thedefault),an__eq__methodwillbegenerated.Thismethodcomparestheclassasifitwereatupleofitsfields,inorder.Bothinstancesinthecomparisonmustbeoftheidenticaltype如果我理解正确,如果我通过eq=False,__

python : Assert that variable is instance method?

如何检查变量是否为实例方法?我正在使用python2.5。类似这样的:classTest:defmethod(self):passassertis_instance_method(Test().method) 最佳答案 inspect.ismethod如果您确实有方法,而不仅仅是您可以调用的东西,是您想知道的。importinspectdeffoo():passclassTest(object):defmethod(self):passprintinspect.ismethod(foo)#Falseprintinspect.isme

python - Django : get_or_create Raises duplicate entry with together_unique

模型示例classExample(Stat):numeric=models.IntegerField(...)date=models.DateField(auto_now_add=True,...)#auto_now_add=TruewastheproblemclassMeta:unique_together=('numeric','date'))如果72和'2011-08-07'已存储Example.object.get_or_create(numeric=72,date='2011-08-07')提高django.db.utils.IntegrityError:(1062,"Dup

python - 如何修复 : W602 deprecated form of raising exception

如果我使用pylint(通过sublimerlinter),我会收到以下警告消息:W602已弃用的引发异常的形式这是我在代码中使用异常的方式:ifCONDITION==True:raiseValueError,HELPING_EXPLANATION 最佳答案 像这样提出你的异常:ifCONDITION==True:raiseValueError(HELPING_EXPLANATION)来自PEP8--StyleGuideforPythonCode-ProgrammingRecommendations:Whenraisinganexc

python - “assert False”和 “self.assertFalse”有什么优势或区别

我正在编写测试,我听说有人说要使用self.assertFalse而不是assertFalse.为什么会这样,有什么好处吗? 最佳答案 如果你跑了importunittestclassTest_Unittest(unittest.TestCase):deftest_assert(self):assertFalsedeftest_assertFalse(self):self.assertFalse(True)if__name__=='__main__':unittest.main()你得到同样的日志信息,同样的失败:FF=======

python - Python 中的 raise 会提高什么?

考虑以下代码:try:raiseException("a")except:try:raiseException("b")finally:raise这将引发Exception:a。我希望它会引发Exception:b(需要我解释为什么吗?)。为什么最后的raise引发原始异常,而不是(我认为)是最后引发的异常? 最佳答案 Raiseisre-raisingthelastexceptionyoucaught,notthelastexceptionyouraised(为了清楚起见,从评论中重新发布)