What is pylint "Too many local variables" trying to tell me and what do I do with that?

Viewed 4802

I'm working on a python function that reads in Excel or CSV files with various measuring data and spits out a dict, containing strings with the metadata and a pandas dataframe with the data.

def reader(file):
"""
blablaba
"""
#500 lines of code
output = {'Error': Errorflag, 'Name': Name, 'Location': Location,
          # 20 more variables
          'Lat': Lat, 'Lon': Lon, 'Number': Number, 'data': data}    
return output

The whole thing works well, but pylint tells me Too many local variables (120/15)pylint(too-many-locals. So I'm apparently violating 'good' coding style, but I'm wondering where this requirement comes from and what I can do about it.

The pylint documentation just helpfully states "Too many local variables (%s/%s) Used when a function or method has too many local variables." This site is a bit more helpful, telling me my function is likely too complex and that I should consider using separate functions.

I could certainly do this, but that would either mean turning reader into multiple functions (location_metadatareader, type_metadatareader, errorfinder, datareader, ...) which would make using and importing it hell, or I could turn a bunch of things in the function into functions itself, e.g. instead of

elevation = elev.replace(",", ".")
elevStart = elevation.find(';')+1
elevEnd = elevation.find('\n')
elevation = elevString1[elevStart:elevEnd]
elev = float(elev)

I could encapsulate in in a function

def elev(elevation):
    elevation = elev.replace(",", ".")
    elevStart = elevation.find(';')+1
    elevEnd = elevation.find('\n')
    elevation = elevString1[elevStart:elevEnd]
    elev = float(elev)
return(elev)

elev = elev(elevation)

adding a bunch more lines without much to gain.

So, what (besides rethinking the whole function or switching off this part of pylint) are my options? But more important: What's the reason for this style convention? Why should I (or shouldn't I) put in some work to follow it?

0 Answers
Related