将类实例的输出作为输入传递给另一个
我有此代码:
import numpy as np
class B():
def __init__(self, a,b):
self.a = a
self.b = b
class Criteria():
def __init__(self, method, minimum, maximum, measures=None):
self.method = method
self.minimum = minimum
self.maximum = maximum
self.measures = measures if measures is not None else None
def calcs(self):
if self.measures is not None:
for x in self.measures:
if (x.a > self.minimum and x.a < self.maximum):
x.a = 999
return self.measures
def avg(self):
if self.measures is not None:
return np.average([x.value for x in self.measures])
else:
return np.average(3)# Here should be the result where None is defined
# Now I put just an arbitrary number
class Evaluate():
def __init__(self, criteria):
self.criteria = criteria
testdata = np.array([Evaluate(
np.array([Criteria('V', 1,100,
np.array([B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)],dtype=object)
),
Criteria('AVG', 22, 220, None)])
)])
for x in testdata:
for idx, el in enumerate(x.criteria):
if el.method == 'V':
el.calcs() # this result must be passed as input to the `el.avg()`
if el.method == 'AVG':
el.avg()
我有一个B
类,其中包含一些数据(a和b字段).
I have a class B
which holds some data (a and b fields).
我正在将这些数据加载到Criteria
类中,以便通过标准并进行相应的更改.
I am loading these data to the Criteria
class in order to pass through the criteria and change accordingly.
然后,Evaluate
类将包含以上所有内容.
Then, the Evaluate
class will hold all the above.
我在Criteria类中使用了measures=None
,因为在例如avg
函数的情况下,我可能没有度量来计算平均值,但是我可能(这是我的情况)我在上面应用平均值的前一个Criteria类.
I am using measures=None
to the Criteria class because in the case for example for the avg
function , I may have not measurements to calculate on them the average, but I may have (this is my case) measurements from previous Criteria class on which I am applying the average.
现在,我要做的就是这个.
Now, what I want to accomplish is this.
首先加载数据:
B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)
通过传递条件(通过运行calcs
函数),这些数据将移至:
These data, by passing the criteria (by running the calcs
function) , will chage to :
B(100, 0.1),
B(999, 0.3),
B(300, 0.2),
B(999, 0.1)
现在,上述数据(第一个条件的结果/输出,必须作为输入传递给第二个条件,并使用avg
函数计算平均值.我不知道这是否可行)在avg
函数中没有任何参数,只需使用self
.
Now, the above data (which is the result/output from the first criteria, must be passed as input to the second criteria and compute the average value using the avg
function.I don't know if this is possible without having any argument in the avg
function . Just have the self
.
所以,我的最终结果将是值599.5.
So, my finaly result will be the value 599.5.
这是对脚本的修改.主要是我添加了repr
.但我也将measures
的None
大小写更改为一个空列表[]
:
Here's a modification of your script. Mainly I added repr
. But I also changed the None
case for measures
to an empty list []
:
import numpy as np
class B():
def __init__(self, a,b):
self.a = a
self.b = b
def __repr__(self):
return 'B(%s, %s)'%(self.a, self.b)
class Criteria():
def __init__(self, method, minimum, maximum, measures=None):
self.method = method
self.minimum = minimum
self.maximum = maximum
self.measures = measures # may be None
def __repr__(self):
# **edit** work with None
if self.measures is None:
measures = 'measures: None'
else:
measures = [' measures:[']
for m in self.measures:
measures.append(' {}'.format(m))
measures.append(' ]')
measures = '\n'+ '\n'.join(measures)
return 'C({0.method},{0.minimum},{0.maximum}, {1})'.format(self, measures)
def calcs(self):
if self.measures is not None:
for x in self.measures:
if (x.a > self.minimum and x.a < self.maximum):
x.a = 999
return self.measures
def avg(self, calcs=None):
# **edit** work with None and calcs
if calcs is None:
calcs = self.measures
if calcs is None:
return 'none'
elif len(calcs)==0:
return '[]'
else:
return np.average([x.a for x in calcs])
class Evaluate():
def __init__(self, criteria):
self.criteria = criteria
def __repr__(self):
#return 'E({})'.format(self.criteria)
astr = 'Evaluate \n'
for c in self.criteria:
astr += '{}\n'.format(c)
return astr
考虑制作一组Criteria
对象. AVG
必须以某种方式知道它使用哪个measures
.一种方法是在构造过程中使用measures
参数.
Consider making a group of Criteria
objects. An AVG
has to know, in some way or other, which measures
it uses. One way is the measures
parameter used during construction.
b1 = np.array([B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)],dtype=object)
b2 = np.array([B(1, 0.1), B(2,.5)])
c1 = Criteria('V', 1, 100, b1)
c2 = Criteria('V', 2, 200, b2)
c3 = Criteria('AVG', 22, 220, None)
c4 = Criteria('AVG', 22, 220, c2.measures)
c5 = Criteria('AVG', 22, 222, c1.measures)
编辑更改迭代以保存最后的calcs
结果,并在AVG
度量为无"时使用该结果. C.avg
现在带有一个可选参数.
edit change the iteration to save the last calcs
result, and use that if the AVG
measures is None. C.avg
now takes an optional parameter.
last_calcs = None
for c in alist:
if c.method=='V':
last_calcs = c.calcs()
print('calcs', c.measures)
if c.method=='AVG':
if c.measures is None:
avg = c.avg(last_calcs)
else:
avg = c.avg()
print('AVG', avg)
具有:
alist = [c3,c1,c3,c2,c3,c4, c5]
这将产生:
evaluate
AVG none # c3 with nothing preceeding
calcs [B(100, 0.1) B(999, 0.3) B(300, 0.2) B(999, 0.1)]
AVG 599.5 # c3 following c1
calcs [B(1, 0.1) B(2, 0.5)]
AVG 1.5 # c3 following c2
AVG 1.5 # c4 with same measures as c2
AVG 599.5 # c5 with same measures as c1