22
33"""
44Class for CAS Registry Numbers® (CAS RN®)
5- allwos to manage, check and sort CAS Registry Numbers®
5+ allows to manage, check and sort CAS Registry Numbers®
66see https://www.cas.org/support/documentation/chemical-substances/checkdig
77for a complete specification of the CAS Registry Numbers®
88and the calculation method to determine the check digit
99"""
1010
1111
1212class CAS :
13- def __init__ (self , cas_rn ):
13+ """
14+ Class for CAS Registry Numbers® (CAS RN®) -
15+ allows to manage, check and sort CAS Registry Numbers®
16+
17+ Example usage:
18+ ```python
19+ from casregnum import CAS
20+ caffeine = CAS("58-08-2")
21+ print(caffeine)
22+ ```
23+ """
24+ def __init__ (self , cas_rn : int | str ) -> None :
1425 # case that input cas_rn is an integer
1526 if isinstance (cas_rn , int ):
1627 self .cas_integer = cas_rn
@@ -24,38 +35,48 @@ def __init__(self, cas_rn):
2435 # case that cas_rn is neither an integer nor a string
2536 else :
2637 raise TypeError (
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 )} )"
38+ f"Invalid CAS Registry Number format '{ cas_rn } ' (expected an integer (<class 'int'>) or a string (<class 'str'>), but found { type (cas_rn )} )"
2939 )
3040 # extract check digit = last digit of the CAS number
3141 self .check_digit = int (str (cas_rn )[- 1 ])
3242
3343 # default string output for CAS Registry Numbers
34- def __str__ (self ):
44+ def __str__ (self ) -> str :
3545 return str (self .cas_string )
3646
47+ # defines a representation for CAS Registry Numbers
48+ def __repr__ (self ) -> str :
49+ return f"CAS(cas_rn='{ self .cas_string } ')"
50+
3751 # defines a string format for CAS Registry Numbers
38- def __format__ (self , format_spec ):
52+ def __format__ (self , format_spec ) -> str :
3953 return f"{ self .cas_string :{format_spec }} "
4054
4155 # checks if two CAS Registry Numbers are equal
42- def __eq__ (self , other ):
43- return True if self .cas_integer == other .cas_integer else False
56+ def __eq__ (self , other : object ) -> bool :
57+ if not isinstance (other , CAS ):
58+ return False
59+ return self .cas_integer == other .cas_integer
4460
4561 # checks if self.cas_integer < other.cas_integer
46- def __lt__ (self , other ):
47- return True if self .cas_integer < other .cas_integer else False
62+ def __lt__ (self , other : object ) -> bool :
63+ if not isinstance (other , CAS ):
64+ return NotImplemented
65+ return self .cas_integer < other .cas_integer
4866
4967 # Returns CAS Registry Number
5068 @property
51- def cas_string (self ):
69+ def cas_string (self ) -> str :
70+ """
71+ Returns the CAS Registry Number as a formatted string (e.g. "58-08-2").
72+ """
5273 return self .__cas_string
5374
5475 # Sets CAS Registry Number
5576 # if the passed input value is a string, parse the string according to _____00-00-0
5677 # if the passed input value is an integer, create the string arrocing to _____00-00-0
5778 @cas_string .setter
58- def cas_string (self , cas_rn ) :
79+ def cas_string (self , cas_rn : str ) -> None :
5980 # convert (formatted) CAS string into integer
6081 if regex_cas := re .match (r"^(\d{2,7})\-(\d{2})-(\d{1})$" , cas_rn ):
6182 self .cas_integer = self .__cas_integer = int (
@@ -70,11 +91,14 @@ def cas_string(self, cas_rn):
7091
7192 # Returns CAS Registry Number as an integer (without the hyphens)
7293 @property
73- def cas_integer (self ):
94+ def cas_integer (self ) -> int :
95+ """
96+ Returns the CAS Registry Number as an integer (e.g. 58082).
97+ """
7498 return self .__cas_integer
7599
76100 @cas_integer .setter
77- def cas_integer (self , cas_rn ) :
101+ def cas_integer (self , cas_rn : int ) -> None :
78102 # by definition, the lowest theoretical CAS number is 10-00-4,
79103 # the officially lowest CAS number on record is 35-66-5 (as of June 2019)
80104 # (Source: https://twitter.com/CASChemistry/status/1144222698740092929)
@@ -86,12 +110,15 @@ def cas_integer(self, cas_rn):
86110
87111 # Returns check digit of the CAS Registry Number
88112 @property
89- def check_digit (self ):
113+ def check_digit (self ) -> int :
114+ """
115+ Returns the check digit of the CAS Registry Number (e.g. 2 for "58-08-2").
116+ """
90117 return self .__check_digit
91118
92119 # Sets the CAS Registry Number check digit
93120 @check_digit .setter
94- def check_digit (self , digit_to_test ) :
121+ def check_digit (self , digit_to_test : int ) -> None :
95122 # check if the check digit fits to the CAS Number
96123 # Source: https://www.cas.org/support/documentation/chemical-substances/checkdig
97124 # get the CAS number without the check digit = integer value of (cas_integer/10)
0 commit comments