提供一个包含对象的数组作为另一个 class 的参数并接收更正后的结果

supply an array full with objects as argument to another class and receive back the corrected

我想做这个。

我有一个 class (MyInputs),我将在其中初始化输入。

我将有一个 numpy 数组,它将包含 class 的许多实例。

例如,

np.array([[MyInputs('31-01-2017 15:02:13.870916', 100)],
         [MyInputs('31-01-2017 15:02:13.870916', 20)]], dtype=object)

然后我会将此数组提供给另一个 class(标准),进行一些计算并 return 返回更正后的数组。

代码:

testStack.py

import numpy as np


class MyInputs():

    def __init__(self, timestamp, value):
        self.timestamp = timestamp
        self.value = value


class Criteria():

    def __init__(self,
                 minimum,
                 maximum,
                 new_value,
                 myinputs):

        self.minimum = minimum
        self.maximum = maximum
        self.new_value = new_value
        self.myinputs = myinputs


    def A(self):
        minimum, maximum, new_value, myinputs = \
        self.minimum, self.maximum, self.new_value, self.myinputs

        for index, i in np.ndenumerate(myinputs):
            if  (myinputs[index].value < minimum or
                    myinputs[index].value > maximum):
                self.replace(new_value)

        return myinputs

    def replace(self, new_value):
        minimum, maximum, new_value, myinputs = \
        self.minimum, self.maximum, self.new_value, self.myinputs


        return new_value

testStack_test.py

import unittest
import pytest
import numpy as np

from testStack import Criteria, MyInputs

class TestCriteria():

    testdata = [
        (34, 120, 34,
         np.array([[MyInputs('31-01-2017 15:02:13.870916', 100)],
                   [MyInputs('31-01-2017 15:02:13.870916', 20)]], dtype=object),

         np.array([[MyInputs('31-01-2017 15:02:13.870916', 100)],
                   [MyInputs('31-01-2017 15:02:13.870916', 34)]], dtype=object)),


    ]

    @pytest.mark.parametrize(
        "minimum, maximum, new_value, myinputs, expected_output", testdata)
    def test_criteria_returns_correct_results(
            self, minimum, maximum, new_value, myinputs, expected_output):

        obs = Criteria(minimum, maximum, new_value, myinputs).A()

        assert np.all(obs == expected_output)


if __name__ == "__main__":
    unittest.main()

现在,如果我 运行 pytest,我收到:

assert False
E        +  where False = <function all at 0x7f57d2a54d08>(array([[<test... dtype=object) == array([[<testS... dtype=object)
E        +    where <function all at 0x7f57d2a54d08> = np.all
E           Full diff:
E           - array([[<testStack.MyInputs object at 0x7f57d2b9f438>],
E           ?                                                  ^^
E           + array([[<testStack.MyInputs object at 0x7f57d2b9f518>],
E           ?                                                  ^^
E           -        [<testStack.MyInputs object at 0x7f57d2b9f4a8>]], dtype=object)
E           ?                                                ^ --
E           +        [<testStack.MyInputs object at 0x7f57d2b1f860>]], dtype=object)
E           ?                                                ^  ++)

因为它比较的是地址而不是值。

我只是想不出如何正确组织它。

我所说的正确是指提供一个 MyInputs 实例数组,然后 return 返回更正后的数组(例如,通过调用 replace 函数来更正 MyInputs 实例的值)。

这是一个简单的 class 测试 ==(并显示自我):

class Foo():
    def __init__(self,a,b):
        self.a=a
        self.b=b
    def __repr__(self):
        return str((self.a,self.b))
    def __eq__(self, other):
        if isinstance(other, Foo):
            return (self.a==other.a) and (self.b==other.b)
        else:
            False

In [43]: A=Foo(1,'a')
In [44]: B=Foo(2,'a')
In [45]: A==B
Out[45]: False
In [46]: B.a=1    # change values to match; diff ids
In [47]: A==B
Out[47]: True
In [48]: A
Out[48]: (1, 'a')
In [49]: B
Out[49]: (1, 'a')

这些对象的数组:

In [51]: arr = np.array([Foo(1,'a'),Foo(2,'b'),Foo(1,'a')])
In [52]: arr==arr
Out[52]: array([ True,  True,  True], dtype=bool)
In [54]: arr==A
Out[54]: array([ True, False,  True], dtype=bool)
In [58]: arr
Out[58]: array([(1, 'a'), (2, 'b'), (1, 'a')], dtype=object)