尝试将 None 设置为默认方法参数时如何解决 AttributeError

How to resolve AttributeError when trying to set None as default method argument

我知道这个问题有很多答案,但我还是不明白... 以下是sa_reporting.py

class saReport():

    def __init__(self, body, to_table, normalise=False, date_col=None):
        global directory
        self.body = body
        self.to_table = to_table
        self.normalise = normalise
        self.date_col = date_col if date_col is not None else []

        directory = os.path.join('/Users','python', self.to_table)
        if not os.path.exists(directory):
            os.mkdir(directory)

    def download_files(self, ...):
        ...

    def download_reports(self, ...):
        ...

    def get_files(self):
        ...

    def read_file(self, file):
        ....

    def load_to_db(self, sort_by=None): # THIS IS WHAT I THINK IS CAUSING THE ERROR

        sort_by = sort_by if sort_by is not None else [] # THIS IS WHAT I TRIED TO FIX IT

        def normalise_data(self, data):
            dim_data = []
            for row in data:
                if row not in dim_data:
                    dim_data.append(row)
            return dim_data

        def convert_dates(self, data):
            if self.date_col:
                for row in data:
                    for index in self.date_col:
                        if len(row[index]) > 10:
                            row[index] = row[index][:-5].replace('T',' ')
                            row[index] = datetime.datetime.strptime(row[index], "%Y-%m-%d %H:%M:%S")
                        else:
                            row[index] = datetime.datetime.strptime(row[index], "%Y-%m-%d").date()
            return data

        print(f'\nWriting data to {self.to_table} table...', end='')
        files = self.get_files()

        for file in files:
            print('Processing ' + file.split("sa360/",1)[1] + '...', end='')
            csv_file = self.read_file(file)
            csv_headers = ', '.join(csv_file[0])
            csv_data = csv_file[1:]

        if self.normalise:
            csv_data = self.normalise_data(csv_data)

        csv_data = self.convert_dates(csv_data)

        if sort_by:
            csv_data = sorted(csv_data, key=itemgetter(sort_by))

        #...some other code that inserts into a database...

正在执行以下脚本 (sa_main.py):

import sa_reporting
from sa_body import *

dim_campaign_test = sa_reporting.saReport(
    body=dim_campaign_body,
    to_table='dimsa360CampaignTest',
    normalise=True,
    date_col=[4,5]
)
dim_campaign_test_download = dim_campaign_test.download_reports()
dim_campaign_test_download.load_to_db(sort_by=0) # THIS IS WHERE THE ERROR OCCURS

输出和错误信息:

Downloading reports...
The report is still generating...restarting

The report is ready
Processing...
Downloading fragment 0 for report AAAnOdc9I_GnxAB0

Files successfully downloaded
Traceback (most recent call last):
  File "sa_main.py", line 43, in <module>
    dim_campaign_test_download.load_to_db(sort_by=0)
AttributeError: 'NoneType' object has no attribute 'load_to_db'

为什么会出现此错误?我该如何解决? 我只想使 None 成为默认参数,如果用户指定 sort_by 参数,则 None 将替换为用户指定的任何内容(应该是整数索引)

此代码似乎暗示 dim_campaign_test_download 被设置为 None。如在下一行中,您将其设置为 dim_campaign_test.download_reports() 的结果,很可能没有找到任何报告。

dim_campaign_test_download = dim_campaign_test.download_reports()

您可能想要改为执行以下操作,因为 dim_campaign_test 是您可能想要操作的 saReport 对象:

dim_campaign_test.load_to_db(sort_by=0)