简体   繁体   English

如何在主模块中连接输入和功能模块,我们可以从函数返回列表吗?

[英]how to connect input and function module in main module and can we return list from a function?

I have three modules: GetInput, Main and Converter.我有三个模块:GetInput、Main 和 Converter。 In the GetInput file there are all the inputs values and excel data in the form of list.在 GetInput 文件中有所有输入值和列表形式的 excel 数据。 In the Converter file I am using those input values from Getinput file and in the main file I am connecting both these files here.在转换器文件中,我使用来自 Getinput 文件的那些输入值,而在主文件中,我在这里连接了这两个文件。 I am doing this so that my code can look more organized.我这样做是为了让我的代码看起来更有条理。 GetInput.py:获取输入.py:

import pandas as pd
import numpy as np
import time

def getInputs():
    df = pd.read_excel('input.xlsx')
    actual = df['actual'].values.tolist()
    schedule = df['schedule'].values.tolist()
    freq = df['frequency'].values.tolist()
    ACP = df['acp'].values.tolist()


    modelInput = {
        'actual': actual, 'schedule': schedule, 'freq': freq, 'ACP': ACP,'df' : df
    }
    return modelInput

Converter.py转换器.py

import pandas as pd
def fun(modelInput):
    underdraw = []
    overdraw = []
    for i,j, in zip(schedule, actual):
        dev = j - i
        if dev < 0:
            underdraw.append(dev)
        else:
            underdraw.append(0)

        if dev > 0:
            overdraw.append(dev)
        else:
            overdraw.append(0)

    df['underdraw'] = pd.Series(underdraw)
    df['overdraw'] = pd.Series(overdraw)
    df.to_excel('mainfile.xlsx')

Main.py主文件

import pandas as pd
import numpy as np
from convert import *
from GetInputs import *

def fun1():
    inpu = getInputs()
    con = fun(inpu)
fun1()

This whole program works when I run it in a single module but it throw errors when I try divide my code into separate modules.当我在单个模块中运行它时,整个程序都可以工作,但是当我尝试将代码分成单独的模块时,它会抛出错误。 Basically it throw error in GetInput.py and in Converter.py (df is not defined) file.基本上它会在GetInput.pyConverter.py (未定义 df)文件中抛出错误。 I know its a very basic thing but I don't know how to make it work.我知道这是一个非常基本的事情,但我不知道如何让它发挥作用。 There is no desired output for this program, I am already getting an output when I run it in a single file.该程序没有所需的输出,当我在单个文件中运行它时,我已经得到了输出。 I just want to divide my code in this format as I mentioned above: GetIput File, Converter File and Main File.我只想按照上面提到的这种格式划分我的代码:GetIput File、Converter File 和 Main File。

Keep all the files in same directory or else mention the file paths at the top of main code using os module.将所有文件保存在同一目录中,或者使用os模块在主代码顶部提及文件路径。

You have misspelled the following in the main code:您在主代码中拼错了以下内容:

from convert import *
from GetInputs import *

It should be:它应该是:

from Converter import *
from GetInput import *

I have tested this using the following:我已经使用以下方法对此进行了测试:

MainModule.py主模块.py

from Converter import *
from GetInputs import *

def fun1():
    inpu = getInputs()
    con = fun(inpu)
fun1()

Converter.py转换器.py

import pandas as pd
def fun(modelInput):
    print("HIE" + modelInput)

GetInputs.py获取输入文件

def getInputs():
    return "modelInput"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM