au-identifiers 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""Validate and format Australian business identifiers: ABN, ACN, ARBN, TFN, Director ID and VIN."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"is_valid_abn", "normalise_abn", "format_abn",
|
|
9
|
+
"is_valid_acn", "normalise_acn", "format_acn",
|
|
10
|
+
"is_valid_arbn", "normalise_arbn", "format_arbn",
|
|
11
|
+
"is_valid_tfn", "normalise_tfn",
|
|
12
|
+
"is_valid_director_id", "normalise_director_id",
|
|
13
|
+
"is_valid_vin", "normalise_vin",
|
|
14
|
+
]
|
|
15
|
+
__version__ = "1.0.0"
|
|
16
|
+
|
|
17
|
+
_SEPARATORS = re.compile(r"[ \t\n\r\v\f-]")
|
|
18
|
+
_ABN_WEIGHTS = (10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
|
|
19
|
+
_ACN_WEIGHTS = (8, 7, 6, 5, 4, 3, 2, 1)
|
|
20
|
+
_TFN_WEIGHTS_9 = (1, 4, 3, 7, 5, 8, 6, 9, 10)
|
|
21
|
+
_TFN_WEIGHTS_8 = (1, 4, 3, 7, 5, 8, 6, 10)
|
|
22
|
+
_VIN = re.compile(r"[A-HJ-NPR-Z0-9]{17}")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _digits(value: object, lengths: tuple[int, ...]) -> str | None:
|
|
26
|
+
if isinstance(value, bool) or not isinstance(value, (str, int)) or (isinstance(value, int) and value < 0):
|
|
27
|
+
return None
|
|
28
|
+
compact = _SEPARATORS.sub("", str(value))
|
|
29
|
+
if len(compact) not in lengths or not compact.isascii() or not compact.isdigit():
|
|
30
|
+
return None
|
|
31
|
+
return compact
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _sum(digits: str, weights: tuple[int, ...]) -> int:
|
|
35
|
+
return sum(int(d) * w for d, w in zip(digits, weights))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def normalise_abn(value: object) -> str | None:
|
|
39
|
+
abn = _digits(value, (11,))
|
|
40
|
+
if abn is None:
|
|
41
|
+
return None
|
|
42
|
+
# Subtract 1 from the first digit, then the weighted sum must be divisible by 89.
|
|
43
|
+
total = (int(abn[0]) - 1) * _ABN_WEIGHTS[0] + _sum(abn[1:], _ABN_WEIGHTS[1:])
|
|
44
|
+
return abn if total % 89 == 0 else None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def is_valid_abn(value: object) -> bool:
|
|
48
|
+
return normalise_abn(value) is not None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def format_abn(value: object) -> str | None:
|
|
52
|
+
abn = normalise_abn(value)
|
|
53
|
+
return f"{abn[:2]} {abn[2:5]} {abn[5:8]} {abn[8:]}" if abn else None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def normalise_acn(value: object) -> str | None:
|
|
57
|
+
acn = _digits(value, (9,))
|
|
58
|
+
if acn is None:
|
|
59
|
+
return None
|
|
60
|
+
return acn if int(acn[8]) == (10 - _sum(acn, _ACN_WEIGHTS) % 10) % 10 else None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def is_valid_acn(value: object) -> bool:
|
|
64
|
+
return normalise_acn(value) is not None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def format_acn(value: object) -> str | None:
|
|
68
|
+
acn = normalise_acn(value)
|
|
69
|
+
return f"{acn[:3]} {acn[3:6]} {acn[6:]}" if acn else None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
normalise_arbn = normalise_acn
|
|
73
|
+
is_valid_arbn = is_valid_acn
|
|
74
|
+
format_arbn = format_acn
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def normalise_tfn(value: object, *, individual: bool = False) -> str | None:
|
|
78
|
+
# 8-digit TFNs (organisations only) have their own weights. A 9-digit TFN with a leading
|
|
79
|
+
# zero passes on either the 9-digit check or the 8-digit check of the rest.
|
|
80
|
+
tfn = _digits(value, (9,) if individual else (8, 9))
|
|
81
|
+
if tfn is None:
|
|
82
|
+
return None
|
|
83
|
+
if len(tfn) == 9 and len(tfn.lstrip("0")) >= 8 and _sum(tfn, _TFN_WEIGHTS_9) % 11 == 0:
|
|
84
|
+
return tfn
|
|
85
|
+
core = tfn.lstrip("0")
|
|
86
|
+
weights = {9: _TFN_WEIGHTS_9, 8: _TFN_WEIGHTS_8}.get(len(core))
|
|
87
|
+
return tfn if weights and _sum(core, weights) % 11 == 0 else None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def is_valid_tfn(value: object, *, individual: bool = False) -> bool:
|
|
91
|
+
return normalise_tfn(value, individual=individual) is not None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def normalise_director_id(value: object) -> str | None:
|
|
95
|
+
# Format only. The check digit method is only given to ATO-registered software providers.
|
|
96
|
+
did = _digits(value, (15,))
|
|
97
|
+
return did if did and did.startswith("036") else None
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def is_valid_director_id(value: object) -> bool:
|
|
101
|
+
return normalise_director_id(value) is not None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def normalise_vin(value: object) -> str | None:
|
|
105
|
+
if not isinstance(value, str):
|
|
106
|
+
return None
|
|
107
|
+
vin = value.strip(" \t\n\r\v\f")
|
|
108
|
+
if not vin.isascii():
|
|
109
|
+
return None
|
|
110
|
+
vin = vin.upper()
|
|
111
|
+
return vin if _VIN.fullmatch(vin) else None
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def is_valid_vin(value: object) -> bool:
|
|
115
|
+
return normalise_vin(value) is not None
|
au_identifiers/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: au-identifiers
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Validate and format Australian identifiers: ABN, ACN, ARBN, TFN, Director ID and VIN.
|
|
5
|
+
Project-URL: Homepage, https://businessapi.com.au
|
|
6
|
+
Project-URL: Source, https://github.com/BusinessAPI/au-identifiers-python
|
|
7
|
+
Project-URL: Issues, https://github.com/BusinessAPI/au-identifiers-python/issues
|
|
8
|
+
Author-email: Business API <dev@businessapi.com.au>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: abn,acn,arbn,asic,ato,australia,checksum,director-id,tfn,validation,vin
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# Australian ABN, ACN and TFN Validation for Python
|
|
22
|
+
|
|
23
|
+
[](https://github.com/BusinessAPI/au-identifiers-python/actions/workflows/test.yml) [](LICENSE)
|
|
24
|
+
|
|
25
|
+
Validate and format Australian identifiers in Python: ABN, ACN, ARBN, TFN, Director ID and VIN.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install au-identifiers
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import au_identifiers as au
|
|
33
|
+
|
|
34
|
+
au.is_valid_abn("51 824 753 556") # True
|
|
35
|
+
au.format_abn("51824753556") # "51 824 753 556"
|
|
36
|
+
au.format_acn("004085616") # "004 085 616"
|
|
37
|
+
au.is_valid_tfn("15195606") # True (8-digit, organisation)
|
|
38
|
+
au.is_valid_tfn("15195606", individual=True) # False
|
|
39
|
+
au.is_valid_director_id("036 123 456 789 012") # True
|
|
40
|
+
au.normalise_vin(" 1hgcm82633a004352 ") # "1HGCM82633A004352"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Each identifier has `is_valid_*` and `normalise_*`. ABN, ACN and ARBN also have `format_*`. `normalise_*` and `format_*` return `None` for invalid input. The ARBN functions are aliases of the ACN ones. Python 3.9+, typed, no dependencies.
|
|
44
|
+
|
|
45
|
+
## Rules
|
|
46
|
+
|
|
47
|
+
| | Format | Check |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| ABN | 11 digits | ATO modulus 89 |
|
|
50
|
+
| ACN, ARBN | 9 digits | ASIC check digit |
|
|
51
|
+
| TFN | 8 or 9 digits | ATO modulus 11 |
|
|
52
|
+
| Director ID | 15 digits, starts with 036 | Format only |
|
|
53
|
+
| VIN | 17 characters, no I, O or Q | Format only (ISO 3779) |
|
|
54
|
+
|
|
55
|
+
Spaces and hyphens in numbers are ignored. VINs are trimmed and upper-cased. Anything else makes the value invalid. Pass ACNs, TFNs and Director IDs as strings so leading zeros aren't lost.
|
|
56
|
+
|
|
57
|
+
## How the checks work
|
|
58
|
+
|
|
59
|
+
### ABN
|
|
60
|
+
|
|
61
|
+
Subtract 1 from the first digit, multiply the 11 digits by 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 and add them up. The ABN is valid if the total divides evenly by 89. For 51 824 753 556 the total is 534, which is 89 x 6.
|
|
62
|
+
|
|
63
|
+
### ACN and ARBN
|
|
64
|
+
|
|
65
|
+
Multiply the first 8 digits by 8, 7, 6, 5, 4, 3, 2, 1 and add them up. Take the remainder after dividing by 10 and subtract it from 10 (10 becomes 0). That's the check digit, and it must match the last digit. For 004 085 616 the total is 84, so the check digit is 6.
|
|
66
|
+
|
|
67
|
+
### TFN
|
|
68
|
+
|
|
69
|
+
Multiply the 9 digits by 1, 4, 3, 7, 5, 8, 6, 9, 10 and add them up. The TFN is valid if the total divides evenly by 11. For 123 456 782 the total is 253, which is 11 x 23.
|
|
70
|
+
|
|
71
|
+
8-digit TFNs (organisations only) use the weights 1, 4, 3, 7, 5, 8, 6, 10. A 9-digit TFN with a leading zero is valid if either the 9-digit check or the 8-digit check on the rest passes. Set the individual option for a person's TFN, which is always 9 digits.
|
|
72
|
+
|
|
73
|
+
### Director ID and VIN
|
|
74
|
+
|
|
75
|
+
The Director ID check digit method is only given to ATO-registered software providers, so it's a format check here. VINs are checked for 17 characters with no I, O or Q (ISO 3779). Australia doesn't use the VIN check digit.
|
|
76
|
+
|
|
77
|
+
## Valid isn't registered
|
|
78
|
+
|
|
79
|
+
These checks run offline and only tell you a number is well formed. To check an ABN or ACN is registered and active, or to register companies, ABNs and business names, use the [Business API ABN lookup and company registration API](https://businessapi.com.au).
|
|
80
|
+
|
|
81
|
+
Also available for [JavaScript / TypeScript](https://github.com/BusinessAPI/au-identifiers-js), [PHP](https://github.com/BusinessAPI/au-identifiers-php), [.NET](https://github.com/BusinessAPI/au-identifiers-dotnet), [Java](https://github.com/BusinessAPI/au-identifiers-java).
|
|
82
|
+
|
|
83
|
+
## Licence
|
|
84
|
+
|
|
85
|
+
MIT
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
au_identifiers/__init__.py,sha256=mimTwRU2RJYVdk5LljebIMPaiGSvExKtgYvOYL0KpEg,3774
|
|
2
|
+
au_identifiers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
au_identifiers-1.0.0.dist-info/METADATA,sha256=on7R_l1ucSDiJCzRgoeIH-3sWKbLtxGN99drzAFbK-4,4336
|
|
4
|
+
au_identifiers-1.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
5
|
+
au_identifiers-1.0.0.dist-info/licenses/LICENSE,sha256=LwhqffapJjaKbrFwXLGE6bUw1Q-QkfYUPpLjNm58ydI,1077
|
|
6
|
+
au_identifiers-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Business API Pty Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|