Skip to content

Commit acb6934

Browse files
committed
Add CAS Registry Number
1 parent 2b6e087 commit acb6934

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

stdnum/cas.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# cas.py - functions for handling CAS registry numbers
2+
#
3+
# Copyright (C) 2022 Arthur de Jong
4+
#
5+
# This library is free software; you can redistribute it and/or
6+
# modify it under the terms of the GNU Lesser General Public
7+
# License as published by the Free Software Foundation; either
8+
# version 2.1 of the License, or (at your option) any later version.
9+
#
10+
# This library is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
# Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with this library; if not, write to the Free Software
17+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
# 02110-1301 USA
19+
20+
"""CAS Registry Number.
21+
22+
A CAS Registry Number, CAS RN or CAS number is a unique identified assign to
23+
chemical substances. The number is issued by the Chemical Abstracts Service
24+
(CAS).
25+
26+
The number consists of 5 to 10 digits and is assigned sequentially and
27+
contains a check digit.
28+
29+
More information:
30+
31+
* https://en.wikipedia.org/wiki/CAS_Registry_Number
32+
* https://www.cas.org/cas-data/cas-registry
33+
34+
>>> validate('12770-26-2')
35+
'12770-26-2'
36+
>>> validate('12770-29-2')
37+
Traceback (most recent call last):
38+
...
39+
InvalidChecksum: ...
40+
>>> validate('012770-26-2')
41+
Traceback (most recent call last):
42+
...
43+
InvalidFormat: ...
44+
"""
45+
46+
import re
47+
48+
from stdnum.exceptions import *
49+
from stdnum.util import clean
50+
51+
52+
_cas_re = re.compile(r'^[1-9][0-9]{1,6}-[0-9]{2}-[0-9]$')
53+
54+
55+
def compact(number):
56+
"""Convert the number to the minimal representation."""
57+
return clean(number).strip()
58+
59+
60+
def calc_check_digit(number):
61+
"""Calculate the check digit. The number passed should not have the check
62+
digit included."""
63+
number = clean(number, '-').strip()
64+
return str(sum((i + 1) * int(n) for i, n in enumerate(reversed(number))) % 10)
65+
66+
67+
def validate(number):
68+
"""Check if the number is a valid CAS Registry Number."""
69+
number = compact(number)
70+
if not _cas_re.match(number):
71+
raise InvalidFormat()
72+
if not number[-1] == calc_check_digit(number[:-1]):
73+
raise InvalidChecksum()
74+
return number
75+
76+
77+
def is_valid(number):
78+
"""Check if the number is a valid CAS Registry Number."""
79+
try:
80+
return bool(validate(number))
81+
except ValidationError:
82+
return False

0 commit comments

Comments
 (0)