1 """ResidentRegistrationNumber test code
   2 """
   3 import unittest, re, random
   4 
   5 rrnf = r'^\d{6}-[1234]\d{6}$'
   6 
   7 def checkRe(aStr):
   8     if re.match(rrnf,aStr) and int(aStr[2:4])<=12 and int(aStr[4:6])<=31:
   9         return 1
  10 
  11 def getAuthenNum(aStr):
  12     sum = 0; i= 0
  13     multiplier = [2,3,4,5,6,7,8,9,2,3,4,5]
  14     for each in aStr[:-1]:
  15         if each == '-': 
  16             continue
  17         sum += int(each)*multiplier[i]
  18         i += 1
  19     return 11 - (sum % 11)
  20     
  21 def checkRrn(aStr):
  22     if checkRe(aStr) and getAuthenNum(aStr) == int(aStr[-1]):
  23         return 1
  24 
  25 def makeVirtualRrn(aBirthStr):
  26     rand = str(random.random()*10000)[:4]
  27     rrn = aBirthStr + '-11' + rand + '1'
  28     return rrn[:-1]+str(getAuthenNum(rrn))
  29 
  30 class ResidentTest(unittest.TestCase):
  31     def testCheckRe(self):
  32         self.failUnless(checkRe('740101-1111119'))
  33         self.failUnless(checkRe('740101-1121111'))
  34         self.failIf(checkRe('741301-1111119'))
  35         self.failIf(checkRe('741133-1111119'))
  36     def testGetAuthenNum(self):
  37         self.assertEquals(8, getAuthenNum('740101-1111118'))
  38 
  39 if __name__=='__main__':
  40     #unittest.main(argv=('','-v'))
  41     print makeVirtualRrn('740101')
web biohackers.net