lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.20-21-g928a09d

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

python-stdnum branch master updated. 1.20-21-g928a09d



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  928a09ded2505d1d307833ae180e24eff6b78dcd (commit)
      from  2b9207582b9712c79015f79151f266b30b5ab824 (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 -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=928a09ded2505d1d307833ae180e24eff6b78dcd

commit 928a09ded2505d1d307833ae180e24eff6b78dcd
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sat Feb 15 15:35:48 2025 +0100

    Add International Standard Name Identifier
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/463

diff --git a/stdnum/isni.py b/stdnum/isni.py
new file mode 100644
index 0000000..5b07a01
--- /dev/null
+++ b/stdnum/isni.py
@@ -0,0 +1,81 @@
+# isni.py - functions for handling International Standard Name Identifiers
+#
+# Copyright (C) 2025 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
+
+"""ISNI (International Standard Name Identifier).
+
+The International Standard Name Identifier (ISNI) is used to identify
+contributors to media content such as books, television programmes, and
+newspaper articles. The number consists of 16 digits where the last character
+is a check digit.
+
+More information:
+
+* https://en.wikipedia.org/wiki/International_Standard_Name_Identifier
+* https://isni.org/
+
+>>> validate('0000 0001 2281 955X')
+'000000012281955X'
+>>> validate('0000 0001 1111 955X')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('0000 0001 2281')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> format('000000012281955X')
+'0000 0001 2281 955X'
+"""
+
+
+from stdnum.exceptions import *
+from stdnum.iso7064 import mod_11_2
+from stdnum.util import clean, isdigits
+
+
+def compact(number):
+    """Convert the ISNI to the minimal representation. This strips the number
+    of any valid ISNI separators and removes surrounding whitespace."""
+    return clean(number, ' -').strip().upper()
+
+
+def validate(number):
+    """Check if the number is a valid ISNI. This checks the length and
+    whether the check digit is correct."""
+    number = compact(number)
+    if not isdigits(number[:-1]):
+        raise InvalidFormat()
+    if len(number) != 16:
+        raise InvalidLength()
+    mod_11_2.validate(number)
+    return number
+
+
+def is_valid(number):
+    """Check if the number provided is a valid ISNI."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def format(number):
+    """Reformat the number to the standard presentation format."""
+    number = compact(number)
+    return ' '.join((number[:4], number[4:8], number[8:12], number[12:16]))

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

Summary of changes:
 stdnum/{gn/nifp.py => isni.py} | 65 +++++++++++++++++++-----------------------
 1 file changed, 30 insertions(+), 35 deletions(-)
 copy stdnum/{gn/nifp.py => isni.py} (52%)


hooks/post-receive
-- 
python-stdnum