lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 0.9-7-g85dd6f2

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 0.9-7-g85dd6f2



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  85dd6f26be85f33986c2cd25c5a10cfdf4a5306c (commit)
      from  2405c89652607261587cfd619c723c805c2cf852 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://arthurdejong.org/git/python-stdnum/commit/?id=85dd6f26be85f33986c2cd25c5a10cfdf4a5306c

commit 85dd6f26be85f33986c2cd25c5a10cfdf4a5306c
Author: Sharoon Thomas <sharoon.thomas@openlabs.co.in>
Date:   Tue Mar 18 21:09:01 2014 +0530

    Add support for ISO6346
    
    Add validation and creation of check digit for ISO6346 codes.
    
    See: https://github.com/arthurdejong/python-stdnum/pull/9

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
diff --git a/tests/test_iso6346.doctest b/tests/test_iso6346.doctest
new file mode 100644
index 0000000..11bd81f
--- /dev/null
+++ b/tests/test_iso6346.doctest
@@ -0,0 +1,59 @@
+test_doctest - more detailed doctests for the stdnum.iso6346 package
+
+Copyright (C) 2014 Openlabs Technologies & consulting (P) Limited
+
+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
+
+
+This file contains more detailed doctests for the stdnum.iso7064 package. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import iso6346
+
+
+Test with valid numbers:
+
+>>> iso6346.validate('CSQU3054383')
+'CSQU3054383'
+>>> iso6346.is_valid('CSQU3054383')
+True
+>>> iso6346.validate('csqu3054383')
+'CSQU3054383'
+>>> iso6346.is_valid('csQU3054383')
+True
+>>> iso6346.validate('tcnu7200794')
+'TCNU7200794'
+>>> iso6346.validate('tolu4734787')
+'TOLU4734787'
+
+
+Test with invalid numbers:
+
+>>> iso6346.validate('CSQU3054384')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> iso6346.validate('CSQU305438')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> iso6346.is_valid('CSQU3054384')
+False
+>>> iso6346.validate('CSQU3054Z83')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...

-----------------------------------------------------------------------

Summary of changes:
 stdnum/iso6346.py                                |   84 ++++++++++++++++++++++
 tests/{test_ean.doctest => test_iso6346.doctest} |   46 ++++++++----
 2 files changed, 115 insertions(+), 15 deletions(-)
 create mode 100644 stdnum/iso6346.py
 copy tests/{test_ean.doctest => test_iso6346.doctest} (56%)


hooks/post-receive
-- 
python-stdnum
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/