8000 Add a Dutch postal code module · sharoonthomas/python-stdnum@b0c47d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0c47d5

Browse files
committed
Add a Dutch postal code module
The Dutch postal code (postcode) consists of four digits followed by two characters and together with the house number should uniquely identify any address. Addresses trac ticket arthurdejong#7.
1 parent 73d05b0 commit b0c47d5

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

stdnum/nl/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# __init__.py - collection of Dutch numbers
22
# coding: utf-8
33
#
4-
# Copyright (C) 2012 Arthur de Jong
4+
# Copyright (C) 2012, 2013 Arthur de Jong
55
#
66
# This library is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU Lesser General Public
@@ -20,5 +20,6 @@
2020

2121
"""Collection of Dutch numbers."""
2222

23-
# provide vat as an alias
23+
# provide aliases
2424
from stdnum.nl import btw as vat
25+
from stdnum.nl import postcode as postcal_code

stdnum/nl/postcode.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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):
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

Comments
 (0)
0