Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdnum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions stdnum/au/__init__.py
Original file line number Diff line number Diff line change
@@ -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."""
72 changes: 72 additions & 0 deletions stdnum/au/abn.py
Original file line number Diff line number Diff line change
@@ -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)
70 changes: 70 additions & 0 deletions stdnum/au/acn.py
Original file line number Diff line number Diff line change
@@ -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)
70 changes: 70 additions & 0 deletions stdnum/au/tfn.py
Original file line number Diff line number Diff line change
@@ -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)