python-stdnum branch master updated. 1.4-2-g466cec8
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum branch master updated. 1.4-2-g466cec8
- From: Commits of the python-stdnum project <python-stdnum-commits [at] lists.arthurdejong.org>
- To: python-stdnum-commits [at] lists.arthurdejong.org
- Reply-to: python-stdnum-users [at] lists.arthurdejong.org
- Subject: python-stdnum branch master updated. 1.4-2-g466cec8
- Date: Sat, 3 Sep 2016 19:11:59 +0200 (CEST)
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 466cec8b4022e7b6c11f7d387580d9d05f2e6f22 (commit)
from d95382fddb759b7e8d81964cf446bc32ab274b42 (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=466cec8b4022e7b6c11f7d387580d9d05f2e6f22
commit 466cec8b4022e7b6c11f7d387580d9d05f2e6f22
Author: David García Garzón <david.garcia@upf.edu>
Date: Sun Aug 28 11:31:20 2016 +0200
Add Spanish CUPS code
diff --git a/stdnum/es/cups.py b/stdnum/es/cups.py
new file mode 100644
index 0000000..5aa847a
--- /dev/null
+++ b/stdnum/es/cups.py
@@ -0,0 +1,113 @@
+# cups.py - functions for handling Spanish CUPS code
+# coding: utf-8
+#
+# Copyright (C) 2016 David García Garzón
+# Copyright (C) 2016 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
+
+"""CUPS (Código Unificado de Punto de Suministro, Supply Point Unified Code)
+
+CUPS codes are used in Spain as unique identifier for energy supply points.
+They are used both for electricity and pipelined gas.
+
+The format is set by the Energy Ministry, and individual codes are issued by
+each local distribution company. The number consist or 20 or 22 digits and is
+built up as follows:
+
+* LL: (letters) country (always 'ES' since it is a national code)
+* DDDD: (numbers) distribution company code (numeric)
+* CCCC CCCC CCCC: identifier within the distribution company (numeric)
+* EE: (letters) check digits
+* N: (number) border point sequence
+* T: (letter) kind of border point
+
+More information:
+
+* https://es.wikipedia.org/wiki/Código_Unificado_de_Punto_de_Suministro
+
+>>> validate('ES 1234-123456789012-JY')
+'ES1234123456789012JY'
+>>> validate('ES 1234-123456789012-JY 1T')
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> validate('ES 1234-123456789012-XY 1F')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> format('ES1234123456789012JY1F')
+'ES 1234 1234 5678 9012 JY 1F'
+"""
+
+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, ' -').strip().upper()
+
+
+def format(number):
+ """Reformat the passed number to the standard format."""
+ number = compact(number)
+ return ' '.join((
+ number[:2],
+ number[2:6],
+ number[6:10],
+ number[10:14],
+ number[14:18],
+ number[18:20],
+ number[20:],
+ )).strip()
+
+
+def calc_check_digits(number):
+ """Calculate the check digits for the number."""
+ alphabet = 'TRWAGMYFPDXBNJZSQVHLCKE'
+ check0, check1 = divmod(int(number[2:18]) % 529, 23)
+ return alphabet[check0] + alphabet[check1]
+
+
+def validate(number):
+ """Check to see if the number provided is a valid CUPS. This checks
+ length, formatting and check digits."""
+ number = compact(number)
+ if len(number) not in (20, 22):
+ raise InvalidLength()
+ if number[:2] != 'ES':
+ raise InvalidComponent()
+ if not number[2:18].isdigit():
+ raise InvalidFormat()
+ if number[20:]:
+ pnumber, ptype = number[20:]
+ if not pnumber.isdigit():
+ raise InvalidFormat()
+ if ptype not in 'FPRCXYZ':
+ raise InvalidFormat()
+ if calc_check_digits(number) != number[18:20]:
+ raise InvalidChecksum()
+ return number
+
+
+def is_valid(number):
+ """Check to see if the number provided is a valid CUPS."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False
diff --git a/tests/test_es_cups.doctest b/tests/test_es_cups.doctest
new file mode 100644
index 0000000..4779124
--- /dev/null
+++ b/tests/test_es_cups.doctest
@@ -0,0 +1,83 @@
+test_my_nric.doctest - more detailed doctests for stdnum.es.cups module
+
+Copyright (C) 2016 David García Garzón
+Copyright (C) 2016 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.es.cups. It
+tries to cover more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum.es import cups
+>>> from stdnum.exceptions import *
+
+
+>>> cups.compact('ES 1234-123456789012-jy')
+'ES1234123456789012JY'
+
+>>> cups.validate('ES 1234-123456789012-JY')
+'ES1234123456789012JY'
+>>> cups.validate('GB 1234-123456789012-JY')
+Traceback (most recent call last):
+ ...
+InvalidComponent: ...
+>>> cups.validate('ES 1234-12X456789012-JY')
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> cups.validate('ES 1234-12345678901X-JY')
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> cups.validate('ES 1234-12456789012-JY')
+Traceback (most recent call last):
+ ...
+InvalidLength: ...
+>>> cups.validate('ES 1234-123456789012-JY 1F')
+'ES1234123456789012JY1F'
+>>> cups.validate('ES 1234-123456789012-JY 1T')
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> cups.validate('ES 1234-123456789012-JY XF')
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> cups.validate('ES 1234-123456789012-XY 1F')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+
+>>> cups.is_valid('ES 1234-123456789012-JY 1F')
+True
+>>> cups.is_valid('ES 1234-123456789012-XY 1F')
+False
+
+>>> cups.format('ES1234123456789012JY')
+'ES 1234 1234 5678 9012 JY'
+>>> cups.format('ES1234123456789012JY1F')
+'ES 1234 1234 5678 9012 JY 1F'
+
+>>> cups.validate('ES 0987 5432 1098 7654 ZF')
+'ES0987543210987654ZF'
+>>> cups.validate('ES 1234 1234 5678 9012 JY')
+'ES1234123456789012JY'
+>>> cups.validate('ES 9750 2109 8765 4321 CQ')
+'ES9750210987654321CQ'
+>>> cups.validate('ES 0999 1100 1234 5678 EK')
+'ES0999110012345678EK'
-----------------------------------------------------------------------
Summary of changes:
stdnum/es/cups.py | 113 +++++++++++++++++++++++++++++++++++++++++++++
tests/test_es_cups.doctest | 83 +++++++++++++++++++++++++++++++++
2 files changed, 196 insertions(+)
create mode 100644 stdnum/es/cups.py
create mode 100644 tests/test_es_cups.doctest
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/
- python-stdnum branch master updated. 1.4-2-g466cec8,
Commits of the python-stdnum project