File tree Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change 1
1
# casrn.py - functions for handling CAS Registry Numbers
2
2
#
3
- # Copyright (C) 2017 Arthur de Jong
3
+ # Copyright (C) 2017-2022 Arthur de Jong
4
4
#
5
5
# This library is free software; you can redistribute it and/or
6
6
# modify it under the terms of the GNU Lesser General Public
32
32
Traceback (most recent call last):
33
33
...
34
34
InvalidChecksum: ...
35
+ >>> validate('012770-26-2')
36
+ Traceback (most recent call last):
37
+ ...
38
+ InvalidFormat: ...
35
39
"""
36
40
41
+ import re
42
+
37
43
from stdnum .exceptions import *
38
44
from stdnum .util import clean , isdigits
39
45
40
46
47
+ _cas_re = re .compile (r'^[1-9][0-9]{1,6}-[0-9]{2}-[0-9]$' )
48
+
49
+
41
50
def compact (number ):
42
51
"""Convert the number to the minimal representation."""
43
52
number = clean (number , ' ' ).strip ()
@@ -59,9 +68,7 @@ def validate(number):
59
68
number = compact (number )
60
69
if not 7 <= len (number ) <= 12 :
61
70
raise InvalidLength ()
62
- if not isdigits (number [:- 5 ]) or not isdigits (number [- 4 :- 2 ]):
63
- raise InvalidFormat ()
64
- if number [- 2 ] != '-' or number [- 5 ] != '-' :
71
+ if not _cas_re .match (number ):
65
72
raise InvalidFormat ()
66
73
if number [- 1 ] != calc_check_digit (number [:- 1 ]):
67
74
raise InvalidChecksum ()
You can’t perform that action at this time.
0 commit comments