-1

I have long lines in my code such as:

if (currentExcelDep[excelPackageName]== depDataCollection[depDataCollectionSet][depDataCollectionSetElement][excelPath] and currentExcelDep[excelPath].split("/")[1] == depDataCollection[depDataCollectionSet][depDataCollectionSetElement][0].split("/")[1] and depDataCollection[depDataCollectionSet][depDataCollectionSetElement][excelVersion]and currentExcelDep[3] == "Approved"):

And my problem is that im having an IndexError SOMEWHERE in this line, because this is the output:

Traceback (most recent call last):
  File "main_collection.py", line 96, in <module>
    if (currentExcelDep[excelPackageName]== depDataCollection[depDataCollectionSet][depDataCollectionSetElement][excelPath] and currentExcelDep[excelPath].split("/")[1] == depDataCollection[depDataCollectionSet][depDataCollectionSetElement][0].split("/")[1] and depDataCollection[depDataCollectionSet][depDataCollectionSetElement][excelVersion]and currentExcelDep[3] == "Approved"):
IndexError: list index out of range

How on earth do I know, where the problem is in the code?
I mean yeah, I can look it up in my spaghetti code and figure out by myself by splitting this big expression to smaller ones, but I want to learn how to deal with this more efficiently, in this case, and also in the future.

Sipos Péter
  • 119
  • 1
  • 11
  • You have indexed one element too much! – Jonte YH May 14 '22 at 18:08
  • 3
    I'd avoid writing such long lines and split it. Better for legibility and debugging. – LHLaurini May 14 '22 at 18:09
  • Okay thanks, but which element? – Sipos Péter May 14 '22 at 18:09
  • 1
    @JonteYH I guess he can read. The real problem is in which array? :D – simre May 14 '22 at 18:10
  • Well, the way to debug it is to add `print` statements for each subclause before you do the `if`, and see which one explodes. – Tim Roberts May 14 '22 at 18:11
  • The way to deal with this more efficiently is to not write code like this, period. Not trying to be snarky... there really *isn't* a better way to find the error but to simply write code where it's much more difficult to introduce errors like this. That obviously doesn't help you in this case, so follow the advice and break your code into smaller expressions and debug them piece by piece. And don't ever write code like this again lol. – ddejohn May 14 '22 at 18:15
  • Try to print out each element you are accessing trust me print on the console helps a lot! – Jonte YH May 14 '22 at 18:16
  • 1
    `depDataCollection[depDataCollectionSet][depDataCollectionSetElement]` is repeated three times.... why did you do this to yourself? – ddejohn May 14 '22 at 18:16
  • Well honestly, im an intern at a big company, and they wanted me to use more describable variable names like these, to know whats what – Sipos Péter May 14 '22 at 18:23
  • You can simplify your code for yourself _and_ for Python by assigning `depDataCollection[depDataCollectionSet][depDataCollectionSetElement]` to a temporary variable with a simpler name. – tripleee May 14 '22 at 18:48

2 Answers2

1

Split the hell out of this spaghetti:

if currentExcelDep[excelPackageName] == \
    depDataCollection[depDataCollectionSet][depDataCollectionSetElement][excelPath] and \
    currentExcelDep[excelPath].split("/")[1] == \
    depDataCollection[depDataCollectionSet][depDataCollectionSetElement][0].split("/")[1] and \
    depDataCollection[depDataCollectionSet][depDataCollectionSetElement][excelVersion] and \
    currentExcelDep[3] == "Approved":

It is still ugly. But it'll narrowing the error to one array...

simre
  • 647
  • 3
  • 9
0

For ease of debugging, you might want to add a sequence of assertions before you examine the individual conditions.

assert excelPackageName in currentExcelDep
assert depDataCollectionSet in depDataCollection
assert depDataCollectionSetElement in depDataCollection[depDataCollectionSet]
assert excelPath in depDataCollection[depDataCollectionSet][depDataCollectionSetElement]
assert excelPath in currentExcelDep
assert "/" in currentExcelDep[excelPath]
assert depDataCollection[depDataCollectionSet][depDataCollectionSetElement]
assert "/" in depDataCollection[depDataCollectionSet]
assert len(currentExcelDep) > 3

(Not sure I managed to catch them all, but you can see the pattern here. Also, I had to guess which variables are lists and which are dictionaries, and I might have guessed wrong.)

Assertions are possible to turn off in production code if you need to for performance reasons, but often, it makes sense to just leave them enabled so that you can see exactly what went wrong when your assumptions don't hold.

tripleee
  • 175,061
  • 34
  • 275
  • 318