如何从另一个模块调用的方法传递参数?

huangapple go评论75阅读模式
英文:

How to pass arguments to a method called from another module?

问题

使用模块B时,我想从模块A调用方法merge_input_data(),将当前B实例中的参数(self.input_1, self.input_2, self.input_3)传递给A中的方法,并执行带有这些参数的merge_input_data()功能。然而,我一直得到以下错误:

> All objects passed were None

我想问一下是否有一种方法可以调用其他模块/类的方法,而不必显式列出它们所有要传递的参数,例如 merge_input_data(self.input_1, self.input_2, self.input_3)?这是因为模块A中的方法通常只接受self作为参数,因为它在A的实例中提供,但我想从另一个模块B中访问它。谢谢!


可以通过以下方式在模块B中调用模块A的方法merge_input_data()并传递参数,而无需显式列出它们:

# 在模块B中导入模块A中的Transaction类
from A import Transaction

# 创建Transaction类的实例
collection = Transaction()

class TestTransaction(object):

    def __init__(self, path):
        # 初始化TestTransaction实例
        self.path = path
        self.input_1 = None
        self.input_2 = None
        self.input_3 = None
        self.input_merged = None
        self.output = None

    def import_test_input_data(self):
        # 在这里调用模块A的方法import_input_data_x,并传递相应参数
        self.input_1 = collection.import_input_data_1()
        self.input_2 = collection.import_input_data_2()
        self.input_3 = collection.import_input_data_3()

    def test_merge_input_data(self):
        # 在这里调用模块A的方法merge_input_data
        collection.merge_input_data()

    def main(self):
        # 在main方法中调用import_test_input_data和test_merge_input_data
        self.import_test_input_data()
        self.test_merge_input_data()

这种方式允许您在模块B中调用模块A的方法,并使用模块A实例的方法来处理相应的参数,而无需显式传递它们。

英文:

I have the following two modules (simplified):

# Module A
class Transaction:

    def __init__(self, path, ):

        self.logger = logging.getLogger("Transaction")

        logging.basicConfig(level=logging.DEBUG)
        self.path = path
        self.input_1 = None
        self.input_2 = None
        self.input_3 = None
        self.input_merged = None
        self.output = None

    def import_input_data_1(self):
        self.input_1 = pd.read_excel(
            self.path
            + country.__data_paths__["collection"]["input"]["transaction"][
                "fy2"
            ],
            index_col=False,
            engine="openpyxl",
            dtype=object,
        )
        return self.input_1

    def import_input_data_2(self):
        self.input_2 = pd.read_excel(
            self.path
            + country.__data_paths__["collection"]["input"]["transaction"][
                "fy1"
            ],
            index_col=False,
            engine="openpyxl",
            dtype=object,
        )
        return self.input_2

    def import_input_data_3(self):
        self.input_3 = pd.read_excel(
            self.path
            + country.__data_paths__["collection"]["input"]["transaction"][
                "fy"
            ],
            index_col=False,
            engine="openpyxl",
            dtype=object,
        )
        return self.input_3

    def merge_input_data(self):
        self.input_merged = pd.concat(
            [self.input_1, self.input_2, self.input_3]
        )
        return self.input_merged

    def main(self):
        self.logger.info("Loading input data 1")
        self.input_1 = self.import_input_data_1()

        self.logger.info("Loading input data 2")
        self.input_2 = self.import_input_data_2()

        self.logger.info("Loading input data 3")
        self.input_3 = self.import_input_data_3()

        self.logger.info("Merging input data")
        self.input_merged = self.merge_input_data()
# Module B
from A import Transaction
collection = Transaction()

