dmachop
dmachop

Reputation: 884

Python: TypeError: 'NoneType' object is not callable

I am trying to read a CSV and add it to list of objects.

This is the class code

class ReportItem:
    '''
    classdocs
    '''
    def __init__(self, hostname, jdbcDbname):
        '''
        Constructor
        '''
        self.hostname = hostname
        self.jdbcDbname = jdbcDbname
        self.dbname = None
        self.schema = None
        self.shortName = None
        self.totalRows = None
        self.archivedRows = None
        self.deletedRows = None
        self.deletedSizeMB = None
        self.totalSizeMB = None
        self.archivedSizeMB = None
        self.deletedSizeMB = None

    def setDbname(self, dbname):
        self.dbname = dbname

    def setSchema(self, schema):
        self.schema = schema

    def setShortName(self, shortName):
        self.shortName = shortName

    def setTotalRows(self, totalRows):
        self.totalRows = totalRows

    def setArchivedRows(self, archivedRows):
        self.archivedRows = archivedRows

    def setDeletedRows(self, deletedRows):
        self.deletedRows = deletedRows

    def setTotalSizeMB(self, totalSizeMB):
        self.totalSizeMB = totalSizeMB

    def setArchivedSizeMB(self, archivedSizeMB):
        self.archivedSizeMB = archivedSizeMB

    def setDeletedSizeMB(self, deletedSizeMB):
        self.deletedSizeMB = deletedSizeMB

This is the implementation code:

import csv
from ReportItem import ReportItem

reportList = []

with open('Tables.csv', 'r') as csvfile:
    has_header = csv.Sniffer().has_header(csvfile.read(1024))
    csvfile.seek(0)
    csvReader = csv.reader(csvfile, delimiter=',', quotechar='\'')
    if has_header:
        next(csvfile, None)
    for col in csvReader:
        reportItem = ReportItem(col[0], col[1])
        reportItem.setDbname(col[2])
        reportItem.setSchema(col[3])
        reportItem.setShortName(col[4])
        reportItem.setTotalRows(col[5])
        reportItem.setArchivedRows(col[6])
        reportItem.setDeletedRows(col[7])
        reportItem.totalSizeMB(col[8])
        reportItem.archivedSizeMB(col[9])
        reportItem.setDeletedSizeMB(col[10])
        reportList.append(reportItem)


for ri in reportList:
    print(ri.dbname)

The CSV contents will look like: xxx xx xxxx xxxx xxxxx -1 0.06 -1 -1 -1 -1

This had resulted in the error:

Traceback (most recent call last):
  File "CSVImporter.py", line 25, in <module>
    reportItem.totalSizeMB(col[8])
TypeError: 'NoneType' object is not callable

Another concern is that this col[8] may also be represented using scientific notation like 1.000E5. What is the best way to approach this issue?

Upvotes: 0

Views: 1050

Answers (2)

David
David

Reputation: 696

It looks like a typo. Your code should be

reportItem.setTotalSizeMB(col[8])

rather than

reportItem.totalSizeMB(col[8])

There is no function totalSizeMB in your class.

Upvotes: 2

Thsise Faek
Thsise Faek

Reputation: 311

I think I found the reason. In the implementation you call the totalSizeMB variable that you define in the init function of the class. Variables do not take arguments. (maybe you meant the function, setTotalSizeMB)

If this is the case, then there is the same problem right under it (archivedSizeMB)

Upvotes: 2

Related Questions