Skip to content

Instantly share code, notes, and snippets.

@RPHAELLA
Last active January 17, 2023 06:37
Show Gist options
  • Save RPHAELLA/c505b2c665b0a11711f2b480aa519e82 to your computer and use it in GitHub Desktop.
Save RPHAELLA/c505b2c665b0a11711f2b480aa519e82 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#
# Singapore\'s Identification Number Generation Tool.
# Copyright (C) 2017
#
# This program can be redistributed and/or modified under the terms of the
# GNU General Public License, either version 3 of the License, or (at your
# option) any later version.
#
import getopt, sys
def get_identification_no(identification):
weight = (2,7,6,5,4,3,2) #()this is tuple and cannot be modified
alpha_singaporean = ('J','Z','I','H','G','F','E','D','C','B','A')
alpha_foreigner= ('X','W','U','T','R','Q','P','N','M','L','K')
total_sum = 0
for i in range (len(weight)):
current_product = weight[i] * int(identification[i+1])
total_sum += current_product
#if 'T' or 'G' total_sum plus 4
if ((identification[0] =='T') or (identification[0] =='G')) :
total_sum += 4
remainder = total_sum % 11
#generate last checking alphabet of the identification no.
if ((identification[0] =='S') or (identification[0] =='T')):
return identification+alpha_singaporean[remainder]
elif((identification[0] =='F') or (identification[0] =='G')):
return identification+alpha_foreigner[remainder]
def main():
try:
opts, args = getopt.getopt(sys.argv[1:],"h:ay",
["help","alpha=","year="])
if not opts:
usage()
except getopt.GetoptError:
usage()
tree = False
for o, a in opts:
if o in ("-h", "--help"):
usage()
if o in ("-a", "--alpha"):
alpha = str(a)
if o in ("-y", "--year"):
year = str(a)
generate(alpha, year)
def usage():
print ' -------------------------------------------------------------------------'
print ' Marilyn Chua ([email protected]) Deloitte Nov 16, 2017'
print ' '
print ' Singapore\'s Identification Number Generation Tool'
print ' Generate list of valid NRIC and FIN with year specified'
print ' Note that this will generate 99,999 records of Identification No.'
print ' '
print ' Typical usage:'
print ' identification-no-generator.py --alpha=S --year=99 '
print ' identification-no-generator.py -a S -y 99 | tee <file>.txt'
print ' '
print ' Options:'
print ' -a, --alpha Set the first alphabet of NRIC / FIN to be generated'
print ' Singaporean - NRIC - S or T'
print ' Foreigner - FIN - F or G'
print ' -y, --year Set the first two number (integer number) of integer'
print ' -------------------------------------------------------------------------'
sys.exit(' ')
def generate(alpha, year):
for i in xrange(99999):
identification = alpha+year+'{0:05}'.format(i)
print get_identification_no(identification)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment