我正在嘗試構建一個程式來自動創建從 MySQL 到 BigQuery 的批處理管道,并且我隨機收到此 linting 錯誤:“未定義變數‘self’”。
DESTINATION_TABLE_FORMAT 類常量無法訪問 self.environment() 因為它給出了未定義的變數 self 錯誤。
當我將 data_lake_paths 向內移動一個縮進時,它開始作業,但我不再能夠通過呼叫變數來訪問它。
任何幫助將非常感激
class MySQLBatchPipeline:
'''
Class to generate MySQL batch pipelines that store CSV's
in GCS then import them into BigQuery
'''
export_format='CSV'
DESTINATION_TABLE_FORMAT = self.get_environment() '.{dataset}.{table}' #<- linter flags this as "Undefined Variable 'self'"
def __init__(
self,
dag,
sql_directory,
gcp_project_id,
mysql_connection_id,
source_schema,
source_table,
gcs_connection_id,
bq_connection_id,
gcs_bucket,
destination_staging_schema,
destination_schema,
destination_table,
environment,
time_delay,
country,
max_file_size: int=int(50e6),
):
self.dag = dag,
self.sql_directory = sql_directory,
self.gcp_project_id = gcp_project_id,
self.mysql_connection_id = mysql_connection_id,
self.source_schema = source_schema,
self.source_table = source_table,
self.gcs_connection_id = gcs_connection_id,
self.bq_connection_id = bq_connection_id,
self.gcs_bucket = gcs_bucket,
self.destination_staging_schema = destination_staging_schema,
self.destination_schema = destination_schema,
self.destination_table = destination_table,
self.time_delay = time_delay,
self.environment = environment,
self.max_file_size = max_file_size,
self.queries = self.get_pipeline_queries()
self.schema_file = self.get_schema_files()
self.country = country
# variables
data_lake_paths = GoogleCloudStoragePaths(self.destination_table) #<- the same Undefined Variable 'self' error is flagged here.
uj5u.com熱心網友回復:
自從
def __init__(self, ...):
...
# this variable
data_lake_paths = GoogleCloudStoragePaths(self.destination_table)
在任何接受self引數的類方法之外,python 會將其設定為類變數而不是實體變數,因此不會self傳遞任何引數(這就是Undefined Variable 'self'發生這種情況的原因)。
把它放在__init__方法或其他方法中,它應該可以作業。
uj5u.com熱心網友回復:
直接定義在類體內的變數不能使用self,只有方法中的變數可以。相反,只需將該變數的定義移動到__init__函式中。如果即使__init__沒有運行也需要變數,那么只需None在類主體中設定它即可。
代碼:
class MySQLBatchPipeline:
'''
Class to generate MySQL batch pipelines that store CSV's
in GCS then import them into BigQuery
'''
export_format='CSV'
DESTINATION_TABLE_FORMAT = self.get_environment() '.{dataset}.{table}'
# Declare here if necessary
data_lake_paths = None
def __init__(
self,
dag,
sql_directory,
gcp_project_id,
mysql_connection_id,
source_schema,
source_table,
gcs_connection_id,
bq_connection_id,
gcs_bucket,
destination_staging_schema,
destination_schema,
destination_table,
environment,
time_delay,
country,
max_file_size: int=int(50e6),
):
self.dag = dag,
self.sql_directory = sql_directory,
self.gcp_project_id = gcp_project_id,
self.mysql_connection_id = mysql_connection_id,
self.source_schema = source_schema,
self.source_table = source_table,
self.gcs_connection_id = gcs_connection_id,
self.bq_connection_id = bq_connection_id,
self.gcs_bucket = gcs_bucket,
self.destination_staging_schema = destination_staging_schema,
self.destination_schema = destination_schema,
self.destination_table = destination_table,
self.time_delay = time_delay,
self.environment = environment,
self.max_file_size = max_file_size,
self.queries = self.get_pipeline_queries()
self.schema_file = self.get_schema_files()
self.country = country
# Define here
data_lake_paths = GoogleCloudStoragePaths(self.destination_table)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/418006.html
標籤:
上一篇:PHP中的組合與聚合
下一篇:c 兩個類相互參考
