'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.
Solution 1:[1]
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.
Solution 2:[2]
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
.
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | boacon-python |
Solution 2 | Josh English |
Solution 3 | DavidWalker |