'Determine the exact error of a long python line
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.
Solution 1:[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...
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
