python-stdnum commit: r30 - in python-stdnum: . stdnum tests
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum commit: r30 - in python-stdnum: . stdnum tests
Author: arthur
Date: Fri Aug 20 21:47:13 2010
New Revision: 30
URL: http://arthurdejong.org/viewvc/python-stdnum?view=rev&revision=30
Log:
add a MEID (Mobile Equipment Identifier) module
Added:
python-stdnum/stdnum/meid.py
python-stdnum/tests/test_meid.doctest
Modified:
python-stdnum/README
Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README Fri Aug 20 21:41:39 2010 (r29)
+++ python-stdnum/README Fri Aug 20 21:47:13 2010 (r30)
@@ -13,6 +13,7 @@
- ISSN (International Standard Serial Number)
- BSN (Burgerservicenummer, the Dutch national identification number)
- IMEI (International Mobile Equipment Identity)
+- MEID (Mobile Equipment Identifier)
- Verhoeff (generic functions for the Verhoeff algorithm)
- Luhn (generic functions for the Luhn and Luhn mod N algorithms)
Added: python-stdnum/stdnum/meid.py
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ python-stdnum/stdnum/meid.py Fri Aug 20 21:47:13 2010 (r30)
@@ -0,0 +1,147 @@
+# meid.py - functions for handling Mobile Equipment Identifiers (MEIDs)
+#
+# Copyright (C) 2010 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
+
+"""Module for handling MEID (Mobile Equipment Identifier) numbers, used
+to identify physical piece of CDMA mobile station equipment.
+
+>>> is_valid('AF 01 23 45 0A BC DE')
+True
+>>> is_valid('AF 01 23 45 0A BC DE C')
+True
+>>> is_valid('29360 87365 0070 3710 0')
+True
+>>> compact('AF 01 23 45 0A BC DE C')
+'AF0123450ABCDE'
+>>> format('af0123450abcDEC', add_check_digit=True)
+'AF 01 23 45 0A BC DE C'
+"""
+
+_hex_alphabet='0123456789ABCDEF'
+
+def _cleanup(number):
+ """Remove any grouping information from the number and removes surrounding
+ whitespace."""
+ return str(number).replace(' ','').replace('-','').strip().upper()
+
+def _ishex(number):
+ for x in number:
+ if x not in _hex_alphabet:
+ return False
+ return True
+
+def _parse(number):
+ number = _cleanup(number)
+ if len(number) == 14 and _ishex(number):
+ # 14-digit hex representation
+ return ( number, '' )
+ elif len(number) == 15 and _ishex(number):
+ # 14-digit hex representation with check digit
+ return ( number[0:14], number[14] )
+ elif len(number) == 18 and number.isdigit():
+ # 18-digit decimal representation
+ return ( number, '' )
+ elif len(number) == 19 and number.isdigit():
+ # 18-digit decimal representation witch check digit
+ return ( number[0:18], number[18] )
+ else:
+ return None
+
+def _calc_check_digit(number):
+ # both the 18-digit decimal format and the 14-digit hex format
+ # containing only decimal digits should use the decimal Luhn check
+ from stdnum import luhn
+ if number.isdigit():
+ return luhn.calc_check_digit(number)
+ else:
+ return luhn.calc_check_digit(number, alphabet=_hex_alphabet)
+
+def compact(number, strip_check_digit=True):
+ """Convert the MEID number to the minimal (hexadicimal) representation.
+ This strips grouping information, removes surrounding whitespace and
+ converts to hexadecimal if needed. If the check digit is to be preserved
+ and conversion is done a new check digit is recalculated."""
+ # first parse the number
+ number, cd = _parse(number)
+ # strip check digit if needed
+ if strip_check_digit:
+ cd = ''
+ # convert to hex if needed
+ if len(number) == 18:
+ number = '%08X%06X' % ( int(number[0:10]), int(number[10:18]) )
+ if cd:
+ cd = _calc_check_digit(number)
+ # put parts back together again
+ return number + cd
+
+def is_valid(number):
+ """Checks to see if the number provided is a valid MEID number."""
+ from stdnum import luhn
+ # first parse the number
+ try:
+ number, cd = _parse(number)
+ except:
+ return False
+ # decimal format can be easily determined
+ if len(number) == 18:
+ return not cd or luhn.is_valid(number + cd)
+ # if the remaining hex format is fully decimal it is an IMEI number
+ if number.isdigit():
+ from stdnum import imei
+ return imei.is_valid(number + cd)
+ # normal hex Luhn validation
+ return not cd or luhn.is_valid(number + cd, alphabet=_hex_alphabet)
+
+def format(number, separator=' ', format=None, add_check_digit=False):
+ """Reformat the passed number to the standard format. The separater
+ used can be provided. If the format is specified (either 'hex' or
+ 'dec') the number is reformatted in that format, otherwise the current
+ representation is kept. If add_check_digit is True a check digit will
+ be added if it is not present yet."""
+ # first parse the number
+ number, cd = _parse(number)
+ # format conversions if needed
+ if format == 'dec' and len(number) == 14:
+ # convert to decimal
+ number = '%010d%08d' % ( int(number[0:8], 16), int(number[8:14], 16) )
+ if cd:
+ cd = _calc_check_digit(number)
+ elif format == 'hex' and len(number) == 18:
+ # convert to hex
+ number = '%08X%06X' % ( int(number[0:10]), int(number[10:18]) )
+ if cd:
+ cd = _calc_check_digit(number)
+ # see if we need to add a check digit
+ if add_check_digit and not cd:
+ cd = _calc_check_digit(number)
+ # split number according to format
+ if len(number) == 14:
+ number = [ number[i*2:i*2+2] for i in range(7) ] + [ cd ]
+ else:
+ number = ( number[:5], number[5:10], number[10:14], number[14:], cd )
+ return separator.join(x for x in number if x)
+
+def to_pseudo_esn(number):
+ """Convert the provided MEID to a pseudo ESN (pESN). The ESN is returned
+ in compact HEX representation."""
+ import hashlib
+ # get the SHA1 of the binary representation of the number
+ s = hashlib.sha1(compact(number, strip_check_digit=True).decode('hex'))
+ # return the last 6 digits of the hash prefixed with the reserved
+ # manufacturer code
+ return "80" + s.hexdigest()[-6:].upper()
Added: python-stdnum/tests/test_meid.doctest
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ python-stdnum/tests/test_meid.doctest Fri Aug 20 21:47:13 2010
(r30)
@@ -0,0 +1,163 @@
+test_meid.doctest - more detailed doctests for stdnum.meid module
+
+Copyright (C) 2010 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
+
+
+This file contains more detailed doctests for the stdnum.meid module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import meid
+
+
+IMEI numbers without the software version (but optionally with a check
+digit) should be valid numbers:
+
+>>> meid.is_valid('49-015420-323751')
+True
+>>> meid.is_valid('35-209900-176148-1')
+True
+>>> meid.is_valid('35-209900-176148-2')
+False
+
+
+MEIDs can be represented as HEX strings (with and without check digit):
+
+>>> meid.is_valid('AF 01 23 45 0A BC DE')
+True
+>>> meid.is_valid('AF 01 23 45 0A BC DE C')
+True
+>>> meid.is_valid('AF 01 23 45 0A BC DE D')
+False
+
+
+Also, MEIDs can be represented in decimal format (with and without check
digit):
+
+>>> meid.is_valid('29360 87365 0070 3710')
+True
+>>> meid.is_valid('29360 87365 0070 3710 0')
+True
+>>> meid.is_valid('29360 87365 0070 3710 1')
+False
+
+
+The is_valid() method should be fairly robust against invalid junk passed:
+
+>>> meid.is_valid(None)
+False
+>>> meid.is_valid('')
+False
+>>> meid.is_valid(0)
+False
+>>> meid.is_valid(object())
+False
+>>> meid.is_valid('29360 ABCDE 0070 3710')
+False
+>>> meid.is_valid('GF 01 23 45 0A BC DE')
+False
+
+The compact method should convert to HEX if needed and can optionally leave
+the check digit intact.
+
+>>> meid.compact('49-015420-323751')
+'49015420323751'
+>>> meid.compact('35-209900-176148-2')
+'35209900176148'
+>>> meid.compact('35-209900-176148-2', strip_check_digit=False)
+'352099001761482'
+>>> meid.compact('af 01 23 45 0a bc de')
+'AF0123450ABCDE'
+>>> meid.compact('AF 01 23 45 0A BC DE C')
+'AF0123450ABCDE'
+>>> meid.compact('AF 01 23 45 0A BC DE C', strip_check_digit=False)
+'AF0123450ABCDEC'
+>>> meid.compact('29360 87365 0070 3710')
+'AF0123450ABCDE'
+>>> meid.compact('29360 87365 0070 3710 0')
+'AF0123450ABCDE'
+>>> meid.compact('29360 87365 0070 3710 0', strip_check_digit=False)
+'AF0123450ABCDEC'
+
+
+The format() function can add the check digit if needed. It should leave
+alone existing check digits (even invalid ones).
+
+>>> meid.format('35-209900-176148-2')
+'35 20 99 00 17 61 48 2'
+>>> meid.format('35-209900-176148')
+'35 20 99 00 17 61 48'
+>>> meid.format('35-209900-176148', add_check_digit=True)
+'35 20 99 00 17 61 48 1'
+>>> meid.format('af0123450abcDE')
+'AF 01 23 45 0A BC DE'
+>>> meid.format('af0123450abcDEC', add_check_digit=True)
+'AF 01 23 45 0A BC DE C'
+
+
+The format() function can also convert to decimal, recalculating the check
+digit if needed (conversion will silently correct incorrect check digits):
+
+>>> meid.format('35-209900-176148', format='dec')
+'08913 28768 0153 2232'
+>>> meid.format('35-209900-176148', format='dec', add_check_digit=True)
+'08913 28768 0153 2232 3'
+>>> meid.format('35-209900-176148-9', format='dec')
+'08913 28768 0153 2232 3'
+>>> meid.format('af0123450abcDE', format='dec')
+'29360 87365 0070 3710'
+>>> meid.format('af0123450abcDE', format='dec', add_check_digit=True)
+'29360 87365 0070 3710 0'
+>>> meid.format('af0123450abcDEC', format='dec')
+'29360 87365 0070 3710 0'
+>>> meid.format('293608736500703710', format='dec')
+'29360 87365 0070 3710'
+>>> meid.format('293608736500703710', format='dec', add_check_digit=True)
+'29360 87365 0070 3710 0'
+>>> meid.format('2936087365007037106', format='dec')
+'29360 87365 0070 3710 6'
+
+
+The format() function can also convert to hex, recalculating the check
+digit if needed (conversion will silently correct incorrect check digits):
+
+>>> meid.format('35-209900-176148', format='hex')
+'35 20 99 00 17 61 48'
+>>> meid.format('35-209900-176148', format='hex', add_check_digit=True)
+'35 20 99 00 17 61 48 1'
+>>> meid.format('35-209900-176148-9', format='hex')
+'35 20 99 00 17 61 48 9'
+>>> meid.format('af0123450abcDE', format='hex')
+'AF 01 23 45 0A BC DE'
+>>> meid.format('af0123450abcDE', format='hex', add_check_digit=True)
+'AF 01 23 45 0A BC DE C'
+>>> meid.format('af0123450abcDEF', format='hex')
+'AF 01 23 45 0A BC DE F'
+>>> meid.format('293608736500703710', format='hex')
+'AF 01 23 45 0A BC DE'
+>>> meid.format('293608736500703710', format='hex', add_check_digit=True)
+'AF 01 23 45 0A BC DE C'
+>>> meid.format('2936087365007037106', format='hex')
+'AF 01 23 45 0A BC DE C'
+
+The conversion function should work regardless of the check digit and
+whether decimal or hex representation is used.
+
+>>> meid.to_pseudo_esn('AF 01 23 45 0A BC DE C')
+'8016B128'
+>>> meid.to_pseudo_esn('29360 87365 0070 3710')
+'8016B128'
--
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits
- python-stdnum commit: r30 - in python-stdnum: . stdnum tests,
Commits of the python-stdnum project