如何定义一个可以由类方法使用的函数?

问题描述 投票:-1回答:1

我有某些代码行,我在代码中使用了两次。所以我试图定义一个函数,以避免两次使用相同的代码。我的问题是如何定义类方法将在报表中打印结果的函数?

这意味着TPT是一个类,而setTestComment和getTestComment是类方法。 setTestComment将设置注释,该注释将由getTestComment接受以在报告中打印注释。

在这里,我不知道如何实现类及其功能。请查看代码以更好地理解。

这是我尝试过的:

我试图构建自己的函数,该函数返回注释。但是我被困在这里。我该如何使自己的函数通过setTestComment方法获取设置的注释,而该注释应由该类的getTestComment方法获取,以打印在报告中。

我知道很难理解,如果您看到代码,就会有个主意。

试图构建自己的功能。

gr,grintern和triggeracceleration是全局变量。

evaluation = true;

def evaluation_acc(ACC_MEAN_3Tests, ACC_MEAN_LENGTH_3Tests):

  if(ACC_MEAN_3Tests > gr(ACC_MEAN_LENGTH_3Tests)):
    TPT.setTestComment("The mean acceleration exceeds the EGAS limit acceleration curve by "+str(ACC_MEAN_3Tests-gr(ACC_MEAN_LENGTH_3Tests)) + " m/s^2");
    evaluation = false;
  else:
    if(ACC_MEAN_3Tests > grintern( ACC_MEAN_LENGTH_3Tests)):
      TPT.setTestComment("The mean acceleration exceeds the process objective limit acceleration curve by "+str(ACC_MEAN_3Tests-grintern(ACC_MEAN_LENGTH_3Tests)) + " m/s^2. Nevertheless, the mean acceleration stays " + str(gr(ACC_MEAN_LENGTH_3Tests)-ACC_MEAN_3Tests) + " m/s^2 below the EGAS limit curve. ");
    else:
      TPT.setTestComment("The mean acceleration stays within the EGAS and the process objective limit acceleration curve");
  if ACC_MEAN_LENGTH_3Tests ==0:
    TPT.setTestComment("No accelerations >"+ str(triggerAcceleration) +" m/s^2 occured."); 

正在使用两次的当前代码

  #Evaluation of acceleration over 3 test cycles. Acceleration > limit curve value
evaluation = true;
if(test.acc_mean() > gr(test.acc_mean_length())):
  TPT.setTestComment("The mean acceleration exceeds the EGAS limit acceleration curve by "+str(test.acc_mean()-gr(test.acc_mean_length())) + " m/s^2");
  evaluation = false;
else:
  if(test.acc_mean() > grintern( test.acc_mean_length())):
    TPT.setTestComment("The mean acceleration exceeds the process objective limit acceleration curve by "+str(test.acc_mean()-grintern(test.acc_mean_length())) + " m/s^2. Nevertheless, the mean acceleration stays " + str(gr(test.acc_mean_length())-test.acc_mean()) + " m/s^2 below the EGAS limit curve. ");
  else:
    TPT.setTestComment("The mean acceleration stays within the EGAS and the process objective limit acceleration curve");
if test.acc_mean_length()==0:
  TPT.setTestComment("No accelerations >"+ str(triggerAcceleration) +" m/s^2 occured.");

在报告中打印注释的代码

TPTReport.add("summary", TPT.getTestComment());

我需要一个提示,以通过定义函数来实现。可能是通过将setTestComment作为参数传递给我自己的函数!但我不确定

随时询问更多信息

提前感谢

python
1个回答
0
投票

您可以执行以下操作:“我的问题是,如何通过类方法定义一个函数以在报告中打印结果”,但是从我的角度来看,这不是很好的代码设计,我会给方法一些参数并创建ifif else结构。无论如何,代码是,请遵循注释并提出问题:

class Toy:
    def __init__(self, a, b, c):  # some random class
        self.a = a
        self.b = b
        self.c = c

    def print_smth(self, f):  # your method, which take function as an argument
        print(f(self.a, self.b, self.c))


def print_one(a, b, c):
    return "{}__{}__{}".format(a, b, c)  # first way to print


def print_two(a, b, c):
    return "{}***{}***{}".format(a, b, c)  # second way to  print


if __name__ == "__main__":
    # tests:
    x = Toy(1, 2, 3)
    x.print_smth(print_one)
    x.print_smth(print_two)

© www.soinside.com 2019 - 2024. All rights reserved.