|
| 1 | +# postcode.py - functions for handling Dutch postal codes |
| 2 | +# |
| 3 | +# Copyright (C) 2013 Arthur de Jong |
| 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 (Dutch postal code). |
| 21 | +
|
| 22 | +The Dutch postal code consists of four numbers followed by two letters. |
| 23 | +
|
| 24 | +>>> validate('2601 DC') |
| 25 | +'2601 DC' |
| 26 | +>>> validate('NL-2611ET') |
| 27 | +'2611 ET' |
| 28 | +>>> validate('26112 ET') |
| 29 | +Traceback (most recent call last): |
| 30 | + ... |
| 31 | +InvalidFormat: ... |
| 32 | +>>> validate('2611 SS') # a few letter combinations are banned |
| 33 | +Traceback (most recent call last): |
| 34 | + ... |
| 35 | +InvalidComponent: ... |
| 36 | +""" |
| 37 | + |
| 38 | +import re |
| 39 | + |
| 40 | +from stdnum.exceptions import * |
| 41 | +from stdnum.util import clean |
| 42 | + |
| 43 | + |
| 44 | +_postcode_re = re.compile(r'^(?P<pt1>[1-9][0-9]{3})(?P<pt2>[A-Z]{2})$') |
| 45 | + |
| 46 | + |
| 47 | +_postcode_blacklist = ('SA', 'SD' ,'SS') |
| 48 | + |
| 49 | + |
| 50 | +def compact(number): |
| 51 | + """Convert the number to the minimal representation. This strips the |
| 52 | + number of any valid separators and removes surrounding whitespace.""" |
| 53 | + number = clean(number, ' -').upper().strip() |
| 54 | + if number.startswith('NL'): |
| 55 | + number = number[2:] |
| 56 | + return number |
| 57 | + |
| 58 | + |
| 59 | +def validate(number): |
9908
code> | 60 | + """Checks to see if the number provided is in the correct format. |
| 61 | + This currently does not check whether the code corresponds to a real |
| 62 | + address.""" |
| 63 | + number = compact(number) |
| 64 | + match = _postcode_re.search(number) |
| 65 | + if not match: |
| 66 | + raise InvalidFormat() |
| 67 | + if match.group('pt2') in _postcode_blacklist: |
| 68 | + raise InvalidComponent() |
| 69 | + return '%s %s' % (match.group('pt1'), match.group('pt2')) |
| 70 | + |
| 71 | + |
| 72 | +def is_valid(number): |
| 73 | + """Checks to see if the number provided is a valid postal code.""" |
| 74 | + try: |
| 75 | + return bool(validate(number)) |
| 76 | + except ValidationError: |
| 77 | + return False |
0 commit comments