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

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

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()并传递参数,而无需显式列出它们:

  1. # 在模块B中导入模块A中的Transaction类
  2. from A import Transaction
  3. # 创建Transaction类的实例
  4. collection = Transaction()
  5. class TestTransaction(object):
  6. def __init__(self, path):
  7. # 初始化TestTransaction实例
  8. self.path = path
  9. self.input_1 = None
  10. self.input_2 = None
  11. self.input_3 = None
  12. self.input_merged = None
  13. self.output = None
  14. def import_test_input_data(self):
  15. # 在这里调用模块A的方法import_input_data_x,并传递相应参数
  16. self.input_1 = collection.import_input_data_1()
  17. self.input_2 = collection.import_input_data_2()
  18. self.input_3 = collection.import_input_data_3()
  19. def test_merge_input_data(self):
  20. # 在这里调用模块A的方法merge_input_data
  21. collection.merge_input_data()
  22. def main(self):
  23. # 在main方法中调用import_test_input_data和test_merge_input_data
  24. self.import_test_input_data()
  25. self.test_merge_input_data()

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

英文:

I have the following two modules (simplified):

  1. # Module A
  2. class Transaction:
  3. def __init__(self, path, ):
  4. self.logger = logging.getLogger("Transaction")
  5. logging.basicConfig(level=logging.DEBUG)
  6. self.path = path
  7. self.input_1 = None
  8. self.input_2 = None
  9. self.input_3 = None
  10. self.input_merged = None
  11. self.output = None
  12. def import_input_data_1(self):
  13. self.input_1 = pd.read_excel(
  14. self.path
  15. + country.__data_paths__["collection"]["input"]["transaction"][
  16. "fy2"
  17. ],
  18. index_col=False,
  19. engine="openpyxl",
  20. dtype=object,
  21. )
  22. return self.input_1
  23. def import_input_data_2(self):
  24. self.input_2 = pd.read_excel(
  25. self.path
  26. + country.__data_paths__["collection"]["input"]["transaction"][
  27. "fy1"
  28. ],
  29. index_col=False,
  30. engine="openpyxl",
  31. dtype=object,
  32. )
  33. return self.input_2
  34. def import_input_data_3(self):
  35. self.input_3 = pd.read_excel(
  36. self.path
  37. + country.__data_paths__["collection"]["input"]["transaction"][
  38. "fy"
  39. ],
  40. index_col=False,
  41. engine="openpyxl",
  42. dtype=object,
  43. )
  44. return self.input_3
  45. def merge_input_data(self):
  46. self.input_merged = pd.concat(
  47. [self.input_1, self.input_2, self.input_3]
  48. )
  49. return self.input_merged
  50. def main(self):
  51. self.logger.info("Loading input data 1")
  52. self.input_1 = self.import_input_data_1()
  53. self.logger.info("Loading input data 2")
  54. self.input_2 = self.import_input_data_2()
  55. self.logger.info("Loading input data 3")
  56. self.input_3 = self.import_input_data_3()
  57. self.logger.info("Merging input data")
  58. self.input_merged = self.merge_input_data()
  1. # Module B
  2. from A import Transaction
  3. collection = Transaction()
  4. class TestTransaction(object):
  5. def __init__(self, path):
  6. logging.basicConfig(level=logging.DEBUG)
  7. self.path = path
  8. self.input_1 = None
  9. self.input_2 = None
  10. self.input_3 = None
  11. self.input_merged = None
  12. self.output = None
  13. def import_test_input_data(self):
  14. self.input_1 = pd.read_excel(
  15. self.path
  16. + test.__data_paths__["collection"]["test"]["input"][
  17. "fy2"
  18. ],
  19. index_col=False,
  20. engine="openpyxl",
  21. dtype=object,
  22. )
  23. self.input_2 = pd.read_excel(
  24. self.path
  25. + test.__data_paths__["collection"]["test"]["input"][
  26. "fy1"
  27. ],
  28. index_col=False,
  29. engine="openpyxl",
  30. dtype=object,
  31. )
  32. self.input_3 = pd.read_excel(
  33. self.path
  34. + test.__data_paths__["collection"]["test"]["input"][
  35. "fy"
  36. ],
  37. index_col=False,
  38. engine="openpyxl",
  39. dtype=object,
  40. )
  41. return self.input_1, self.input_2, self.input_3
  42. def test_merge_input_data(self) -> None:
  43. collection.merge_input_data()
  44. def main(self):
  45. self.input_1, self.input_2, self.input_3 = self.import_test_input_data()
  46. 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 来创建属于类的函数,这些函数不需要类的属性。

例如:

  1. # 模块 A
  2. class Collection:
  3. def _merge_input_data(self):
  4. self.input_merged = self.merge_input_data(self.input_1, self.input_2, self.input_3)
  5. @staticmethod
  6. def merge_input_data(*args):
  7. return pd.concat(list(args))
  8. # 模块 B
  9. from 模块A import Collection
  10. class Testing:
  11. def test_merge_input_data(self):
  12. 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:

  1. # Module A
  2. class Collection:
  3. def _merge_input_data(self):
  4. self.input_merged = self.merge_input_data(self.input_1, self.input_2, self.input_3)
  5. @staticmethod
  6. def merge_input_data(*args):
  7. return pd.concat(list(args))
  8. # Module B
  9. from Module A import Collection
  10. class Testing:
  11. def test_merge_input_data(self):
  12. 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()方法中准备好了这些数据。

你应该使用这些数据:

  1. def test_merge_input_data(self) -> None:
  2. self.import_test_input_data()
  3. collection.input_1 = self.input_1
  4. collection.input_2 = self.input_2
  5. collection.input_3 = self.input_3
  6. 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:

  1. def test_merge_input_data(self) -> None:
  2. self.import_test_input_data():
  3. collection.input_1 = self.input_1
  4. collection.input_2 = self.input_2
  5. collection.input_3 = self.input_3
  6. 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:

确定