i am getting error: ValueError: need more than 2 values to unpack when i run the unit test now, so 2 failures and one skip now as far as i have read about
lambda
You could write a utility function to make your results uniform:
def do_something2():
return 1, 2
def do_something3():
return 1, 2, 3
def do_something5():
return 1, 2, 3, 4, 5
def uniform_result(*args):
return args[0], args[1], args[2:]
a, b, c = uniform_result(*do_something2())
print a, b, c
# 1 2 ()
a, b, c = uniform_result(*do_something3())
print a, b, c
# 1 2 (3,)
a, b, c = uniform_result(*do_something5())
print a, b, c
# 1 2 (3, 4, 5)
Instead of unpacking in your assignment:
a, b, c = do_something()
Try assigning the result to a single variable and testing its length:
t = do_something()
# t is now a tuple (or list, or whatever was returned) of results
if len(t) > 2:
# Can use the third result!
c = t[2]
So errors
is a list that contains items that are tuples of length 2 or 3. You want a way to unpack tuples of varying length in a for-loop. As you have noted, there is no clean way to do this in Python2. Rather than coming up with a clever way of implementing this behavior, I would suggest making sure that your errors list always contains tuples of length of 3. This can be done every time you add an item to errors
, or after the fact, like this:
errors = [(x[0], x[1], x[2]) if len(x) == 3 else (x[0], x[1], None) for x in errors]
Or you could make a generator (which goes against my advice of not finding clever way to implement this behavior):
def widen_tuples(iter, width, default=None):
for item in iter:
if len(item) < width:
item = list(item)
while len(item) < width:
item.append(default)
item = tuple(item)
yield item
Use it like this:
>>> errors = [(1, 2), (1, 2, 3)]
>>> for a, b, c in widen_tuples(errors, 3):
... print a, b, c
1 2 None
1 2 3
I suggest complementing the list to the necessary length with the None
elements:
data = give_me_list()
(val1, val2, val3) = data + (3 - len(data)) * [None]
3 is the number of the left hand side values. If the list can have excessive elements, then use protection against that:
data = give_me_list()[:3]
(val1, val2, val3) = data + (3 - len(data)) * [None]