我想处理AssertionError两者以隐藏用户的堆栈跟踪的不必要部分,并打印一条消息,说明错误发生的原因以及用户应该做些什么.
有没有办法找出块assert内失败的行或声明except?
try:
assert True
assert 7 == 7
assert 1 == 2
# many more statements like this
except AssertionError:
print 'Houston, we have a problem.'
print
print 'An error occurred on line ???? in statement ???'
exit(1)
Run Code Online (Sandbox Code Playgroud)
我不想将其添加到每个断言语句中:
assert 7 == 7, "7 == 7"
Run Code Online (Sandbox Code Playgroud)
因为它重复信息.
在我的代码中,我eval用来评估用户给出的字符串表达式.有没有办法编译或以其他方式加快这种说法?
import math
import random
result_count = 100000
expression = "math.sin(v['x']) * v['y']"
variable = dict()
variable['x'] = [random.random() for _ in xrange(result_count)]
variable['y'] = [random.random() for _ in xrange(result_count)]
# optimize anything below this line
result = [0] * result_count
print 'Evaluating %d instances of the given expression:' % result_count
print expression
v = dict()
for index in xrange(result_count):
for name in variable.keys():
v[name] = variable[name][index]
result[index] = eval(expression) # <-- option ONE
#result[index] = math.sin(v['x']) * …Run Code Online (Sandbox Code Playgroud) 如何从__getattr__函数中获取类函数列表?
Python v2.7如果重要的话.
尝试使用dir内部__getattr__导致无限递归.
class Hal(object):
def __getattr__(self, name):
print 'I don\'t have a %s function' % name
names = dir(self) # <-- infinite recursion happens here
print 'My functions are: %s' % ', '.join(names)
exit()
def close_door(self):
pass
x = Hal()
x.open_door()
Run Code Online (Sandbox Code Playgroud)
这是我想要的输出:
I don't have a open_door function
My functions are: close_door, __getattr__, __init__, __doc__, ...
Run Code Online (Sandbox Code Playgroud)
获得我想要的输出的任何其他解决方案都可以正常工作.我希望在不存在函数的情况下进行模糊字符串匹配,以尝试建议用户可能具有的含义.
什么是最有效(及时)检查两个相对较短(约3-8个元素)列表是否是彼此的移位副本的方式?如果是这样,确定并返回偏移量?
这是我想要的示例代码和输出:
>>> def is_shifted_copy(list_one, list_two):
>>> # TODO
>>>
>>> is_shifted_copy([1, 2, 3], [1, 2, 3])
0
>>> is_shifted_copy([1, 2, 3], [3, 1, 2])
1
>>> is_shifted_copy([1, 2, 3], [2, 3, 1])
2
>>> is_shifted_copy([1, 2, 3], [3, 2, 1])
None
>>> is_shifted_copy([1, 2, 3], [1])
None
>>> is_shifted_copy([1, 1, 2], [2, 1, 1])
1
Run Code Online (Sandbox Code Playgroud)
列表可能有重复的条目.如果有多个偏移量有效,则返回任何偏移量.
我有一个枚举列表,定义如下:
enum PinEnum {
kPinInvalid,
kPinA0,
kPinA1,
kPinB0,
kPinB1,
kPinC0,
kPinC1,
}
Run Code Online (Sandbox Code Playgroud)
这些枚举中的每一个都需要与另外两个值相关联,即端口和引脚号.目前,我通过运行时函数访问这些:
GPIO_TypeDef * PinGetPort(const PinEnum pin) {
switch (pin) {
case kPinA0:
case kPinA1:
return GPIOA;
case kPinB0:
case kPinB1:
return GPIOB;
case kPinC0:
case kPinC1:
return GPIOC;
default:
return NULL;
}
}
uint16_t PinGetPin(const PinEnum pin) {
switch (pin) {
case kPinA0:
case kPinB0:
case kPinC0:
return GPIO_Pin_0;
case kPinA1:
case kPinB1:
case kPinC1:
return GPIO_Pin_1;
default:
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
特别是,我这样做是因为我不希望大型查找表在运行时占用RAM(代码大小不是问题).
有没有办法使用编译时查找表,constexpr函数或模板构造来执行此操作,以便语句PinGetPin(kPinA0)和 …
在我的程序中,我经常使用-1(也称为UINT_MAX)作为无符号变量的值来表示特殊的东西.我也比较这个值.打开更高级别的警告消息表明编译器(VS和GCC)不喜欢这种语法.
// warning C4245: 'initializing' : conversion from 'int' to 'unsigned int', signed/unsigned mismatch
unsigned a = -1;
// warning C4146: unary minus operator applied to unsigned type, result still unsigned
unsigned b = -1U;
// fine but messy
unsigned c = unsigned(-1);
// no warning on VS, signed/unsigned comparison warning with GCC
if (a == -1)
{
std::cout << "check\n";
}
Run Code Online (Sandbox Code Playgroud)
问题1:上面给出的语法(每种情况)是合法的C++代码吗?
问题2:我是否真的必须写无符号(-1)我使用此值来分配/比较unsigned int或者是否有更简洁的方法不会触发编译器警告?
如何使用基于范围的循环创建自定义类以循环STL容器中的连续项目对?
这是我想要的语法和输出:
std::list<int> number_list;
number_list.push_back(1);
number_list.push_back(2);
number_list.push_back(3);
auto paired_list = Paired(number_list);
for (const auto & pair : paired_list) {
std::printf("The pair is (%d, %d)\n", *(pair[0]), *(pair[1]));
// or
//std::printf("The pair is (%d, %d)\n", *(pair.first), *(pair.second));
}
// output:
// The pair is (1, 2)
// The pair is (2, 3)
Run Code Online (Sandbox Code Playgroud)
我知道这些(以及更多)是必需的,但我无法弄清楚:
template <class T>
class Paired {
???
class iterator {
???
}
iterator begin() {
...
}
iterator end() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
不要担心const修饰符.
没有提升.
请勿修改或复制容器中的对象.
众所周知,浮点值不能准确表示每个十进制值。因此,1/3 的浮点值不完全是 1/3。因此,通常不建议直接比较浮点值。
但是,在此应用程序中,我试图确定两个分数 a / b 和 c / d 是否相等。如果是,则存在整数 e 和 f 使得a * e = c * f和b * e = d * f。假设 a、b、c、d、e、f 都是正整数,可以精确地用浮点值表示。
在实践中,简单地比较a/b来c/d工作,但它保证工作?Python 和/或 IEEE-754 中有什么东西可以保证这样的方案有效吗?
示例代码(显示此方案适用于合理数量的值):
a = 1.0
b = 3.0
for c in xrange(1, 999):
assert a / b == (a * c) / (b * c)
Run Code Online (Sandbox Code Playgroud)
如果这不能保证,是否有一个反例,其值为 a、b、c、d,使得 a/b = c/d(在数学中)但 Python 无法比较a / b == c / d …
如何以编程方式确定一个函数是否调用另一个函数?我无法修改任何一个功能。
这是我想要的(source_calls_target):
>>> def check():
>>> pass
>>> def test_check():
>>> check()
>>> def source_calls_target(source, target):
>>> # if source() calls target() somewhere, return True
>>> ???
>>> source_calls_target(test_check, check)
True
>>> source_calls_target(check, test_check)
False
Run Code Online (Sandbox Code Playgroud)
理想情况下,我不想实际调用target().
理想情况下,我想检查target()对source. 它可能会或可能不会根据条件语句实际调用它。