dimanche 14 juillet 2019

how to avoid repeating asserts (or how to insert a for loop in try assert statements)?

I'd like to avoid repeating an assert statement one time for each of these and more cases:

na000 ='567890123456789012345678901234567890123456789012345678901234567890'
na003 = '45678901234567890123456789012345678901234567890123456789012345678'
na099 = '012345678901234567890123456789012345678901234567890123456789012345'
na098 = '67890123456789012345678901234567890XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
na050 = '12XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
na002 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

Speficaclly I'd like to avoid repeating asserts like this:

try:
    assert levenshteinize(na000, nai03, 1)[0] == 4, 'testing 3 substitut and 1 insert error' 
    assert levenshteinize(na000, no099, 1)[0] == 99, 'testing 99 omisions error'
    assert levenshteinize(na000, no098, sil)[0] == 98, 'testing 98 omisions error'
    assert levenshteinize(na000, no050, sil)[0] == 50, 'testing 50 omisions error'
    assert levenshteinize(na000, no002, sil)[0] == 2, 'testing 2 omisions error'
except AssertionError as e
...

I've inserted the strings in a dictionary with the key as the var name (the number). But what's important is that to avoid the asserts I've create a function that includes the try... assert... except in a for-loop. I've tried different versions, similar to the one below, but none of them seem to be ok (invalid syntax error even before running it).

test_lev(nerrors = defaultlist, silent = 0, levreturn: int = 0):

try:
    for num in nerrors: 
        assert nnocrate.levenshteinize(na['000'], na[numto3st(num)], silent)[levreturn] == num, f'testing {num} error'
except AssertionError as e:
    print(f'levenshtein() has failed with na when {e.args[0]}\n')
else:
    print('\nAll na assert OK)

How can I do that? How can I insert a for loop or something to avoid repeating the assert ?

Aucun commentaire:

Enregistrer un commentaire