Skip to content

Commit

Permalink
FEATURE: More helpful error message with safeZip
Browse files Browse the repository at this point in the history
Before it was necessary to debug the error to see what
was unbalanced.  Now it prints out the list of lists directly
with the length of each sublist in a formatted manner.
  • Loading branch information
timmahrt committed Nov 5, 2016
1 parent fa69410 commit 1c01067
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lmeds/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
import math


class UnbalancedListsError(Exception):

def __init__(self, listOfLists):
super(UnbalancedListsError, self).__init__()

self.listOfLists = listOfLists

def __str__(self):
retStr = "All sublists must be the same length.\n"
for subList in self.listOfLists:
retStr += "Length:%04d,List:%s\n" % (len(subList), subList)

return retStr


def decodeUnicode(inputStr):

# Python 2-to-3 compatibility hack
Expand Down Expand Up @@ -127,7 +142,8 @@ def safeZip(listOfLists, enforceLength):

if enforceLength is True:
length = len(listOfLists[0])
assert(all([length == len(subList) for subList in listOfLists]))
if not all([length == len(subList) for subList in listOfLists]):
raise UnbalancedListsError(listOfLists)

return zip_longest(*listOfLists)

Expand Down

0 comments on commit 1c01067

Please sign in to comment.