Created
April 1, 2016 07:52
-
-
Save hasyimibhar/5f06f5fcdd9ddb8324112b2735cd267b to your computer and use it in GitHub Desktop.
Simple MyKad Parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
from datetime import date | |
import re | |
class InvalidMykadNumber(Exception): | |
def __init__(self, value): | |
self.value = value | |
def __str__(self): | |
return repr(self.value) | |
mykad_info = namedtuple('mykad_info', ['birth_date', 'birth_place', 'gender']) | |
birth_places = { | |
"01": "Johor", | |
"02": "Kedah", | |
"03": "Kelantan", | |
"04": "Malacca", | |
"05": "Negeri Sembilan", | |
"06": "Pahang", | |
"07": "Penang", | |
"08": "Perak", | |
"09": "Perlis", | |
"10": "Selangor", | |
"11": "Terengganu", | |
"12": "Sabah", | |
"13": "Sarawak", | |
"14": "Federal Territory of Kuala Lumpur", | |
"15": "Federal Territory of Labuan", | |
"16": "Federal Territory of Putrajaya", | |
"21": "Johor", | |
"22": "Johor", | |
"23": "Johor", | |
"24": "Johor", | |
"25": "Kedah", | |
"26": "Kedah", | |
"27": "Kedah", | |
"28": "Kelantan", | |
"29": "Kelantan", | |
"30": "Malacca", | |
"31": "Negeri Sembilan", | |
"32": "Pahang", | |
"33": "Pahang", | |
"34": "Penang", | |
"35": "Penang", | |
"36": "Perak", | |
"37": "Perak", | |
"38": "Perak", | |
"39": "Perak", | |
"40": "Perlis", | |
"41": "Selangor", | |
"42": "Selangor", | |
"43": "Selangor", | |
"44": "Selangor", | |
"45": "Terengganu", | |
"46": "Terengganu", | |
"47": "Sabah", | |
"48": "Sabah", | |
"49": "Sabah", | |
"50": "Sarawak", | |
"51": "Sarawak", | |
"52": "Sarawak", | |
"53": "Sarawak", | |
"54": "Federal Territory of Kuala Lumpur", | |
"55": "Federal Territory of Kuala Lumpur", | |
"56": "Federal Territory of Kuala Lumpur", | |
"57": "Federal Territory of Kuala Lumpur", | |
"58": "Federal Territory of Labuan", | |
"59": "Negeri Sembilan", | |
} | |
def parse_mykad_number(n): | |
if not re.compile('^\d{12}$').match(n): | |
raise InvalidMykadNumber("Invalid format") | |
bd = n[:6] | |
try: | |
birth_date = date(1900 + int(bd[:2]), int(bd[2:4]), int(bd[4:6])) | |
except ValueError: | |
raise InvalidMykadNumber("Invalid birth date") | |
bp = n[6:8] | |
if bp in birth_places: | |
birth_place = birth_places[bp] | |
elif int(bp) >= 60: | |
birth_place = "Outside Malaysia" | |
else: | |
raise InvalidMykadNumber("Invalid birth place number") | |
gender = "Male" if int(n[11]) % 2 else "Female" | |
return mykad_info(birth_date, birth_place, gender) | |
info = parse_mykad_number("913031014783") | |
print info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mykad_info(birth_date=datetime.date(1991, 8, 31), birth_place='Johor', gender='Male') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment