10000 add an IMEI (International Mobile Equipment Identity) module · odony/python-stdnum@6bb04af · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bb04af

Browse files
committed
add an IMEI (International Mobile Equipment Identity) module
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@26 9dea7c4f-944c-4273-ac1a-574ede026edc
1 parent ca08995 commit 6bb04af

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

README

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ in different formats.
77
Available formats
88
-----------------
99

10-
Currently this module supports the following formats:
10+
Currently this package supports the following formats:
1111

1212
- ISBN (International Standard Book Number)
1313
- ISSN (International Standard Serial Number)
1414
- BSN (Burgerservicenummer, the Dutch national identification number)
15+
- IMEI (International Mobile Equipment Identity)
1516
- Verhoeff (generic functions for the Verhoeff algorithm)
1617
- Luhn (generic functions for the Luhn and Luhn mod N algorithms)
1718

stdnum/imei.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# imei.py - functions for handling International Mobile Equipment Identity
2+
# (IMEI) numbers
3+
#
4+
# Copyright (C) 2010 Arthur de Jong
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
# 02110-1301 USA
20+
21+
"""Module for handling IMEI (International Mobile Equipment Identity)
22+
numbers, used to identify mobile phones.
23+
24+
>>> is_valid('35686800-004141-20')
25+
True
26+
>>> is_valid('35-417803-685978-1') # incorrect check digit
27+
False
28+
>>> compact('35686800-004141-20')
29+
'3568680000414120'
30+
>>> format('354178036859789')
31+
'35-417803-685978-9'
32+
>>> imei_type('35686800-004141-20')
33+
'IMEISV'
34+
"""
35+
36+
37+
def compact(number):
38+
"""Convert the IMEI number to the minimal representation. This strips the
39+
number of any valid separators and removes surrounding whitespace."""
40+
number = number.replace(' ','').replace('-','').strip().upper()
41+
return number
42+
43+
def imei_type(number):
44+
"""Check the passed number and returns 'IMEI', 'IMEISV' or None (for
45+
invalid) for checking the type of number passed."""
46+
try:
47+
number = compact(number)
48+
except:
49+
return None
50+
if len(number) == 14: # IMEI without check digit
51+
return 'IMEI' if number.isdigit() else None
52+
if len(number) == 15: # IMEI with check digit
53+
from stdnum import luhn
54+
return 'IMEI' if luhn.is_valid(number) else None
55+
elif len(number) == 16:
56+
return 'IMEISV' if number.isdigit() else None
57+
else:
58+
return None
59+
60+
def is_valid(number):
61+
"""Checks to see if the number provided is a valid IMEI (or IMEISV)
62+
number."""
63+
return imei_type(number) is not None
64+
65+
def format(number, separator='-'):
66+
"""Reformat the passed number to the standard format."""
67+
number = compact(number)
68+
number = ( number[:2], number[2:8], number[8:14], number[14:] )
69+
return separator.join(x for x in number if x)

tests/test_imei.doctest

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
test_imei.doctest - more detailed doctests for stdnum.imei module
2+
3+
Copyright (C) 2010 Arthur de Jong
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
02110-1301 USA
19+
20+
21+
This file contains more detailed doctests for the stdnum.imei module. It
22+
tries to test more corner cases and detailed functional 67E6 ity that is not
23+
really useful as module documentation.
24+
25+
>>> from stdnum import imei
26+
27+
28+
Should be valid numbers:
29+
30+
>>> imei.is_valid('49-015420-323751')
31+
True
32+
>>> imei.is_valid('35-209900-176148-1')
33+
True
34+
>>> imei.is_valid('35-209900-176148-23')
35+
True
36+
>>> imei.is_valid('350077-52-323751-3')
37+
True
38+
>>> imei.is_valid('354178036859789')
39+
True
40+
41+
42+
These are normal variations that should just work. Getting the type:
43+
44+
>>> imei.imei_type('35686800-004141-20')
45+
'IMEISV'
46+
>>> imei.imei_type('35-417803-685978-9')
47+
'IMEI'
48+
>>> imei.imei_type('35-417803-685978-2') is None # invalid check digit
49+
True
50+
>>> imei.imei_type('3568680000414120')
51+
'IMEISV'
52+
53+
54+
The is_valid() method should be fairly robust against invalid junk passed:
55+
56+
>>> imei.is_valid(None)
57+
False
58+
>>> imei.is_valid('')
59+
False
60+
>>> imei.is_valid(0)
61+
False
62+
>>> imei.is_valid(object())
63+
False
64+
>>> imei.is_valid('3abc6800-0041X1-20')
65+
False

0 commit comments

Comments
 (0)
0