Python 向另一个类提供一个包含完整对象的数组作为参数,并接收更正后的
我想这样做 我有一个类MyInputs,我将在其中初始化输入 我将有一个numpy数组,它将保存该类的许多实例 比如说,Python 向另一个类提供一个包含完整对象的数组作为参数,并接收更正后的,python,numpy,pytest,Python,Numpy,Pytest,我想这样做 我有一个类MyInputs,我将在其中初始化输入 我将有一个numpy数组,它将保存该类的许多实例 比如说, np.array([[MyInputs('31-01-2017 15:02:13.870916', 100)], [MyInputs('31-01-2017 15:02:13.870916', 20)]], dtype=object) 然后,我将把这个数组提供给另一个类标准,进行一些计算并返回更正后的数组 守则: testStack.py import n
np.array([[MyInputs('31-01-2017 15:02:13.870916', 100)],
[MyInputs('31-01-2017 15:02:13.870916', 20)]], dtype=object)
然后,我将把这个数组提供给另一个类标准,进行一些计算并返回更正后的数组
守则:
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
现在,如果我运行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实例数组,并通过调用replace函数返回正确的数组,例如MyInputs实例的值。这是一个测试==并显示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')
这些对象的数组:
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)
确保==适用于此类对象。如果没有_cmp__)方法,它将求助于对象cmp,该对象仅在id上匹配。在对象数据类型数组中使用==时也要小心。它也可能需要匹配对象ID。testdata[0][3]==testdata[0][4]生成什么?你想要什么?testdata[0][n][0][0]。时间戳匹配。@hpaulj:它产生相同的结果。它比较地址。我想比较初始的MyInputs类和更正的MyInputs类。如果您也能帮助我,我将不胜感激。谢谢!
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)