Home > Blockchain >  python passing function variables to another function
python passing function variables to another function

Time:05-06

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....)

    data_connection = cursor.fetchall()
    return data_connection


def download_files(data_connection):
        data_connection = cursor.fetchall()
        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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related