33"""
44Class for CAS Registry Numbers® (CAS RN®)
55allwos to manage, check and sort CAS Registry Numbers®
6- see https://www.cas.org/support/documentation/chemical-substances/checkdig for a complete specification of the CAS Registry Numbers®
6+ see https://www.cas.org/support/documentation/chemical-substances/checkdig
7+ for a complete specification of the CAS Registry Numbers®
78and the calculation method to determine the check digit
89"""
910
@@ -15,47 +16,48 @@ def __init__(self, cas_rn):
1516 self .cas_integer = cas_rn
1617 # convert integer into (formatted) CAS string
1718 self .cas_string = re .sub (
18- "^(\d{2,7})(\d{2})(\d{1})$" , "\g<1>-\g<2>-\g<3>" , str (cas_rn )
19+ r "^(\d{2,7})(\d{2})(\d{1})$" , r "\g<1>-\g<2>-\g<3>" , str (cas_rn )
1920 )
2021 # case that cas_rn is a string
2122 elif isinstance (cas_rn , str ):
2223 self .cas_string = cas_rn
2324 # case that cas_rn is neither an integer nor a string
2425 else :
2526 raise TypeError (
26- f"Invalid CAS Registry Number format '{ cas_rn } ' (expected an integer (<class 'int'>) or a string (<class 'str'>), but found { type (cas_rn )} )"
27+ f"Invalid CAS Registry Number format '{ cas_rn } ' (expected an integer (<class 'int'>) "
28+ f"or a string (<class 'str'>), but found { type (cas_rn )} )"
2729 )
2830 # extract check digit = last digit of the CAS number
2931 self .check_digit = int (str (cas_rn )[- 1 ])
3032
31- ### default string output for CAS Registry Numbers
33+ # default string output for CAS Registry Numbers
3234 def __str__ (self ):
3335 return str (self .cas_string )
3436
35- ### defines a string format for CAS Registry Numbers
37+ # defines a string format for CAS Registry Numbers
3638 def __format__ (self , format_spec ):
3739 return f"{ self .cas_string :{format_spec }} "
3840
39- ### checks if two CAS Registry Numbers are equal
41+ # checks if two CAS Registry Numbers are equal
4042 def __eq__ (self , other ):
4143 return True if self .cas_integer == other .cas_integer else False
4244
43- ### checks if self.cas_integer < other.cas_integer
45+ # checks if self.cas_integer < other.cas_integer
4446 def __lt__ (self , other ):
4547 return True if self .cas_integer < other .cas_integer else False
4648
47- ### Returns CAS Registry Number
49+ # Returns CAS Registry Number
4850 @property
4951 def cas_string (self ):
5052 return self .__cas_string
5153
52- ### Sets CAS Registry Number
53- ### if the passed input value is a string, parse the string according to _____00-00-0
54- ### if the passed input value is an integer, create the string arrocing to _____00-00-0
54+ # Sets CAS Registry Number
55+ # if the passed input value is a string, parse the string according to _____00-00-0
56+ # if the passed input value is an integer, create the string arrocing to _____00-00-0
5557 @cas_string .setter
5658 def cas_string (self , cas_rn ):
5759 # convert (formatted) CAS string into integer
58- if regex_cas := re .match ("^(\d{2,7})\-(\d{2})-(\d{1})$" , cas_rn ):
60+ if regex_cas := re .match (r "^(\d{2,7})\-(\d{2})-(\d{1})$" , cas_rn ):
5961 self .cas_integer = self .__cas_integer = int (
6062 regex_cas .group (1 ) + regex_cas .group (2 ) + regex_cas .group (3 )
6163 )
@@ -66,7 +68,7 @@ def cas_string(self, cas_rn):
6668 )
6769 self .__cas_string = cas_rn
6870
69- ### Returns CAS Registry Number as an integer (without the hyphens)
71+ # Returns CAS Registry Number as an integer (without the hyphens)
7072 @property
7173 def cas_integer (self ):
7274 return self .__cas_integer
@@ -77,17 +79,17 @@ def cas_integer(self, cas_rn):
7779 # the officially lowest CAS number on record is 35-66-5 (as of June 2019)
7880 # (Source: https://twitter.com/CASChemistry/status/1144222698740092929)
7981 if cas_rn < 10004 or cas_rn > 9999999995 :
80- raise TypeError (
82+ raise ValueError (
8183 f"Invalid CAS number '{ cas_rn } ' (must be an integer between 10004 and 9999999995)"
8284 )
8385 self .__cas_integer = cas_rn
8486
85- ### Returns check digit of the CAS Registry Number
87+ # Returns check digit of the CAS Registry Number
8688 @property
8789 def check_digit (self ):
8890 return self .__check_digit
8991
90- ### Sets the CAS Registry Number check digit
92+ # Sets the CAS Registry Number check digit
9193 @check_digit .setter
9294 def check_digit (self , digit_to_test ):
9395 # check if the check digit fits to the CAS Number
@@ -102,6 +104,7 @@ def check_digit(self, digit_to_test):
102104 # test (check sum mod 10) against check digit
103105 if check_sum % 10 != int (digit_to_test ):
104106 raise ValueError (
105- f"Invalid CAS number '{ self .cas_string } ' (found check digit '{ digit_to_test } ', but expected '{ check_sum % 10 } ')"
107+ f"Invalid CAS number '{ self .cas_string } ' "
108+ f"(found check digit '{ digit_to_test } ', but expected '{ check_sum % 10 } ')"
106109 )
107110 self .__check_digit = int (digit_to_test )
0 commit comments