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

Time:05-10

I need to pass data_connection into my main.py engine because that variable is where the data from mysql string is being stored. In main.py it says parameter data_connection unfilled but I can only enter 'data_connection=' I'm confused what I'm supposed to do...

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:
            # 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:
        get_connection()
        download_files()



if __name__ == "__main__":
    run_engine()

CodePudding user response:

data_connection is a local variable in load_file.py that is destroyed when the function exits. You also have a like-named parameter in download_files. These are two different, unique variables created on each call to the function. They happen to have the same name, but in different namespaces so do not refer to the same thing.

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

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

Notice that I didn't use the variable data_connection in the first function at all, just returned its data. And when calling download_files I used a different name for the variable. This highlights the difference between a named variable in one context and the object it references in another context.

  • Related