|
| 1 | +# postnummer.py - functions for handling Swedish postal codes |
| 2 | +# |
| 3 | +# Copyright (C) 2021 Michele Ciccozzi |
| 4 | +# |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation; either |
| 8 | +# version 2.1 of the License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This library is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +# Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public |
| 16 | +# License along with this library; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | +# 02110-1301 USA |
| 19 | + |
| 20 | +"""Postcode (the Swedish postal code). |
| 21 | +
|
| 22 | +The Swedish postal code consists of three numbers followed by two numbers, |
| 23 | +separated by a single space. |
| 24 | +
|
| 25 | +More information: |
| 26 | +
|
| 27 | +* https://en.wikipedia.org/wiki/Postal_codes_in_Sweden |
| 28 | +* https://sv.wikipedia.org/wiki/Postnummer_i_Sverige |
| 29 | +
|
| 30 | +>>> validate('114 18') |
| 31 | +'11418' |
| 32 | +>>> validate('SE-11418') |
| 33 | +'11418' |
| 34 | +>>> validate('1145 18') |
| 35 | +Traceback (most recent call last): |
| 36 | + ... |
| 37 | +InvalidLength: ... |
| 38 | +>>> format('11418') |
| 39 | +'114 18' |
| 40 | +""" |
| 41 | + |
| 42 | +from stdnum.exceptions import * |
| 43 | +from stdnum.util import clean, isdigits |
| 44 | + |
| 45 | + |
| 46 | +def compact(number): |
| 47 | + """Convert the number to the minimal representation. This strips the |
| 48 | + number of any valid separators and removes surrounding whitespace.""" |
| 49 | + number = clean(number, ' -').upper().strip() |
| 50 | + if number.startswith('SE'): |
| 51 | + number = number[2:] |
| 52 | + return number |
| 53 | + |
| 54 | + |
| 55 | +def validate(number): |
| 56 | + """Check if the number is in the correct format. This currently does not |
| 57 | + check whether the code corresponds to a real address.""" |
| 58 | + number = compact(number) |
| 59 | + if not isdigits(number) or number.startswith('0'): |
| 60 | + raise InvalidFormat() |
| 61 | + if len(number) != 5: |
| 62 | + raise InvalidLength() |
| 63 | + return number |
| 64 | + |
| 65 | + |
| 66 | +def is_valid(number): |
| 67 | + """Check if the number is a valid postal code.""" |
| 68 | + try: |
| 69 | + return bool(validate(number)) |
| 70 | + except ValidationError: |
| 71 | + return False |
| 72 | + |
| 73 | + |
| 74 | +def format(number): |
| 75 | + """Reformat the number to the standard presentation format.""" |
| 76 | + number = compact(number) |
| 77 | + return '%s %s' % (number[:3], number[3:]) |
0 commit comments