#format python """ResidentRegistrationNumber test code """ import unittest, re, random rrnf = r'^\d{6}-[1234]\d{6}$' def checkRe(aStr): if re.match(rrnf,aStr) and int(aStr[2:4])<=12 and int(aStr[4:6])<=31: return 1 def getAuthenNum(aStr): sum = 0; i= 0 multiplier = [2,3,4,5,6,7,8,9,2,3,4,5] for each in aStr[:-1]: if each == '-': continue sum += int(each)*multiplier[i] i += 1 return 11 - (sum % 11) def checkRrn(aStr): if checkRe(aStr) and getAuthenNum(aStr) == int(aStr[-1]): return 1 def makeVirtualRrn(aBirthStr): rand = str(random.random()*10000)[:4] rrn = aBirthStr + '-11' + rand + '1' return rrn[:-1]+str(getAuthenNum(rrn)) class ResidentTest(unittest.TestCase): def testCheckRe(self): self.failUnless(checkRe('740101-1111119')) self.failUnless(checkRe('740101-1121111')) self.failIf(checkRe('741301-1111119')) self.failIf(checkRe('741133-1111119')) def testGetAuthenNum(self): self.assertEquals(8, getAuthenNum('740101-1111118')) if __name__=='__main__': #unittest.main(argv=('','-v')) print makeVirtualRrn('740101')