小编Jas*_*sha的帖子

基于C++范围的for loop over valarray rvalue不起作用

我想迭代一个临时的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)

c++ for-loop rvalue temporary-objects valarray

15
推荐指数
2
解决办法
1240
查看次数

当异常链接时,如何使用 pytest 测试 python 3 中的异常

我有一些在 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

任何帮助都会很棒。

pytest python-3.x

7
推荐指数
1
解决办法
853
查看次数

如何将特定类型的 numpy 数组声明为数据类中的类型

我所拥有的:
我正在创建一个数据类,并说明其元素的类型:

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 数组

python-3.x numpy-ndarray python-dataclasses

6
推荐指数
1
解决办法
1万
查看次数

与使用常规配置文件相比,Hydra 的优势是什么

我想知道使用 Hydra 管理我的配置文件与直接加载 .yaml 配置文件(使用 import yaml)相比有什么优势?

python configuration-files hydra fb-hydra

4
推荐指数
1
解决办法
4104
查看次数

在显式子类型构造期间未调用typing.Protocol 类`__init__` 方法

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)

正如所料,具体子类Concrete1Concrete2继承method从他们各自的超类。此行为记录在PEP的显式声明实现部分:

要明确声明某个类实现了给定的协议,可以将其用作常规基类。在这种情况下,类可以使用协议成员的默认实现。

...

请注意,显式和隐式子类型之间几乎没有区别,显式子类化的主要好处是“免费”获得一些协议方法。

然而,当协议类实现__init__的方法,__init__是 …

protocols abstract-base-class python-3.x python-typing

3
推荐指数
1
解决办法
418
查看次数

std::filesystem::exists -- 如果函数返回 true,我是否需要检查 std::error_code 值?

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)ecfalse

例如,假设我有以下内容:

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) { ... }

c++ error-code c++17

2
推荐指数
1
解决办法
3196
查看次数