我需要將 data_connection 傳遞到我的 main.py 引擎中,因為該變數是存盤 mysql 字串資料的位置。在 main.py 它說引數 data_connection 未填充,但我只能輸入 'data_connection=' 我很困惑我應該做什么......
加載檔案.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)
主檔案
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()
uj5u.com熱心網友回復:
data_connection是 load_file.py 中的區域變數,在函式退出時被銷毀。您在 download_files 中還有一個類似名稱的引數。這是在每次呼叫函式時創建的兩個不同的唯一變數。它們碰巧有相同的名稱,但在不同的命名空間中,所以不要指代相同的東西。
這種情況下的解決方案是從第一個函式回傳物件,并在第二個函式中將其用作引數。
加載檔案.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)
主檔案
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在第一個函式中使用變數,只是回傳了它的資料。在呼叫時,download_files我為變數使用了不同的名稱。這突出了一個背景關系中的命名變數和它在另一個背景關系中參考的物件之間的區別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/472095.html
