lists.arthurdejong.org
RSS feed

python-stdnum commit: r36 - in python-stdnum: . stdnum tests

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

python-stdnum commit: r36 - in python-stdnum: . stdnum tests



Author: arthur
Date: Fri Aug 27 16:44:26 2010
New Revision: 36
URL: http://arthurdejong.org/viewvc/python-stdnum?view=rev&revision=36

Log:
add ISAN (International Standard Audiovisual Number) module

Added:
   python-stdnum/stdnum/isan.py
   python-stdnum/tests/test_isan.doctest
Modified:
   python-stdnum/README

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Fri Aug 27 13:36:42 2010        (r35)
+++ python-stdnum/README        Fri Aug 27 16:44:26 2010        (r36)
@@ -12,6 +12,7 @@
 - ISBN (International Standard Book Number)
 - ISSN (International Standard Serial Number)
 - ISMN (International Standard Music Number)
+- ISAN (International Standard Audiovisual Number)
 - BSN (Burgerservicenummer, the Dutch national identification number)
 - IMEI (International Mobile Equipment Identity)
 - MEID (Mobile Equipment Identifier)

Added: python-stdnum/stdnum/isan.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/isan.py        Fri Aug 27 16:44:26 2010        (r36)
@@ -0,0 +1,118 @@
+# isan.py - functions for handling International Standard Audiovisual Numbers
+#           (ISANs)
+#
+# Copyright (C) 2010 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
+
+"""Module for handling ISANs (International Standard Audiovisual Numbers),
+used to identify audiovisual works.
+
+>>> is_valid('000000018947000000000000')
+True
+>>> compact('0000-0000-D07A-0090-Q-0000-0000-X')
+'00000000D07A009000000000'
+>>> is_valid('0000-0001-8CFA-0000-I-0000-0000-K')
+True
+>>> is_valid('0000-0001-8CFA-0000-A-0000-0000-K') # invalid check digit
+False
+>>> format('000000018947000000000000')
+'0000-0001-8947-0000-8-0000-0000-D'
+
+>>> to_urn('00000000D07A009000000000')
+'URN:ISAN:0000-0000-D07A-0090-Q-0000-0000-X'
+>>> to_xml('1881-66C7-3420-6541-Y-9F3A-0245-O')
+'<ISAN root="1881-66C7-3420" episode="6541" version="9F3A-0245" />'
+"""
+
+
+from stdnum import iso7064
+
+
+def split(number):
+    """Splits the number into a root, an episode or part, a check digit a
+    version and another check digit. If any of the parts are missing an
+    empty string is returned."""
+    number = number.replace(' ','').replace('-','').strip().upper()
+    if len(number) == 17 or len(number) == 26:
+        return ( number[0:12], number[12:16], number[16], number[17:25], 
number[25:] )
+    elif len(number) > 16:
+        return ( number[0:12], number[12:16], '', number[16:24], number[24:] )
+    else:
+        return ( number[0:12], number[12:16], number[16:], '', '' )
+
+def compact(number, strip_check_digits=True):
+    """Convert the ISAN to the minimal (hexadicimal) representation.
+    digits. This strips the number of any valid separators and removes
+    surrounding whitespace. The check digits are also removed by default."""
+    number = list(split(number))
+    number[2] = number[4] = ''
+    return ''.join(number)
+
+def _check(number, length, required=True):
+    if ( number or required ) and length != len(number):
+        return False
+    for x in number:
+        if x not in '0123456789ABCDEF':
+            return False
+    return True
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid ISAN. If check digits
+    are present in the number they are validated."""
+    try:
+        ( root, episode, check1, version, check2 ) = split(number)
+        return _check(root, 12) and _check(episode, 4) and _check(version, 8, 
False) \
+           and ( not check1 or iso7064.mod_37_36.is_valid(root+episode+check1) 
) \
+           and ( not check2 or 
iso7064.mod_37_36.is_valid(root+episode+version+check2) )
+    except:
+        return False
+
+def format(number, separator='-', strip_check_digits=False, 
add_check_digits=True):
+    """Reformat the passed number to the standard format. If
+    add_check_digits is True the check digit will be added if they are not
+    present yet. If both strip_check_digits and add_check_digits are True the
+    check digits will be recalculated."""
+    ( root, episode, check1, version, check2 ) = split(number)
+    if strip_check_digits:
+        check1 = check2 = ''
+    if add_check_digits and not check1:
+        check1 = iso7064.mod_37_36.calc_check_digit(root+episode)
+    if add_check_digits and not check2 and version:
+        check2 = iso7064.mod_37_36.calc_check_digit(root+episode+version)
+    number = [ root[i:i+4] for i in range(0, 12, 4) ] + [ episode ]
+    if check1:
+        number.append(check1)
+    if version:
+        number.extend((version[0:4], version[4:]))
+    if check2:
+        number.append(check2)
+    return separator.join(number)
+
+def to_binary(number):
+    """Convert the number to it's binary representation (without the check
+    digits)."""
+    return compact(number, strip_check_digits=True).decode('hex')
+
+def to_xml(number):
+    """Returns the XML form of the ISAN as a string."""
+    number = format(number, strip_check_digits=True, add_check_digits=False)
+    return '<ISAN root="%s" episode="%s" version="%s" />' % (
+              number[0:14], number[15:19], number[20:] )
+
+def to_urn(number):
+    """Returns the URN representation of the ISAN."""
+    return 'URN:ISAN:' + format(number, add_check_digits=True)

Added: python-stdnum/tests/test_isan.doctest
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/tests/test_isan.doctest       Fri Aug 27 16:44:26 2010        
(r36)
@@ -0,0 +1,99 @@
+test_isan.doctest - more detailed doctests for stdnum.isan module
+
+Copyright (C) 2010 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
+
+
+This file contains more detailed doctests for the stdnum.isan module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import isan
+
+
+This is how numbers should be split (possible with and without
+episode number):
+
+>>> isan.split('000000018947000000000000')
+('000000018947', '0000', '', '00000000', '')
+>>> isan.split('0000-0000-D07A-0090-Q-0000-0000-X')
+('00000000D07A', '0090', 'Q', '00000000', 'X')
+>>> isan.split('000000018947000000000000')
+('000000018947', '0000', '', '00000000', '')
+>>> isan.split('1881-66C7-3420-6541-Y')
+('188166C73420', '6541', 'Y', '', '')
+>>> isan.split('1881-66C7-3420-6541')
+('188166C73420', '6541', '', '', '')
+
+Compacting should also work:
+
+>>> isan.compact('000000018947000000000000')
+'000000018947000000000000'
+>>> isan.compact('0000-0000-D07A-0090-Q-0000-0000-X')
+'00000000D07A009000000000'
+>>> isan.compact('000000018947000000000000')
+'000000018947000000000000'
+>>> isan.compact('1881-66C7-3420-6541-Y')
+'188166C734206541'
+>>> isan.compact('1881-66C7-3420-6541')
+'188166C734206541'
+
+
+These should be valid numbers:
+
+>>> isan.is_valid('0000-0000-D07A-0090-Q-0000-0000-X')
+True
+>>> isan.is_valid('1881-66C7-3420-6541-Y-9F3A-0245-O')
+True
+>>> isan.is_valid('0000-0000-D07A-0090-Q-0000-0000-X')
+True
+>>> isan.is_valid('0000-0000-D07A-0090-Q')
+True
+>>> isan.is_valid('00000000D07A0090-Z') # wrong check digit
+False
+
+
+The is_valid() method should be fairly robust against invalid junk passed:
+
+>>> isan.is_valid(None)
+False
+>>> isan.is_valid('')
+False
+>>> isan.is_valid(0)
+False
+>>> isan.is_valid(object())
+False
+>>> isan.is_valid('3abc6800-0041X1-20')
+False
+
+
+The format() function can add the check digit if needed. It should leave
+alone existing check digits (even invalid ones) and respect the presence
+or absence of a version number.
+
+>>> isan.format('00000000D07A0090')
+'0000-0000-D07A-0090-Q'
+>>> isan.format('1881-66C734206541-9F3A-0245')
+'1881-66C7-3420-6541-Y-9F3A-0245-O'
+>>> isan.format('1881-66C7-3420-6541-?-9F3A-0245-?', strip_check_digits=True, 
add_check_digits=True)
+'1881-66C7-3420-6541-Y-9F3A-0245-O'
+
+
+A simple test for the to_binary() function.
+
+>>> isan.to_binary('0000-0000-D07A-0090-Q').encode('hex')
+'00000000d07a0090'
--
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits