lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.16-19-g8071444

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

python-stdnum branch master updated. 1.16-19-g8071444



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  80714444f92f7c0a80118b7fdf64537c5b9975b7 (commit)
      from  424e408707a66fa97e95d3fd109c4a2ff44af08f (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=80714444f92f7c0a80118b7fdf64537c5b9975b7

commit 80714444f92f7c0a80118b7fdf64537c5b9975b7
Author: michele <michele.ciccozzi@medpeople.se>
Date:   Fri Aug 20 16:24:58 2021 +0200

    Add swedish postcode validator
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/271

diff --git a/stdnum/se/__init__.py b/stdnum/se/__init__.py
index 76c4a0f..23b98c5 100644
--- a/stdnum/se/__init__.py
+++ b/stdnum/se/__init__.py
@@ -22,3 +22,4 @@
 
 # provide aliases
 from stdnum.se import personnummer as personalid  # noqa: F401
+from stdnum.se import postnummer as postal_code  # noqa: F401
diff --git a/stdnum/se/postnummer.py b/stdnum/se/postnummer.py
new file mode 100644
index 0000000..b2246db
--- /dev/null
+++ b/stdnum/se/postnummer.py
@@ -0,0 +1,77 @@
+# postnummer.py - functions for handling Swedish postal codes
+#
+# Copyright (C) 2021 Michele Ciccozzi
+#
+# 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
+
+"""Postcode (the Swedish postal code).
+
+The Swedish postal code consists of three numbers followed by two numbers,
+separated by a single space.
+
+More information:
+
+* https://en.wikipedia.org/wiki/Postal_codes_in_Sweden
+* https://sv.wikipedia.org/wiki/Postnummer_i_Sverige
+
+>>> validate('114 18')
+'11418'
+>>> validate('SE-11418')
+'11418'
+>>> validate('1145 18')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> format('11418')
+'114 18'
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean, isdigits
+
+
+def compact(number):
+    """Convert the number to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    number = clean(number, ' -').upper().strip()
+    if number.startswith('SE'):
+        number = number[2:]
+    return number
+
+
+def validate(number):
+    """Check if the number is in the correct format. This currently does not
+    check whether the code corresponds to a real address."""
+    number = compact(number)
+    if not isdigits(number) or number.startswith('0'):
+        raise InvalidFormat()
+    if len(number) != 5:
+        raise InvalidLength()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid postal code."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def format(number):
+    """Reformat the number to the standard presentation format."""
+    number = compact(number)
+    return '%s %s' % (number[:3], number[3:])
diff --git a/tests/test_se_postnummer.doctest b/tests/test_se_postnummer.doctest
new file mode 100644
index 0000000..dc86529
--- /dev/null
+++ b/tests/test_se_postnummer.doctest
@@ -0,0 +1,75 @@
+test_se_postnummer.doctest - more detailed doctests for stdnum.se.postnummer 
module
+
+Copyright (C) 2021 Michele Ciccozzi
+
+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.se.postnummer
+module. It tries to cover more corner cases and detailed functionality that
+is not really useful as module documentation.
+
+
+>>> from stdnum.se import postnummer
+
+
+Test for various formats and corner cases.
+
+>>> postnummer.is_valid('114 18')
+True
+>>> postnummer.is_valid('SE-114 18')
+True
+>>> postnummer.is_valid('114018')
+False
+>>> postnummer.is_valid('01418')
+False
+
+>>> postnummer.validate('114 18')
+'11418'
+>>> postnummer.validate('SE-114 18')
+'11418'
+>>> postnummer.validate('11418')
+'11418'
+
+>>> postnummer.format('114 18')
+'114 18'
+>>> postnummer.format('SE-114 18')
+'114 18'
+>>> postnummer.format('11418')
+'114 18'
+
+
+Invalid values are rejected.
+
+
+>>> postnummer.validate('114180')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> postnummer.validate('1 14 18')
+'11418'
+>>> postnummer.validate('a' * 10)
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> postnummer.validate('a' * 11)
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> postnummer.validate('01234')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...

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

Summary of changes:
 stdnum/se/__init__.py                       |  1 +
 stdnum/{nl/postcode.py => se/postnummer.py} | 58 +++++++++++-----------
 tests/test_se_postnummer.doctest            | 75 +++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 31 deletions(-)
 copy stdnum/{nl/postcode.py => se/postnummer.py} (61%)
 create mode 100644 tests/test_se_postnummer.doctest


hooks/post-receive
-- 
python-stdnum