Switch to python3

This commit is contained in:
j 2014-09-30 18:15:32 +02:00
commit 9ba4b6a91a
5286 changed files with 677347 additions and 576888 deletions

View file

@ -0,0 +1,25 @@
# __init__.py - collection of Dutch numbers
# coding: utf-8
#
# Copyright (C) 2012, 2013 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
"""Collection of Dutch numbers."""
# provide aliases
from stdnum.nl import btw as vat
from stdnum.nl import postcode as postcal_code

View file

@ -0,0 +1,80 @@
# brin.py - functions for handling Brin numbers
#
# Copyright (C) 2013 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
"""Brin number (Dutch number for schools).
The Brin (Basis Registratie Instellingen) is a number to identify schools
and related institutions. The number consists of four alphanumeric
characters, sometimes extended with two digits to indicate the site (this
complete code is called the vestigingsnummer).
The register of these numbers can be downloaded from:
http://www.duo.nl/organisatie/open_onderwijsdata/databestanden/default.asp
>>> validate('05 KO')
'05KO'
>>> validate('07NU 00')
'07NU00'
>>> validate('12KB1')
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('30AJ0A') # location code has letter
Traceback (most recent call last):
...
InvalidFormat: ...
"""
import re
from stdnum.exceptions import *
from stdnum.util import clean
# this regular expression is based on what was found in the online
# database: the first two digits are always numeric, followed by two
# letters and an optional two letter location identifier
_brin_re = re.compile(r'^(?P<brin>[0-9]{2}[A-Z]{2})(?P<location>[0-9]{2})?$')
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, ' -.').upper().strip()
def validate(number):
"""Checks to see if the number provided is in the correct format.
This currently does not check whether the number points to a
registered school."""
number = compact(number)
if len(number) not in (4, 6):
raise InvalidLength()
match = _brin_re.search(number)
if not match:
raise InvalidFormat()
return number
def is_valid(number):
"""Checks to see if the number provided is a valid Brin number."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,83 @@
# bsn.py - functions for handling BSNs
#
# Copyright (C) 2010, 2011, 2012, 2013 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
"""BSN (Burgerservicenummer, Dutch national identification number).
The BSN is a number with up to 9 digits (the leading 0's are commonly left
out) which is used as the Dutch national identification number.
>>> validate('1112.22.333')
'111222333'
>>> validate('1112.52.333')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('1112223334')
Traceback (most recent call last):
...
InvalidLength: ...
>>> format('111222333')
'1112.22.333'
"""
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."""
number = clean(number, ' -.').strip()
# pad with leading zeroes
return (9 - len(number)) * '0' + number
def checksum(number):
"""Calculate the checksum over the number. A valid number should have
a check digit of 0."""
return (sum((9 - i) * int(n) for i, n in enumerate(number[:-1])) -
int(number[-1])) % 11
def validate(number):
"""Checks to see if the number provided is a valid BSN. This checks
the length and whether the check digit is correct."""
number = compact(number)
if not number.isdigit() or int(number) <= 0:
raise InvalidFormat()
if len(number) != 9:
raise InvalidLength()
if checksum(number) != 0:
raise InvalidChecksum()
return number
def is_valid(number):
"""Checks to see if the number provided is a valid BSN. This checks
the length and whether the check digit is correct."""
try:
return bool(validate(number))
except ValidationError:
return False
def format(number):
"""Reformat the passed number to the standard format."""
number = compact(number)
return number[:4] + '.' + number[4:6] + '.' + number[6:]

View file

@ -0,0 +1,70 @@
# btw.py - functions for handling Dutch VAT numbers
#
# Copyright (C) 2012, 2013 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
"""BTW-nummer (Omzetbelastingnummer, the Dutch VAT number).
The BTW-nummer is the Dutch number for VAT. It consists of a RSIN or BSN
followed by a B and two digits that identify the unit within the
organisation (usually 01).
>>> validate('004495445B01')
'004495445B01'
>>> validate('NL4495445B01')
'004495445B01'
>>> validate('123456789B90')
Traceback (most recent call last):
...
InvalidChecksum: ...
"""
from stdnum.exceptions import *
from stdnum.nl import bsn
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."""
number = clean(number, ' -.').upper().strip()
if number.startswith('NL'):
number = number[2:]
return bsn.compact(number[:-3]) + number[-3:]
def validate(number):
"""Checks to see if the number provided is a valid BTW number. This checks
the length, formatting and check digit."""
number = compact(number)
if not number[10:].isdigit() or int(number[10:]) <= 0:
raise InvalidFormat()
if len(number) != 12:
raise InvalidLength()
if number[9] != 'B':
raise InvalidFormat()
bsn.validate(number[:9])
return number
def is_valid(number):
"""Checks to see if the number provided is a valid BTW number. This checks
the length, formatting and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,72 @@
# onderwijsnummer.py - functions for handling onderwijsnummers
#
# Copyright (C) 2012, 2013 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
"""Onderwijsnummer (Dutch student school number).
The onderwijsnummers (education number) is very similar to the BSN (Dutch
national identification number) for students without a BSN. It uses a
checksum mechanism similar to the BSN.
>>> validate('1012.22.331')
'101222331'
>>> validate('100252333')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('1012.22.3333')
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('2112.22.337') # number must start with 10
Traceback (most recent call last):
...
InvalidFormat: ...
"""
from stdnum.exceptions import *
from stdnum.nl.bsn import compact, checksum
__all__ = ['compact', 'validate', 'is_valid']
def validate(number):
"""Checks to see if the number provided is a valid onderwijsnummer.
This checks the length and whether the check digit is correct and
whether it starts with the right sequence."""
number = compact(number)
if not number.isdigit() or int(number) <= 0:
raise InvalidFormat()
if not number.startswith('10'):
raise InvalidFormat()
if len(number) != 9:
raise InvalidLength()
if checksum(number) != 5:
raise InvalidChecksum()
return number
def is_valid(number):
"""Checks to see if the number provided is a valid onderwijsnummer.
This checks the length and whether the check digit is correct and
whether it starts with the right sequence."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,77 @@
# postcode.py - functions for handling Dutch postal codes
#
# Copyright (C) 2013 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
"""Postcode (Dutch postal code).
The Dutch postal code consists of four numbers followed by two letters.
>>> validate('2601 DC')
'2601 DC'
>>> validate('NL-2611ET')
'2611 ET'
>>> validate('26112 ET')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('2611 SS') # a few letter combinations are banned
Traceback (most recent call last):
...
InvalidComponent: ...
"""
import re
from stdnum.exceptions import *
from stdnum.util import clean
_postcode_re = re.compile(r'^(?P<pt1>[1-9][0-9]{3})(?P<pt2>[A-Z]{2})$')
_postcode_blacklist = ('SA', 'SD' ,'SS')
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('NL'):
number = number[2:]
return number
def validate(number):
"""Checks to see if the number provided is in the correct format.
This currently does not check whether the code corresponds to a real
address."""
number = compact(number)
match = _postcode_re.search(number)
if not match:
raise InvalidFormat()
if match.group('pt2') in _postcode_blacklist:
raise InvalidComponent()
return '%s %s' % (match.group('pt1'), match.group('pt2'))
def is_valid(number):
"""Checks to see if the number provided is a valid postal code."""
try:
return bool(validate(number))
except ValidationError:
return False