-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added is_freight_container_id validator
- Loading branch information
1 parent
5f08645
commit ffb23f7
Showing
5 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import re | ||
from .utils.assert_string import assert_string | ||
|
||
isISO6346Str = re.compile(r'^[A-Z]{3}((U[0-9]{7})|([J,Z][0-9]{6,7}))$') | ||
isDigit = re.compile(r'^[0-9]$') | ||
|
||
def is_freight_container_id(input): | ||
input = assert_string(input) | ||
input = input.upper() | ||
|
||
if not isISO6346Str.match(input): | ||
return False | ||
|
||
if len(input) == 11: | ||
_sum = 0 | ||
for i in range(len(input) - 1): | ||
if not isDigit.match(input[i]): | ||
letterCode = ord(input[i]) - 55 | ||
if letterCode < 11: | ||
convertedCode = letterCode | ||
elif 11 <= letterCode <= 20: | ||
convertedCode = 12 + (letterCode % 11) | ||
elif 21 <= letterCode <= 30: | ||
convertedCode = 23 + (letterCode % 21) | ||
else: | ||
convertedCode = 34 + (letterCode % 31) | ||
_sum += convertedCode * (2 ** i) | ||
else: | ||
_sum += int(input[i]) * (2 ** i) | ||
|
||
checkSumDigit = _sum % 11 | ||
return int(input[-1]) == checkSumDigit | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import unittest | ||
from pyvalidator.is_freight_container_id import is_freight_container_id | ||
from . import print_test_ok | ||
|
||
class TestIsFreightContainerId(unittest.TestCase): | ||
|
||
def test_valid_ISO6346(self): | ||
valid_codes = [ | ||
'HLXU2008419', | ||
'TGHU7599330', | ||
'ECMU4657496', | ||
'MEDU6246078', | ||
'YMLU2809976', | ||
'MRKU0046221', | ||
'EMCU3811879', | ||
'OOLU8643084', | ||
'HJCU1922713', | ||
'QJRZ123456', | ||
] | ||
for code in valid_codes: | ||
self.assertTrue(is_freight_container_id(code)) | ||
print_test_ok() | ||
|
||
def test_invalid_ISO6346(self): | ||
invalid_codes = [ | ||
'OOLU1922713', | ||
'HJCU1922413', | ||
'FCUI985619', | ||
'ECMJ4657496', | ||
'TBJA7176445', | ||
'AFFU5962593', | ||
] | ||
for code in invalid_codes: | ||
self.assertFalse(is_freight_container_id(code)) | ||
print_test_ok() | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |