robert-m-elder commited on
Commit
7b3dbe4
·
1 Parent(s): 55b7cf1

added is_cas(), check_cas()

Browse files
Files changed (1) hide show
  1. ChemID.py +19 -1
ChemID.py CHANGED
@@ -247,4 +247,22 @@ def name2cas(name):
247
  cas = possible_cas[0]
248
  except:
249
  cas = None
250
- return cas
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  cas = possible_cas[0]
248
  except:
249
  cas = None
250
+ return cas
251
+
252
+ def check_cas(cas):
253
+ n1,n2,n3 = cas.split('-')
254
+ # combine and flip first 2 numbers
255
+ tmp = ''.join([n1,n2])[::-1]
256
+ # sum of number*position in string
257
+ check = sum([i*int(tmp[i-1]) for i in range(1,len(tmp)+1)])
258
+ # mod 10
259
+ check = check%10
260
+ # if these match, then it's a legit cas number
261
+ return check == int(n3)
262
+
263
+ def is_cas(cas):
264
+ try:
265
+ return check_cas(cas)
266
+ except:
267
+ return False
268
+