From ff3bcbd36b1ffe819e5d87c2dfa3b83279c8c314 Mon Sep 17 00:00:00 2001 From: Vincent Bastos Date: Mon, 19 Sep 2016 23:08:48 +1000 Subject: [PATCH] add Australian number: ABN, ACN, TFN --- stdnum/__init__.py | 3 ++ stdnum/au/__init__.py | 21 +++++++++++++ stdnum/au/abn.py | 72 +++++++++++++++++++++++++++++++++++++++++++ stdnum/au/acn.py | 70 +++++++++++++++++++++++++++++++++++++++++ stdnum/au/tfn.py | 70 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 236 insertions(+) create mode 100644 stdnum/au/__init__.py create mode 100644 stdnum/au/abn.py create mode 100644 stdnum/au/acn.py create mode 100644 stdnum/au/tfn.py diff --git a/stdnum/__init__.py b/stdnum/__init__.py index d12dcd47..e573fb81 100644 --- a/stdnum/__init__.py +++ b/stdnum/__init__.py @@ -29,6 +29,9 @@ * ar.cuit: CUIT (Código Único de Identificación Tributaria, Argentinian tax number) * at.businessid: Austrian Company Register Numbers * at.uid: UID (Umsatzsteuer-Identifikationsnummer, Austrian VAT number) +* au.abn: ABN Australian Business Number +* au.acn: ACN Australian Company Number +* au.tfn: TFN Australian Tax File Number * be.vat: BTW, TVA, NWSt (Belgian VAT number) * bg.egn: EGN (ЕГН, Единен граждански номер, Bulgarian personal identity codes) * bg.pnf: PNF (ЛНЧ, Личен номер на чужденец, Bulgarian number of a foreigner) diff --git a/stdnum/au/__init__.py b/stdnum/au/__init__.py new file mode 100644 index 00000000..85e943b7 --- /dev/null +++ b/stdnum/au/__init__.py @@ -0,0 +1,21 @@ +# __init__.py - collection of Australian numbers +# coding: utf-8 +# +# Copyright (C) 2015 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 Australian numbers.""" diff --git a/stdnum/au/abn.py b/stdnum/au/abn.py new file mode 100644 index 00000000..c28ff37d --- /dev/null +++ b/stdnum/au/abn.py @@ -0,0 +1,72 @@ +# abn.py - functions for handling Australian Business Numbers (ABNs) +# +# Copyright (C) 2016 Vincent Bastos +# +# 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 + +"""ABN. + +>>> validate('83 914 571 673') +'83914571673' +>>> validate('99 999 999 999') +Traceback (most recent call last): + ... +InvalidChecksum: ... +""" +import copy +import operator + +from stdnum.exceptions import * + + +def compact(number): + """Convert the number to the minimal representation. This strips the + number of any valid separators and removes surrounding whitespace.""" + number = number.replace(" ", "") + return number + + +def checksum(number): + """Calculate the checksum.""" + weights(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19) + temp_number = copy.copy(number) + temp_number[0] -= 1 + return sum(map(operator.mul, temp_number, weights)) % 89 + + +def validate(number): + """Checks to see if the number provided is a valid ABN number. This checks + the length, formatting and check digit.""" + number = compact(number) + if not number.isdigit(): + raise InvalidFormat() + if len(number) != 11: + raise InvalidLength() + if checksum(number) != 0: + raise InvalidChecksum() + return number + + +def is_valid(number): + """Checks to see if the number provided is a valid ABN number. This checks + the length, formatting and check digit.""" + try: + return bool(validate(number)) + except ValidationError: + return False + +def format(number): + return "{}{} {}{}{} {}{}{} {}{}{}".format(*number) diff --git a/stdnum/au/acn.py b/stdnum/au/acn.py new file mode 100644 index 00000000..675c7792 --- /dev/null +++ b/stdnum/au/acn.py @@ -0,0 +1,70 @@ +# acn.py - functions for handling Australian Company Numbers (ACNs) +# +# Copyright (C) 2016 Vincent Bastos +# +# 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 + +"""ACN. + +>>> validate('010 499 966') +'010499966' +>>> validate('999 999 999') +Traceback (most recent call last): + ... +InvalidChecksum: ... +""" + +import operator + +from stdnum.exceptions import * + + +def compact(number): + """Convert the number to the minimal representation. This strips the + number of any valid separators and removes surrounding whitespace.""" + number = number.replace(" ", "") + return number + + +def checksum(number): + """Calculate the checksum.""" + weights(8, 7, 6, 5, 4, 3, 2, 1) + return (10 - (sum(map(operator.mul, number, weights))) % 10) % 10 + + +def validate(number): + """Checks to see if the number provided is a valid ACN number. This checks + the length, formatting and check digit.""" + number = compact(number) + if not number.isdigit(): + 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 ACN number. This checks + the length, formatting and check digit.""" + try: + return bool(validate(number)) + except ValidationError: + return False + +def format(number): + return "{}{}{} {}{}{} {}{}{}".format(*number) diff --git a/stdnum/au/tfn.py b/stdnum/au/tfn.py new file mode 100644 index 00000000..a749797e --- /dev/null +++ b/stdnum/au/tfn.py @@ -0,0 +1,70 @@ +# tfn.py - functions for handling Australian Tax File Numbers (TFNs) +# +# Copyright (C) 2016 Vincent Bastos +# +# 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 + +"""TFN. + +>>> validate('876 543 210') +'876543210' +>>> validate('999 999 999') +Traceback (most recent call last): + ... +InvalidChecksum: ... +""" + +import operator + +from stdnum.exceptions import * + + +def compact(number): + """Convert the number to the minimal representation. This strips the + number of any valid separators and removes surrounding whitespace.""" + number = number.replace(" ", "") + return number + + +def checksum(number): + """Calculate the checksum.""" + weights(1, 4, 3, 7, 5, 8, 6, 9, 10) + return sum(map(operator.mul, number, weights)) % 11 + + +def validate(number): + """Checks to see if the number provided is a valid TFN number. This checks + the length, formatting and check digit.""" + number = compact(number) + if not number.isdigit(): + 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 TFN number. This checks + the length, formatting and check digit.""" + try: + return bool(validate(number)) + except ValidationError: + return False + +def format(number): + return "{}{}{} {}{}{} {}{}{}".format(*number)