Arg0s1080 / mrz

Machine Readable Zone generator and checker for official travel documents sizes 1, 2, 3, MRVA and MRVB (Passports, Visas, national id cards and other travel documents)
GNU General Public License v3.0
328 stars 122 forks source link

Code not executed after checker returns False #17

Closed xxxpsyduck closed 4 years ago

xxxpsyduck commented 4 years ago

Hello I have a code block like this:

...
checker1 = TD3CodeChecker(...)

if bool(checker1) == True:
    ...
else:
   ...

The problem is when checker1 does not return True then all the lines below it won't run (the else statement never triggered).

Arg0s1080 commented 4 years ago

Hi

I don't know if I understood you, but it works fine for me.

An example:

from mrz.checker.td3 import TD3CodeChecker

# This MRZ code should return True 
mrz_td3 = ("P<CANMARTIN<<SARAH<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
           "ZE000509<9CAN8501019F2301147<<<<<<<<<<<<<<08")

check1 = TD3CodeChecker(mrz_td3)

if bool(check1):
    print("check1 it's True")
else:
    print("check1 it's False")

# Now let's introduce some errors:
mrz_td3 = ("BDBADMARTIN<<SARAH<<<<<<<<<BAD<<<<BAD<<<<<<<\n"
           "ZE000509<1CAN8501019F2301147<<<<<<<<<<<<<<11")

check2 = TD3CodeChecker(mrz_td3)

if bool(check2) == True:
    print("check2 it's True")
else:
    print("check2 it's False")

Output:

check1 it's True
check2 it's False

Maybe if you showed the complete code we could see where is the error.

Regards!

xxxpsyduck commented 4 years ago

I have error with the following code:

from mrz.checker.td3 import TD3CodeChecker

mrz_td3 = ("8053333LX2E7EZ90X2908022W6L0LL889SI95326S<2SL<\n"
           "333333333333333333233D13NVL13NX35ONV0317O19SI54")

check2 = TD3CodeChecker(mrz_td3)

if bool(check2) == True:
    print("check2 it's True")
else:
    print("check2 it's False")`

mrz_td3 = ("P<CANMARTIN<<SARAH<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
           "ZE000509<9CAN8501019F2301147<<<<<<<<<<<<<<08")

check1 = TD3CodeChecker(mrz_td3)

if bool(check1):
    print("check1 it's True")
else:
    print("check1 it's False")

Output:

raise LengthError(cause=len(s), document=document_description, length=length) mrz.base.errors.LengthError: ('String was not recognized as a valid TD3. It should have 88 characters', '93')

xxxpsyduck commented 4 years ago

@Arg0s1080 I modified your code to bypass all the exceptions. Btw there are some mistake in base/string_checkers.py, there is no name 's' defined in precheck() function

def precheck(document_description: str, string: str, length: int):
    if check_string(_check_upper(string)) and len(string) != length:
        raise LengthError(cause=len(s), document=document_description, length=length)
    if not is_printable(string, "\n"):
        raise FieldError("%s contains invalid characters" % document_description, s)
Arg0s1080 commented 4 years ago

Hi again!

In the output you can see what the error is. Your mrz string has 93 chars. MRZ code of all TD3 must be 88 chars long (89 if escape character is counted)

So, as I see it, everything works fine.

Regards