我正在尝试从其他模块访问主应用程序中的初始化类,但不知道如何操作。 背景:我想在主应用程序的整个执行过程中用数据更新数据帧。你知道吗
我必须遵循以下应用程序结构(这是我应用程序中代码的简化版本):
constraints
- test_function.py (separate module which should be able to update the initialized class in the main app)
functions
- helper.py (the class which contains the dataframe logic)
main.py (my main application code)
你知道吗主.py地址:
import functions.helper
gotstats = functions.helper.GotStats()
gotstats.add(solver_stat='This is a test')
gotstats.add(type='This is a test Type!')
print(gotstats.result())
import constraints.test_function
constraints.test_function.test_function()
你知道吗助手.py地址:
class GotStats(object):
def __init__(self):
print('init() called')
import pandas as pd
self.df_got_statistieken = pd.DataFrame(columns=['SOLVER_STAT','TYPE','WAARDE','WAARDE_TEKST','LOWER_BOUND','UPPER_BOUND','OPTIMALISATIE_ID','GUROBI_ID'])
def add(self,solver_stat=None,type=None,waarde=None,waarde_tekst=None,lower_bound=None,upper_bound=None,optimalisatie_id=None,gurobi_id=None):
print('add() called')
self.df_got_statistieken = self.df_got_statistieken.append({'SOLVER_STAT': solver_stat,'TYPE': type, 'WAARDE': waarde, 'OPTIMALISATIE_ID': optimalisatie_id, 'GUROBI_ID': gurobi_id}, ignore_index=True)
def result(self):
print('result() called')
df_got_statistieken = self.df_got_statistieken
return df_got_statistieken
试验_函数.py地址:
import sys, os
sys.path.append(os.getcwd())
def test_function():
import functions.helper
gotstats = functions.helper.GotStats()
gotstats.add(solver_stat='This is a test from the seperate module')
gotstats.add(type='This is a test type from the seperate module!')
print(gotstats.result())
if __name__ == "__main__":
test_function()
在main中,我用“gotstats=函数.helper.GotStats()". 之后,我可以正确地使用它的函数,并使用add函数添加dataframe行。 我希望test\u function()能够将数据帧行添加到同一个对象中,但我不知道如何做到这一点(在当前的测试代码中)_函数.py只是在它的本地名称空间中创建了一个新类(我不希望这样)。我是否需要用一个函数来扩展类对象以获得活动的对象(比如日志记录.getLogger(名称)?你知道吗
任何方向正确的帮助都将不胜感激。你知道吗
使您的
test_function
接受实例作为参数,并在调用时将其传递给函数:你知道吗主.py地址:
试验_函数.py地址:
相关问题 更多 >
编程相关推荐