将函数变量传递到另一个文件 python
passing function variables to another file python
我需要将 data_connection 传递到我的 main.py 引擎中,因为该变量是存储来自 mysql 字符串的数据的地方。在 main.py 它说参数 data_connection 未填充但我只能输入 'data_connection=' 我很困惑我应该做什么...
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()
data_connection
是load_file.py中的局部变量,在函数退出时销毁。 download_files 中还有一个 like-named 参数。这是在每次调用函数时创建的两个不同的唯一变量。它们恰好具有相同的名称,但在不同的命名空间中,因此请不要引用相同的东西。
这种情况下的解决方案是 return 来自第一个函数的对象,并将其用作第二个函数的参数。
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()
请注意,我根本没有在第一个函数中使用变量 data_connection
,只是 return 编辑了它的数据。在调用 download_files
时,我为变量使用了不同的名称。这突出了一个上下文中的命名变量与它在另一个上下文中引用的对象之间的区别。
我需要将 data_connection 传递到我的 main.py 引擎中,因为该变量是存储来自 mysql 字符串的数据的地方。在 main.py 它说参数 data_connection 未填充但我只能输入 'data_connection=' 我很困惑我应该做什么...
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()
data_connection
是load_file.py中的局部变量,在函数退出时销毁。 download_files 中还有一个 like-named 参数。这是在每次调用函数时创建的两个不同的唯一变量。它们恰好具有相同的名称,但在不同的命名空间中,因此请不要引用相同的东西。
这种情况下的解决方案是 return 来自第一个函数的对象,并将其用作第二个函数的参数。
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()
请注意,我根本没有在第一个函数中使用变量 data_connection
,只是 return 编辑了它的数据。在调用 download_files
时,我为变量使用了不同的名称。这突出了一个上下文中的命名变量与它在另一个上下文中引用的对象之间的区别。