assert

异常处理

与世无争的帅哥 提交于 2019-12-01 15:51:37
目录 一、什么是异常 1.1 语法错误 1.2 逻辑错误 二、异常的种类 2.1 常用异常 2.2 其他异常 三、异常处理 3.1 提前预防 3.2 之后预防 四、try...except总结 五、抛出异常raise 5.1 自定义异常 六、断言assert 一、什么是异常 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下 1.1 语法错误 语法错误,根本过不了python解释器的语法检测,必须在程序执行前就改正。 # 语法错误示范一 if # 语法错误示范二 def test: pass # 语法错误示范三 class Foo pass # 语法错误示范四 print(haha 1.2 逻辑错误 # TypeError:int类型不可迭代 for i in 3: pass # ValueError num=input(">>: ") #输入hello int(num) # NameError aaa # IndexError l=['egon','aa'] l[3] # KeyError dic={'name':'egon'} dic['age'] # AttributeError class Foo:pass Foo.x # ZeroDivisionError

How to check if value is nan in unittest?

二次信任 提交于 2019-12-01 15:47:27
I've got functions, which sometimes return NaNs with float('nan') (I'm not using numpy). How do I write a test for it, since assertEqual(nan_value, float('nan')) is just like float('nan') == float('nan') always false. Is there maybe something like assertIsNan ? I could not find anything about it… I came up with assertTrue(math.isnan(nan_value)) math.isnan(x) will raise a TypeError if x is neither a float nor a Real . It's better to use something like this : import math class NumericAssertions: """ This class is following the UnitTest naming conventions. It is meant to be used along with

How to use static_assert for constexpr function arguments in C++?

China☆狼群 提交于 2019-12-01 15:25:52
I have several brief constexpr functions in my libraries that perform some simple calculations. I use them both in run-time and compile-time contexts. I would like to perform some assertions in the body of these functions, however assert(...) is not valid in a constexpr function and static_assert(...) can not be used to check function parameters. Example: constexpr int getClamped(int mValue, int mMin, int mMax) noexcept { assert(mMin <= mMax); // does not compile! return mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue); } Is there a way to check whether the function is being executed in

What Standard Calls are Actually Macros

心不动则不痛 提交于 2019-12-01 14:06:53
I asked a question here about assert which is implemented in the standard as a macro, not a function. This had caused me an issue because the way that assert appears to be a function in the way it takes a parameter: assert(true) Thus I tried to use it as: std::assert(true) and of course being a macro that didn't work. My question is this: Are there any other macros provided by the standard library which would appear as functions that take parameters? If we look at [headers] paragraphs 5 and 6 we have Names which are defined as macros in C shall be defined as macros in the C++ standard library,

How to check if value is nan in unittest?

风格不统一 提交于 2019-12-01 13:53:51
问题 I've got functions, which sometimes return NaNs with float('nan') (I'm not using numpy). How do I write a test for it, since assertEqual(nan_value, float('nan')) is just like float('nan') == float('nan') always false. Is there maybe something like assertIsNan ? I could not find anything about it… 回答1: I came up with assertTrue(math.isnan(nan_value)) 回答2: math.isnan(x) will raise a TypeError if x is neither a float nor a Real . It's better to use something like this : import math class

Pytest命令行执行测试

こ雲淡風輕ζ 提交于 2019-12-01 11:59:43
Pytest命令行执行测试 from collections import namedtuple Task = namedtuple('Task', ['summary','owner','done','id']) # __new__.__defaults__创建默认的Task对象 Task.__new__.__defaults__ = (None, None, False, None) def test_default(): """ 如果不传任何参数,则默认调用缺省对象Task.__new__.__defaults__ = (None, None, False, None) """ t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2 def test_member_access(): """ 利用属性名来访问对象成员 :return: """ t = Task('buy milk', 'brian') assert t.summary == 'buy milk' assert t.owner == 'brian' assert(t.done, t.id) == (False, None) def test_asdict(): """ _asdict()返回一个字典 """ t_task = Task('do

