简体   繁体   中英

python passing function variables to another function

I am trying to pass a variable from one function into my main engine function in a separate file. I have looked up other answers and gone through some but I can't seem to wrap my head around it..

load_file.py

def get_connection():
    cursor = connection.cursor()
    cursor.execute(
        "SELECT ID, Type, Server, Port, User, Password, isActive, FileExtension, FileContains, FileLocation, "
    "ScheduleMinutes, IntervalTime from DataConnection WHERE isActive=True")

    data_connection = cursor.fetchall()


def download_files(data_connection):
    for data_connection_detail in data_connection:
        connection_type = data_connection_detail[1]

        if connection_type == 'IMAP':
             ez_email.read_email_imap(data_connection_detail)

        elif connection_type == 'POP3':
             ez_email.read_email_pop3(data_connection_detail)

        elif connection_type == 'FTP':
             ez_ftp.easy_ftp(data_connection_detail)

main.py

from load_file import download_files
from load_file import get_connection

def run_engine():
    while True:
        get_connection()
        download_files()


if __name__ == "__main__":
    run_engine()

When I pass 'data_connection' to 'download_files' function it says I have an unfilled parameter inside of my main.py engine.

I'm sorry if this has already been answered but I'm just having trouble understanding it.

The line

def download_files(data_connection):

implies a parameter to be passed.

I think you might want

def run_engine(): while True: conn = get_connection() download_files(conn)

The get_connection function looks strange to me, like it's not returning a connection but the results of fetchall .

Your download_files() function has a required parameter (data_connection). When you call it in run_engine, you are not passing a parameter.

You will need to capture the returned variable from get_connection and pass it to download_files, like this:

data_con = get_connection()  # place the returned variable from get_connection into a variable called data_con
download_files(data_con)     # pass that data_con variable to the download_files function

But the returned value from get_connection is not actually a connection in your current code - you'll need to make sure you're returning/passing the right variable.

load_file.py

def get_connection():
        cursor = connection.cursor()
        cursor.execute(
            "SELECT ID, Type, Server, Port, User, Password, isActive, FileExtension, FileContains, FileLocation, "
        "ScheduleMinutes, IntervalTime from DataConnection WHERE isActive=True")
    
        return cursor.fetchall()

def download_files(data_connection):

        for data_connection_detail in data_connection:
            # type email will be IMAP, POP3, or FTP
            connection_type = data_connection_detail[1]

            # create data_source object
            if connection_type == 'IMAP':
                ez_email.read_email_imap(data_connection_detail)

            elif connection_type == 'POP3':
                ez_email.read_email_pop3(data_connection_detail)

            elif connection_type == 'FTP':
                ez_ftp.easy_ftp(data_connection_detail)

main.py

from load_file import get_connection
from load_file import download_files

def run_engine():
    while True:
        data = get_connection()
        download_files(data)



if __name__ == "__main__":
    run_engine()

The solution in this case is to return the object from the first function and use it as a parameter in the second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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