小编Mar*_*tin的帖子

什么是Rust的汽车特性?

试图解决Trait bound Sized中描述的问题对于Sized trait不满意,我发现以下代码给出了以下错误:

trait SizedTrait: Sized {
    fn me() -> Self;
}

trait AnotherTrait: Sized {
    fn another_me() -> Self;
}

impl AnotherTrait for SizedTrait + Sized {
    fn another_me() {
        Self::me()
    }
}
Run Code Online (Sandbox Code Playgroud)
error[E0225]: only auto traits can be used as additional traits in a trait object
 --> src/main.rs:9:36
  |
9 | impl AnotherTrait for SizedTrait + Sized {
  |                                    ^^^^^ non-auto additional trait
Run Code Online (Sandbox Code Playgroud)

Rust Book根本没有提到auto trait.

Rust中的自动特性是什么?它与非自动特征有何不同?

rust

15
推荐指数
1
解决办法
3363
查看次数

如何在py.test中将几个参数化夹具连接成一个新的夹具?

如果我有两个参数化夹具,我怎么能创建一个单独的测试函数,首先用一个夹具的实例调用,然后用另一个夹具的实例调用?

我想创建一个以某种方式连接两个现有灯具的新灯具是有意义的.这适用于"普通"灯具,但我似乎无法使用参数化灯具.

这是我尝试过的简化示例:

import pytest

@pytest.fixture(params=[1, 2, 3])
def lower(request):
    return "i" * request.param

@pytest.fixture(params=[1, 2])
def upper(request):
    return "I" * request.param

@pytest.fixture(params=['lower', 'upper'])
def all(request):
    return request.getfuncargvalue(request.param)

def test_all(all):
    assert 0, all
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我收到此错误:

request = <SubRequest 'lower' for <Function 'test_all[lower]'>>

    @pytest.fixture(params=[1, 2, 3])
    def lower(request):
>       return "i" * request.param
E       AttributeError: 'SubRequest' object has no attribute 'param'
Run Code Online (Sandbox Code Playgroud)

...和相同的错误upper().

我做错了什么?

我怎样才能解决这个问题?


更新:

有一个PyTest插件可以用来解决这个问题:https://github.com/TvoroG/pytest-lazy-fixture.

pip-installing这个插件,上述代码的唯一必要的改变如下:

@pytest.fixture(params=[pytest.lazy_fixture('lower'),
                        pytest.lazy_fixture('upper')])
def all(request):
    return request.param
Run Code Online (Sandbox Code Playgroud)

但请注意,与当前的PyTest版本3.6.3不兼容,请参阅 …

python fixtures pytest

10
推荐指数
2
解决办法
3646
查看次数

How to find out `DataFrame.to_numpy` did not create a copy

The pandas.DataFrame.to_numpy method has a copy argument with the following documentation:

copy : bool, default False

Whether to ensure that the returned value is a not a view on another array. Note that copy=False does not ensure that to_numpy() is no-copy. Rather, copy=True ensure that a copy is made, even if not strictly necessary.

Playing around a bit, it seems like calling to_numpy on data that is both adjacent in memory and not of mixed types, keeps a view. But …

python numpy pandas

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

标签 统计

python ×2

fixtures ×1

numpy ×1

pandas ×1

pytest ×1

rust ×1