Assert to compare two lists of objects C#

柔情痞子 提交于 2019-12-01 10:30:15
I am currently trying to learn how to use unit testing, and I have created the actual list of 3 animal objects and the expected list of 3 animal objects. The question is how do I Assert to check the lists are equal? I have tried CollectionAssert.AreEqual and Assert.AreEqual but to no avail. Any help would be appreciated. The test method: [TestMethod] public void createAnimalsTest2() { animalHandler animalHandler = new animalHandler(); // arrange List<Animal> expected = new List<Animal>(); Animal dog = new Dog("",0); Animal cat = new Cat("",0); Animal mouse = new Mouse("",0); expected.Add(dog);

高精度模板

廉价感情. 提交于 2019-12-01 10:17:46
题目是洛谷 p1005 矩阵取数游戏 很水的题 重要的是高精度的板子 太牛皮了 1 #include <iostream> 2 #include <iomanip> 3 #include <vector> 4 #include <cassert> 5 #include <cstring> 6 #include <string> 7 #include <sstream> 8 9 using namespace std; 10 #define N 82 11 typedef long long LL; 12 13 int MyAtoi(const char* s, const char* t) { 14 int ans = 0; 15 for (; s != t; ++s) 16 (ans *= 10) += *s - '0'; 17 return ans; 18 } 19 20 template<typename T, typename MT> 21 MT Add(T x, MT p) { 22 return x >= p ? x - p : x; 23 } 24 25 const LL tens[] = {1LL,10LL,100LL,1000LL,10000LL,100000LL,1000000LL,10000000LL,100000000LL,1000000000LL}; 26

python 测试 笔记

ぐ巨炮叔叔 提交于 2019-12-01 10:02:59
python 提供了PyUnit用于组织测试,是java junit程序包的一个python版本。 1 断言 断言是在代码中使用的语句,在进行开发时,可以使用它们去测试代码的有效性,如果这个语言的结果不为真,将会引发一个AssertionError错误,如果这个错误未被捕获,那么程序将会停止。 >>> large=1000 >>> assert large>500 >>> assert large<500 Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> assert large<500 AssertionError >>> string="this is a string" >>> assert type(string)==type("") >>> assert type(string)!=type("") Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> assert type(string)!=type("") AssertionError 如果一个称为__debug__的特殊内部变量为True,就检查断言。如果任何一个断言没有成功,就会引发一个AssertionError错误

注意机制CBAM

天涯浪子 提交于 2019-12-01 09:49:38
这是一种用于前馈卷积神经网络的简单而有效的注意模块。 给定一个中间特征图,我们的模块会沿着两个独立的维度(通道和空间)依次推断注意力图,然后将注意力图乘以输入特征图以进行自适应特征修饰。 由于CBAM是轻量级的通用模块,因此可以以可忽略的开销将其无缝集成到任何CNN架构中,并且可以与基础CNN一起进行端到端训练。 为了实现这一目标,我们依次应用频道和空间关注模块(如图1所示),以便每个分支机构都可以分别学习在频道和空间轴上参与的“内容”和“位置”。结果,我们的模块通过学习要强调或抑制的信息来有效地帮助网络中的信息流。将结果先通过通道加权模块,再通过空间位置加权模块 这里对网络做一个实际性的分析, channel attention Module 主要是关注哪些通道对网络的最后输出结果起到作用,即文章中提到的‘什么’,即哪些特征对最终的预测起到了决定性的作用 channel 特征分析,输入通过一个最大值池化和均值池化 最大值池化分析:首先通过对宽度和高度进行最大值池化,然后对特征通道进行全连接,为了减少参数,这里的输出通道为 channel / 8, 下一步再进行全连接,使得输出通道为 channel。 均值池化分析:首先通过对宽度和高度进行均值池化,然后对特征通道进行全连接,为了减少参数,这里的输出通道为channel / 8, 下一步再使用全连接,使得输出通道为channel。