class TestTransaction(object):

    def __init__(self, path):

        logging.basicConfig(level=logging.DEBUG)
        self.path = path
        self.input_1 = None
        self.input_2 = None
        self.input_3 = None
        self.input_merged = None
        self.output = None

    def import_test_input_data(self):
        self.input_1 = pd.read_excel(
            self.path
            + test.__data_paths__["collection"]["test"]["input"][
                "fy2"
            ],
            index_col=False,
            engine="openpyxl",
            dtype=object,
        )

        self.input_2 = pd.read_excel(
            self.path
            + test.__data_paths__["collection"]["test"]["input"][
                "fy1"
            ],
            index_col=False,
            engine="openpyxl",
            dtype=object,
        )

        self.input_3 = pd.read_excel(
            self.path
            + test.__data_paths__["collection"]["test"]["input"][
                "fy"
            ],
            index_col=False,
            engine="openpyxl",
            dtype=object,
        )

        return self.input_1, self.input_2, self.input_3

    def test_merge_input_data(self) -> None:
        collection.merge_input_data()

def main(self):

        self.input_1, self.input_2, self.input_3 = self.import_test_input_data()

        self.input_1, self.input_2, self.input_3 = self.merge_input_data()

Using module B, I want to call the method merge_input_data() from module A, pass arguments from my current instance in B (self.input_1, self.input_2, self.input_3) to the method in A and execute the functionality of merge_input_data() with these arguments. However, I keep getting the error:

> All objects passed were None

I want to ask if there is a way to call methods from other modules / classes without them having all to-be-passed arguments explicitly listed, e.g. merge_input_data(self.input_1, self.input_2, self.input_3)? This is because the method from module A usually only takes self as an argument due to it being provided in the instance of A itself, but I want to access it from another module B. Thanks!

答案1

得分: 2

你可以使用 @staticmethod 来创建属于类的函数,这些函数不需要类的属性。

例如:

# 模块 A
class Collection:
    def _merge_input_data(self):
        self.input_merged = self.merge_input_data(self.input_1, self.input_2, self.input_3)

    @staticmethod
    def merge_input_data(*args):
        return pd.concat(list(args))

# 模块 B

from 模块A import Collection

class Testing:
    def test_merge_input_data(self):
        result = Collection.merge_input_data(FIRST, SECOND, THIRD, ...)

我已经添加了函数 Collection._merge_input_data,它可以用于类的实例。但是你没有提供所有的代码,所以我不确定你是否从你的类中使用了这个函数。

另外,在模块 B 中,你应该提供一个输入列表来进行合并。

英文:

You can use @staticmethod to create function belonging to a class which do not need the attributes of a class.

For example:

# Module A
class Collection:
    def _merge_input_data(self):
        self.input_merged = self.merge_input_data(self.input_1, self.input_2, self.input_3)

    @staticmethod
    def merge_input_data(*args):
        return pd.concat(list(args))

# Module B

from Module A import Collection

class Testing:
    def test_merge_input_data(self):
        result = Collection.merge_input_data(FIRST, SECOND, THIRD, ...)

I have add the function Collection._merge_input_data which can be used for an instance of the class. But you did not provide all the code so I am not sure of you used this function from your class or not.

Also in module B you should provide a list with inputs to merge.

答案2

得分: 1

看起来你想要用测试数据替换真实的数据,这是class Transaction在其import_input_data_x()方法中收集的数据。

看起来你已经在测试类中的import_test_input_data()方法中准备好了这些数据。

你应该使用这些数据:

def test_merge_input_data(self) -> None:
    self.import_test_input_data()
    collection.input_1 = self.input_1
    collection.input_2 = self.input_2
    collection.input_3 = self.input_3
    collection.merge_input_data()
英文:

It looks like you are wanting to substitute test data for the actual data that the real class Transaction: collects in its import_input_data_x() methods.

Its looks like you've prepared this data in import_test_input_data() in your test class.

You should use this data:

def test_merge_input_data(self) -> None:
    self.import_test_input_data():
    collection.input_1 = self.input_1
    collection.input_2 = self.input_2
    collection.input_3 = self.input_3
    collection.merge_input_data()

huangapple
  • 本文由 发表于 2023年1月9日 19:54:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定