Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I introduce the output of variables in the message of asserts

Tags:

python

I am doing some validation of the input data of one program I have created. I am doing this with assert. If assertion arises I want to know in which part of the data occurs so I want to get the value that arises the assertion.

assert all(isinstance(e, int)
           for l1 in sequence.values()
           for l2 in l1 for e in l2),"Values of the dictionnary aren't lists of integers. Assert found in  '{l2}'"
# This code doesn't work
like image 642
Manolo Dominguez Becerra Avatar asked Jan 25 '23 05:01

Manolo Dominguez Becerra


1 Answers

Not when using all as it does not expose the "iteration variable". You will need an explicit, nested loop. You also forgot the f'' prefix to denote an f-string:

for l1 in [['a']]:
    for l2 in l1:
        for e in l2:
            assert isinstance(e, int), f"Values of the dictionnary 
aren't lists of integers. Assert found in  '{l2}'"
AssertionError: Values of the dictionnary aren't lists of integers. Assert found in  'a'
like image 63
DeepSpace Avatar answered Jan 26 '23 18:01

DeepSpace