简体   繁体   中英

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

I know there are loads of answers to this question but I'm still not getting it... Following is 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...

Executing the following script ( 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

Output and error message:

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'

Why am I getting this error? And how can I fix it? I just want to make None be the default argument and if a user specifies the sort_by parameter then None will be replaced with whatever the user specifies (which should be an integer index)

This code would seem to suggest that dim_campaign_test_download is being set to None . As in the below line, you set it to the result of dim_campaign_test.download_reports() , it is likely that no reports are being found.

dim_campaign_test_download = dim_campaign_test.download_reports()

You might want to instead do the following, as dim_campaign_test is the saReport Object on which you probably want to operate:

dim_campaign_test.load_to_db(sort_by=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM