vininfo
vininfo copied to clipboard
Vin().validate() requires vin that is already given
It seems irrational to be, but to make validate (classmethod) work, you have to instantiate the Vin with a VIN number and call validate again with a VIN number.
Vin('someVIN').validate('someVIN')
is the only way that I managed to make it work.
In my humble opinion, you should be able to check like so:
vin = 'SomeVIN'
vin_object = Vin(vin)
vin_object.validate() # This returns True or False
P.S.1: Or even better would be:
vin = 'SomeVIN'
vin_object = Vin(vin)
if vin_object.is_valid: # This returns True or False
print('Yay!')
vin_object.is_valid_with_exception() # or something like that
>>> InvalidVIN: This VIN number is invalid.
P.S.2: I had an idea:
def validate(self, num: str = None, with_exception: bool = False) -> str:
"""Performs basic VIN validation and sanation.
:param num: Other VIN to check
:param with_exception: Flag to determine if the validate function should raise an exception or not.
"""
if num is None:
num = self.num
num = num.strip().upper()
if len(num) != 17:
raise ValidationError(f'VIN number requires 17 chars ({num_len} given)')
pattern = r"^[A-HJ-NPR-Z0-9]{17}$"
if not re.match(pattern, num):
if with_exception:
raise ValidationError(f"VIN number must only contain alphanumeric symbols except 'I', 'O', and 'Q' ")
else:
return False
return True
Cheers 🍺
you have to instantiate the Vin with a VIN number and call validate again with a VIN number.
Do you mean Vin.validate('X')
don't work for you?
If you need a property .is_valid
pull request is welcome.
Hi Igor,
No, it does not.
Hi,
No, it does not.
validate()
can be accessed explicitly (as a class method); otherwise it's called implicitly on object instantiation, so you do not need to call it again. And what is your use case?
This should work for your use case:
vin = 'SomeVIN'
try:
Vin(vin)
except Exception:
return False
else:
return True