Switch to python3

This commit is contained in:
j 2014-09-30 18:15:32 +02:00
commit 9ba4b6a91a
5286 changed files with 677347 additions and 576888 deletions

View file

@ -0,0 +1,21 @@
# __init__.py - collection of Malaysian numbers
# coding: utf-8
#
# Copyright (C) 2013 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
"""Collection of Malaysian numbers."""

View file

@ -0,0 +1,86 @@
# generated from National Registration Department of Malaysia, downloaded from
# http://www.jpn.gov.my/en/informasi/states-code
# http://www.jpn.gov.my/en/informasi/country-code
01 state="Johor" country="Malaysia" countries="Malaysia"
02 state="Kedah" country="Malaysia" countries="Malaysia"
03 state="Kelantan" country="Malaysia" countries="Malaysia"
04 state="Melaka" country="Malaysia" countries="Malaysia"
05 state="Negeri Sembilan" country="Malaysia" countries="Malaysia"
06 state="Pahang" country="Malaysia" countries="Malaysia"
07 state="Pulau Pinang" country="Malaysia" countries="Malaysia"
08 state="Perak" country="Malaysia" countries="Malaysia"
09 state="Perlis" country="Malaysia" countries="Malaysia"
10 state="Selangor" country="Malaysia" countries="Malaysia"
11 state="Terengganu" country="Malaysia" countries="Malaysia"
12 state="Sabah" country="Malaysia" countries="Malaysia"
13 state="Sarawak" country="Malaysia" countries="Malaysia"
14 state="Wilayah Persekutuan (Kuala Lumpur)" country="Malaysia" countries="Malaysia"
15 state="Wilayah Persekutuan (Labuan)" country="Malaysia" countries="Malaysia"
16 state="Wilayah Persekutuan (Putrajaya)" country="Malaysia" countries="Malaysia"
21 state="Johor" country="Malaysia" countries="Malaysia"
22 state="Johor" country="Malaysia" countries="Malaysia"
23 state="Johor" country="Malaysia" countries="Malaysia"
24 state="Johor" country="Malaysia" countries="Malaysia"
25 state="Kedah" country="Malaysia" countries="Malaysia"
26 state="Kedah" country="Malaysia" countries="Malaysia"
27 state="Kedah" country="Malaysia" countries="Malaysia"
28 state="Kelantan" country="Malaysia" countries="Malaysia"
29 state="Kelantan" country="Malaysia" countries="Malaysia"
30 state="Melaka" country="Malaysia" countries="Malaysia"
31 state="Negeri Sembilan" country="Malaysia" countries="Malaysia"
32 state="Pahang" country="Malaysia" countries="Malaysia"
33 state="Pahang" country="Malaysia" countries="Malaysia"
34 state="Pulau Pinang" country="Malaysia" countries="Malaysia"
35 state="Pulau Pinang" country="Malaysia" countries="Malaysia"
36 state="Perak" country="Malaysia" countries="Malaysia"
37 state="Perak" country="Malaysia" countries="Malaysia"
38 state="Perak" country="Malaysia" countries="Malaysia"
39 state="Perak" country="Malaysia" countries="Malaysia"
40 state="Perlis" country="Malaysia" countries="Malaysia"
41 state="Selangor" country="Malaysia" countries="Malaysia"
42 state="Selangor" country="Malaysia" countries="Malaysia"
43 state="Selangor" country="Malaysia" countries="Malaysia"
44 state="Selangor" country="Malaysia" countries="Malaysia"
45 state="Terengganu" country="Malaysia" countries="Malaysia"
46 state="Terengganu" country="Malaysia" countries="Malaysia"
47 state="Sabah" country="Malaysia" countries="Malaysia"
48 state="Sabah" country="Malaysia" countries="Malaysia"
49 state="Sabah" country="Malaysia" countries="Malaysia"
50 state="Sarawak" country="Malaysia" countries="Malaysia"
51 state="Sarawak" country="Malaysia" countries="Malaysia"
52 state="Sarawak" country="Malaysia" countries="Malaysia"
53 state="Sarawak" country="Malaysia" countries="Malaysia"
54 state="Wilayah Persekutuan (Kuala Lumpur)" country="Malaysia" countries="Malaysia"
55 state="Wilayah Persekutuan (Kuala Lumpur)" country="Malaysia" countries="Malaysia"
56 state="Wilayah Persekutuan (Kuala Lumpur)" country="Malaysia" countries="Malaysia"
57 state="Wilayah Persekutuan (Kuala Lumpur)" country="Malaysia" countries="Malaysia"
58 state="Wilayah Persekutuan (Labuan)" country="Malaysia" countries="Malaysia"
59 state="Negeri Sembilan" country="Malaysia" countries="Malaysia"
60 country="Brunei" countries="Brunei"
61 country="Indonesia" countries="Indonesia"
62 countries="Cambodia, Kampuchea"
63 country="Laos" countries="Laos"
64 country="Mynmar" countries="Mynmar"
65 country="Filipina" countries="Filipina"
66 country="Singapura" countries="Singapura"
67 country="Thailand" countries="Thailand"
68 country="Vietnam" countries="Vietnam"
74 country="China" countries="China"
75 country="India" countries="India"
76 country="Pakistan" countries="Pakistan"
77 country="Arab Saudi" countries="Arab Saudi"
78 country="Sri Lanka" countries="Sri Lanka"
79 country="Bangladesh" countries="Bangladesh"
82 state="Negeri Tidak Diketahui" country="Malaysia" countries="Malaysia"
83 countries="Australia, American Samoa, Macedonia, New Zealand, New Caledonia, Papua New Gurney, Fiji, Timor Leste"
84 countries="Argentina, Anguilla, Aruba, Bolivia, Brazil, Paraguay, Peru, Chile, Colombia, Equador, Uruguay, Venezuela"
85 countries="Algeria, Angola, Kenya, Afrika Tengah, Liberia, Afrika Selatan, Mali, Mauritania, Morocco, Malawi, Botswana, Mozambique, Burundi, Nigeria, Namibia, Cameroon, Chad, Rwanda, Senegal, Sierra Leone, Somalia, Djibouti, Sudan, Egypt, Ethopia, Swaziland, Eritrea, Gambia, Ghana, Tunisia, Tanzania, Tonga, Togo, Uganda, Zaire, Zambia, Zimbabwe"
86 countries="Austria, Luxembourg, Armenia, Malta, Monaco, Belgium, Nitherlands, Norway, Cyprus, Portugal, Denmark, Sweeden, Spain, Switzerland, France, Finland, Slovakia, Slovenia, Greece, Germany, Holy See (Vatican City), Italy"
87 countries="Britain, Ireland"
88 countries="Jordan, Kuwait, Lebanon, Bahrain, Oman, Qatar, Syria, Turkey, United Arab Emirate, Iran, Iraq, Israel, Yemen"
89 countries="Japan, Korea Selatan, Korea Utara, Taiwan"
90 countries="Jamaica, Bahamas, Barbados, Belize, Mexico, Nicaragua, Panama, Puerto Rico, Costa Rica, Cuba, Dominica, El Salvador, Grenada, Guatemala, Trinidad&Tobado, Haiti, Honduras"
91 countries="Canada, Greenland, United State"
92 countries="Albania, Albania, Latvia, Lithuania, Bulgaria, Byelorussia, Bosnia, Belarus, Poland, Romania, Russia, Czechoslovakia, Crotia, Esthonia, Serbia, Georgia, Hungary, Ukraine"
93 countries="Afghanistan, Antigua & Barbuda, Kazakhstan, Andorra/Andora, Libya, Arzebaijan, Antartica, Maldives, Madagascar, Mauritius, Mongolia, Benin, Maghribi, Bhutan, Macau, Nepal, Bermuda, Burkina faso/Burkina, Bora-bora, Bouvet Island, Palestine, Cape Verde, Comoros, Seychelles, Soloman Islands, Samoa, San Marino, Guinea, Gibraltar, Tajikistan, Tukmenistan, Hong Kong, Uzbekistan, Ivory Coast, Vanuatu, Iceland, Yugoslavia"

View file

@ -0,0 +1,112 @@
# nric.py - functions for handling NRIC numbers
#
# Copyright (C) 2013 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
"""NRIC No. (Malaysian National Registration Identity Card Number).
The NRIC No. is the unique identifier for issued to Malaysian citizens and
permanent residents.
The number consist of 12 digits in three sections. The first 6 digits
represent the birth date, followed by two digits represeting the birth
place and finally four digits. The gender of a person can be derived from
the last digit: odd numbers for males and even numbers for females.
>>> validate('770305-02-1234')
'770305021234'
>>> validate('771305-02-1234') # invalid date
Traceback (most recent call last):
...
InvalidComponent: ...
>>> validate('770305-99-1234') # unknown birth place code
Traceback (most recent call last):
...
InvalidComponent: ...
>>> format('770305021234')
'770305-02-1234'
"""
import datetime
from stdnum.exceptions import *
from stdnum.util import clean
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, ' -*').strip()
def get_birth_date(number):
"""Split the date parts from the number and return the birth date.
Note that in some cases it may return the registration date instead of
the birth date and it may be a century off."""
number = compact(number)
year = int(number[0:2])
month = int(number[2:4])
day = int(number[4:6])
# this is a bit broken but it's easy
try:
return datetime.date(year + 1900, month, day)
except ValueError:
pass
try:
return datetime.date(year + 2000, month, day)
except ValueError:
raise InvalidComponent()
def get_birth_place(number):
"""Use the number to look up the place of birth of the person. This can
either be a state or federal territory within Malaysia or a country
outside of Malaysia."""
from stdnum import numdb
number = compact(number)
results = numdb.get('my/bp').info(number[6:8])[0][1]
if not results:
raise InvalidComponent()
return results
def validate(number):
"""Checks to see if the number provided is a valid NRIC numbers. This
checks the length, formatting and birth date and place."""
number = compact(number)
if len(number) != 12:
raise InvalidLength()
if not number.isdigit():
raise InvalidFormat()
get_birth_date(number)
get_birth_place(number)
return number
def is_valid(number):
"""Checks to see if the number provided is a valid NRIC numbers. This
checks the length, formatting and birth date and place."""
try:
return bool(validate(number))
except ValidationError:
return False
def format(number):
"""Reformat the passed number to the standard format."""
number = compact(number)
return number[:6] + '-' + number[6:8] + '-' + number[8:]