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

会有一股神秘感。 提交于 2019-12-25 18:36:34

问题


I want to do this.

I have a class (MyInputs) where I will initialize the inputs.

I will have a numpy array which will hold many instances of the class.

For example,

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

I will then supply this array to another class (Criteria), make some computations and return back the corrected array.

The code :

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

Right now, if I run the pytest , I am receiving:

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           ?                                                ^  ++)

because it compares the addresses and not the values.

I just can't think how to organize this to be correct.

And by correct, I mean supply an array of MyInputs instances and return back the corrected array ( correct for example the value of the MyInputs instance by calling replace function).


回答1:


This is a simple class that tests for == (and displays self):

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

an array of these objects:

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)


来源:https://stackoverflow.com/questions/41985458/supply-an-array-full-with-objects-as-argument-to-another-class-and-receive-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!