This is a validation function for Australian Business Numbers (ABNs). It tests whether an integer or string is a valid ABN (but not whether it is a legal ABN, i.e. if it belongs to the person or business entity that is quoting it).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | def isabn(obj):
"""isabn(string or int) -> True|False
Validate an ABN (Australian Business Number).
http://www.ato.gov.au/businesses/content.asp?doc=/content/13187.htm
Accepts an int or a string of exactly 11 digits and no leading zeroes.
Digits may be optionally separated with spaces. Any other input raises
TypeError or ValueError.
Return True if the argument is a valid ABN, otherwise False.
>>> isabn('53 004 085 616')
True
>>> isabn('93 004 085 616')
False
"""
if isinstance(obj, int):
if not 10**10 <= obj < 10**11:
raise ValueError('int out of range for an ABN')
obj = str(obj)
assert len(obj) == 11
if not isinstance(obj, str):
raise TypeError('expected a str or int but got %s' % type(obj))
obj = obj.replace(' ', '')
if len(obj) != 11:
raise ValueError('ABN must have exactly 11 digits')
if not obj.isdigit():
raise ValueError('non-digit found in ABN')
if obj.startswith('0'):
raise ValueError('leading zero not allowed in ABNs')
digits = [int(c) for c in obj]
digits[0] -= 1
weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
assert len(digits) == len(weights) == 11
chksum = sum(d*w for d,w in zip(digits, weights)) % 89
return chksum == 0
|
Tags: abn, validation