Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/iso6346.py
diff options
context:
space:
mode:
authorSharoon Thomas <sharoon.thomas@openlabs.co.in>2014-03-18 16:39:01 +0100
committerArthur de Jong <arthur@arthurdejong.org>2014-03-18 21:26:25 +0100
commit85dd6f26be85f33986c2cd25c5a10cfdf4a5306c (patch)
tree961bda84446bede59109a9d1477f2814b05f7b5a /stdnum/iso6346.py
parent2405c89652607261587cfd619c723c805c2cf852 (diff)
Add support for ISO6346
Add validation and creation of check digit for ISO6346 codes. See: https://github.com/arthurdejong/python-stdnum/pull/9
Diffstat (limited to 'stdnum/iso6346.py')
-rw-r--r--stdnum/iso6346.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/stdnum/iso6346.py b/stdnum/iso6346.py
new file mode 100644
index 0000000..07d1a85
--- /dev/null
+++ b/stdnum/iso6346.py
@@ -0,0 +1,84 @@
+# iso6346.py - functions for handling ISO 6346
+#
+# Copyright (C) 2014 Openlabs Technologies & Consulting (P) Limited
+# Copyright (C) 2014 Arthur de Jong
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""ISO 6346 (International standard for container identification)
+
+ISO 6346 is an international standard covering the coding, identification and
+marking of intermodal (shipping) containers used within containerized
+intermodal freight transport. The standard establishes a visual identification
+system for every container that includes a unique serial number (with check
+digit), the owner, a country code, a size, type and equipment category as well
+as any operational marks. The standard is managed by the International
+Container Bureau (BIC).
+
+>>> validate('csqu3054383')
+'CSQU3054383'
+>>> validate('CSQU3054384')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+"""
+
+import re
+import string
+
+from stdnum.exceptions import InvalidChecksum, InvalidFormat, InvalidLength, \
+ ValidationError
+from stdnum.util import clean
+
+
+_iso6346_re = re.compile('^\w{3}(U|J|Z|R)\d{7}$')
+
+
+def compact(number):
+ """Convert the number to the minimal representation. This strips the
+ number of any valid separators and removes surrounding whitespace."""
+ return clean(number).strip().upper()
+
+
+def calc_check_digit(number):
+ """Calculate check digit and return it for the 10 digit owner code and
+ serial number."""
+ number = compact(number)
+ alphabet = '0123456789A BCDEFGHIJK LMNOPQRSTU VWXYZ'
+ return str(sum(
+ alphabet.index(n) * pow(2, i)
+ for i, n in enumerate(number)) % 11)
+
+
+def validate(number):
+ """Validate the given number (unicode) for conformity to ISO 6346."""
+ number = compact(number)
+ if len(number) != 11:
+ raise InvalidLength()
+ if not _iso6346_re.match(number):
+ raise InvalidFormat()
+ if calc_check_digit(number[:-1]) != number[-1]:
+ raise InvalidChecksum()
+ return number
+
+
+def is_valid(number):
+ """Check whether the number conforms to the standard ISO6346. Unlike
+ the validate function, this will not raise ValidationError(s)."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False