lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.7-13-gb7b812c

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

python-stdnum branch master updated. 1.7-13-gb7b812c



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  b7b812c88c76fe36738efc445c4672f52ef89765 (commit)
      from  a6ae1d0d2bb8f28fef71678f70951a769086c78b (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=b7b812c88c76fe36738efc445c4672f52ef89765

commit b7b812c88c76fe36738efc445c4672f52ef89765
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Wed Nov 22 22:55:14 2017 +0100

    Add Euro bank notes serial number
    
    This adds validation of serial numbers that appear on Euro bills.

diff --git a/stdnum/eu/banknote.py b/stdnum/eu/banknote.py
new file mode 100644
index 0000000..f8d445d
--- /dev/null
+++ b/stdnum/eu/banknote.py
@@ -0,0 +1,72 @@
+# banknote.py - functions for handling Euro banknote serial numbers
+#
+# Copyright (C) 2017 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
+
+"""Euro banknote serial numbers.
+
+The serial number consists of one letter and 11 digits, or two letters and 10
+digits for the new series banknotes.
+
+More information:
+
+* https://en.wikipedia.org/wiki/Euro_banknotes#Serial_number
+
+>>> validate('P36007033744')
+'P36007033744'
+>>> validate('P36007033743')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+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, ' ').upper().strip()
+
+
+def checksum(number):
+    """Calculate the checksum over the number."""
+    # replace letters by their ASCII number
+    return sum(int(x) if x.isdigit() else ord(x) for x in number) % 9
+
+
+def validate(number):
+    """Check if the number is a valid banknote serial number."""
+    number = compact(number)
+    if not number[:2].isalnum() or not number[2:].isdigit():
+        raise InvalidFormat()
+    if len(number) != 12:
+        raise InvalidLength()
+    if number[0] not in 'BCDEFGHJLMNPRSTUVWXYZ':
+        raise InvalidComponent()
+    if checksum(number) != 0:
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid banknote serial number."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_eu_banknote.doctest b/tests/test_eu_banknote.doctest
new file mode 100644
index 0000000..8a6d7a3
--- /dev/null
+++ b/tests/test_eu_banknote.doctest
@@ -0,0 +1,72 @@
+test_eu_banknote.doctest - more detailed doctests for the stdnum.eu.banknote 
module
+
+Copyright (C) 2017 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.eu.banknote module.
+It contains some corner case tests and tries to validate a serial numbers
+that have been found online.
+
+>>> from stdnum.eu import banknote
+>>> from stdnum.exceptions import *
+
+
+Some basic tests for invalid numbers.
+
+>>> banknote.validate('RABCDEFGHIJN')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> banknote.validate('Z123')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> banknote.validate('A71220515713')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> banknote.validate('P36007033742')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+These should all be valid serial numbers.
+
+>>> numbers = '''
+...
+... F00172834112
+... P36007033744
+... S22227803764
+... SD9103468574
+... U36749787719
+... U71220515711
+... V37421817898
+... VA0436214792
+... X14234175767
+... X45370810262
+... X64709081183
+... X80420825306
+... Y12470649328
+... Z10708476264
+... Z34706855889
+... ZA1567572696
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not banknote.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/{fi/alv.py => eu/banknote.py}               | 42 +++++++------
 ...test_ec_ci.doctest => test_eu_banknote.doctest} | 71 ++++++++++++----------
 2 files changed, 61 insertions(+), 52 deletions(-)
 copy stdnum/{fi/alv.py => eu/banknote.py} (60%)
 copy tests/{test_ec_ci.doctest => test_eu_banknote.doctest} (51%)


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