Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2016-10-14 18:33:05 +0200
committerArthur de Jong <arthur@arthurdejong.org>2016-10-14 23:09:37 +0200
commit8028c3abe0a805577fe7bef3f0d4840264429071 (patch)
tree0f32aedaa23efe7e89cb888569f511a5b0b9c4c6
parent70b94ee719cfe3f40aa2efe91370104a171c8677 (diff)
Add Australian Company Number
Based on the implementation provided by Vincent Bastos <vincent@lavalab.com.au> See https://github.com/arthurdejong/python-stdnum/pull/40
-rw-r--r--stdnum/au/acn.py91
-rw-r--r--tests/test_au_acn.doctest125
2 files changed, 216 insertions, 0 deletions
diff --git a/stdnum/au/acn.py b/stdnum/au/acn.py
new file mode 100644
index 0000000..ed99d3d
--- /dev/null
+++ b/stdnum/au/acn.py
@@ -0,0 +1,91 @@
+# acn.py - functions for handling Australian Company Numbers (ACNs)
+#
+# Copyright (C) 2016 Vincent Bastos
+# Copyright (C) 2016 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
+
+"""ACN (Australian Company Number).
+
+The Australian Company Number (ACN) is a company identifier issued by the
+Australian Securities and Investments Commission.
+
+More information:
+
+* https://en.wikipedia.org/wiki/Australian_Company_Number
+
+>>> validate('004 085 616')
+'004085616'
+>>> validate('010 499 966')
+'010499966'
+>>> validate('999 999 999')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> format('004085616')
+'004 085 616'
+>>> to_abn('002 724 334')
+'43002724334'
+"""
+
+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."""
+ return clean(number, ' ').strip()
+
+
+def calc_check_digit(number):
+ """Calculate the checksum."""
+ return str((sum(int(n) * (i - 8) for i, n in enumerate(number))) % 10)
+
+
+def validate(number):
+ """Checks to see if the number provided is a valid ACN. This checks the
+ length, formatting and check digit."""
+ number = compact(number)
+ if not number.isdigit():
+ raise InvalidFormat()
+ if len(number) != 9:
+ raise InvalidLength()
+ if calc_check_digit(number) != number[-1]:
+ raise InvalidChecksum()
+ return number
+
+
+def is_valid(number):
+ """Checks to see if the number provided is a valid ACN. This checks the
+ length, formatting and check digit."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False
+
+
+def format(number):
+ """Reformat the passed number to the standard format."""
+ number = compact(number)
+ return ' '.join((number[0:3], number[3:6], number[6:]))
+
+
+def to_abn(number):
+ """Convert the number to an Australian Business Number (ABN)."""
+ from stdnum.au import abn
+ number = compact(number)
+ return abn.calc_check_digits(number) + number
diff --git a/tests/test_au_acn.doctest b/tests/test_au_acn.doctest
new file mode 100644
index 0000000..da7bd4a
--- /dev/null
+++ b/tests/test_au_acn.doctest
@@ -0,0 +1,125 @@
+test_au_acn.doctest - more detailed doctests for the stdnum.au.acn module
+
+Copyright (C) 2016 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
+
+
+This file contains more detailed doctests for the stdnum.au.acn module. It
+tries to validate a number of numbers that have been found online.
+
+>>> from stdnum.au import acn, abn
+>>> from stdnum.exceptions import *
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 000 024 733
+... 001 002 731
+... 001 976 272
+... 002 724 334
+... 002 955 722
+... 003 855 561
+... 004 071 854
+... 004 235 063
+... 004 394 763
+... 005 957 004
+... 007 433 623
+... 050 539 350
+... 055 980 204
+... 082 930 916
+... 088 952 023
+... 093 966 888 
+... 099503456
+... 104 045 089
+... 104 128 001
+... 112 045 002
+... 116 306 453
+... 125 295 712 
+... 135 427 075 
+... 141 800 357
+... 143477632
+...
+... 000 000 019
+... 000 250 000
+... 000 500 005
+... 000 750 005
+... 001 000 004
+... 001 250 004
+... 001 500 009
+... 001 749 999
+... 001 999 999
+... 002 249 998
+... 002 499 998
+... 002 749 993
+... 002 999 993
+... 003 249 992
+... 003 499 992
+... 003 749 988
+... 003 999 988
+... 004 249 987
+... 004 499 987
+... 004 749 982
+... 004 999 982
+... 005 249 981
+... 005 499 981
+... 005 749 986
+... 005 999 977
+... 006 249 976
+... 006 499 976
+... 006 749 980
+... 006 999 980
+... 007 249 989
+... 007 499 989
+... 007 749 975
+... 007 999 975
+... 008 249 974
+... 008 499 974
+... 008 749 979
+... 008 999 979
+... 009 249 969
+... 009 499 969
+... 009 749 964
+... 009 999 964
+... 010 249 966
+... 010 499 966
+... 010 749 961
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not acn.is_valid(x)]
+[]
+
+
+These numbers have been found in combination with an existing ABN.
+
+>>> numbers = '''
+...
+... 000 024 733 / 79 000 024 733
+... 002 724 334 / 43002724334
+... 004 071 854 / 56 004 071 854
+... 004 235 063 / 63 004 235 063
+... 004 394 763 / 74 004 394 763
+... 055 980 204 / 31 055 980 204
+... 104 045 089 / 97 104 045 089
+... 112 045 002 / 19 112 045 002
+... 143477632 / 28143477632
+...
+... '''
+>>> lines = (l.split('/') for l in numbers.splitlines() if l)
+>>> [(x, y) for x, y in lines if acn.to_abn(x) != abn.compact(y)]
+[]