我想迭代一个临时的valarray,但它不起作用.这是我的(非工作)代码:
#include <iostream>
#include <valarray>
int main()
{
using namespace std;
valarray<int> numerators = {99, 26, 25};
valarray<int> denominators = {9, 2, 5};
for (int i : numerators / denominators) { cout << i << ","; }
// lots of errors
return 0;
}
Run Code Online (Sandbox Code Playgroud)
下面是我想要实现的最小工作示例,除了我不想定义像这样的对象temp_array
.
#include <iostream>
#include <valarray>
int main()
{
using namespace std;
valarray<int> numerators = {99, 26, 25};
valarray<int> denominators = {9, 2, 5};
valarray<int> && temp_array = numerators / denominators;
for (int i : …
Run Code Online (Sandbox Code Playgroud) 我有一些在 python 3 中使用自定义异常的代码。
就像是:
def get_my_custom_error():
try:
1.0/0.0
except ZeroDivisionError as err:
raise MyCustomError() from err
Run Code Online (Sandbox Code Playgroud)
在我的测试文件中我有以下内容:
with pytest.raises(MyCustomError):
get_my_custom_error()
Run Code Online (Sandbox Code Playgroud)
我目前得到的输出如下
ZeroDivisionError
the above exception was the direct cause of the following error:
MyCustomError
Run Code Online (Sandbox Code Playgroud)
这会导致测试失败。
所以代码似乎可以工作,但 pytest 似乎没有检查最高级别的错误(这就是我希望它做的)。
Python 3.6.1 :: Anaconda 4.4.0 pytest 3.0.7
任何帮助都会很棒。
我所拥有的:
我正在创建一个数据类,并说明其元素的类型:
class Task():
n_items: int
max_weight: int
max_size: int
items: numpy.array(Item) # incorrect way of doing it
Run Code Online (Sandbox Code Playgroud)
我想做的事情是
我想声明,该items将是“Item”类的对象的 numpy 数组
我想知道使用 Hydra 管理我的配置文件与直接加载 .yaml 配置文件(使用 import yaml)相比有什么优势?
Python 的PEP 544引入typing.Protocol
了结构子类型,也就是“静态鸭子类型”。
在此 PEP 关于合并和扩展协议的部分中,指出
一般的哲学是协议大多像常规的 ABC,但静态类型检查器会专门处理它们。
因此,人们期望从 的子类继承与期望从 的子类继承typing.Protocol
的方式大致相同abc.ABC
:
from abc import ABC
from typing import Protocol
class AbstractBase(ABC):
def method(self):
print("AbstractBase.method called")
class Concrete1(AbstractBase):
...
c1 = Concrete1()
c1.method() # prints "AbstractBase.method called"
class ProtocolBase(Protocol):
def method(self):
print("ProtocolBase.method called")
class Concrete2(ProtocolBase):
...
c2 = Concrete2()
c2.method() # prints "ProtocolBase.method called"
Run Code Online (Sandbox Code Playgroud)
正如所料,具体子类Concrete1
和Concrete2
继承method
从他们各自的超类。此行为记录在PEP的显式声明实现部分:
要明确声明某个类实现了给定的协议,可以将其用作常规基类。在这种情况下,类可以使用协议成员的默认实现。
...
请注意,显式和隐式子类型之间几乎没有区别,显式子类化的主要好处是“免费”获得一些协议方法。
然而,当协议类实现__init__
的方法,__init__
是 …
std::filesystem::exists用于检查“给定的文件状态或路径是否对应于现有文件或目录”。在我的代码中,我使用具有以下签名的定义:
bool exists( const std::filesystem::path& p, std::error_code& ec ) noexcept;
Run Code Online (Sandbox Code Playgroud)
我的问题是:如果函数返回布尔值true
,我还需要检查错误代码的值吗ec
?或者我可以假设,如果std::filesystem::exists
返回true
,则没有错误并且(bool)ec
是false
?
例如,假设我有以下内容:
std::error_code ec;
std::filesystem::path fpath = "fname";
bool does_exist = std::filesystem::exists(fpath, ec);
if (does_exist) {
...
}
Run Code Online (Sandbox Code Playgroud)
是否需要在区块(bool)ec == false
内进行检查if (does_exist) { ... }
?