python-stdnum commit: r26 - in python-stdnum: . stdnum tests
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum commit: r26 - in python-stdnum: . stdnum tests
- From: Commits of the python-stdnum project <python-stdnum-commits [at] lists.arthurdejong.org>
- To: python-stdnum-commits [at] lists.arthurdejong.org
- Reply-to: python-stdnum-users [at] lists.arthurdejong.org
- Subject: python-stdnum commit: r26 - in python-stdnum: . stdnum tests
- Date: Fri, 20 Aug 2010 16:16:57 +0200 (CEST)
Author: arthur
Date: Fri Aug 20 16:16:55 2010
New Revision: 26
URL: http://arthurdejong.org/viewvc/python-stdnum?view=rev&revision=26
Log:
add an IMEI (International Mobile Equipment Identity) module
Added:
python-stdnum/stdnum/imei.py
python-stdnum/tests/test_imei.doctest
Modified:
python-stdnum/README
Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README Fri Aug 20 14:16:23 2010 (r25)
+++ python-stdnum/README Fri Aug 20 16:16:55 2010 (r26)
@@ -7,11 +7,12 @@
Available formats
-----------------
-Currently this module supports the following formats:
+Currently this package supports the following formats:
- ISBN (International Standard Book Number)
- ISSN (International Standard Serial Number)
- BSN (Burgerservicenummer, the Dutch national identification number)
+- IMEI (International Mobile Equipment Identity)
- Verhoeff (generic functions for the Verhoeff algorithm)
- Luhn (generic functions for the Luhn and Luhn mod N algorithms)
Added: python-stdnum/stdnum/imei.py
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ python-stdnum/stdnum/imei.py Fri Aug 20 16:16:55 2010 (r26)
@@ -0,0 +1,69 @@
+# imei.py - functions for handling International Mobile Equipment Identity
+# (IMEI) numbers
+#
+# 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 IMEI (International Mobile Equipment Identity)
+numbers, used to identify mobile phones.
+
+>>> is_valid('35686800-004141-20')
+True
+>>> is_valid('35-417803-685978-1') # incorrect check digit
+False
+>>> compact('35686800-004141-20')
+'3568680000414120'
+>>> format('354178036859789')
+'35-417803-685978-9'
+>>> imei_type('35686800-004141-20')
+'IMEISV'
+"""
+
+
+def compact(number):
+ """Convert the IMEI number to the minimal representation. This strips the
+ number of any valid separators and removes surrounding whitespace."""
+ number = number.replace(' ','').replace('-','').strip().upper()
+ return number
+
+def imei_type(number):
+ """Check the passed number and returns 'IMEI', 'IMEISV' or None (for
+ invalid) for checking the type of number passed."""
+ try:
+ number = compact(number)
+ except:
+ return None
+ if len(number) == 14: # IMEI without check digit
+ return 'IMEI' if number.isdigit() else None
+ if len(number) == 15: # IMEI with check digit
+ from stdnum import luhn
+ return 'IMEI' if luhn.is_valid(number) else None
+ elif len(number) == 16:
+ return 'IMEISV' if number.isdigit() else None
+ else:
+ return None
+
+def is_valid(number):
+ """Checks to see if the number provided is a valid IMEI (or IMEISV)
+ number."""
+ return imei_type(number) is not None
+
+def format(number, separator='-'):
+ """Reformat the passed number to the standard format."""
+ number = compact(number)
+ number = ( number[:2], number[2:8], number[8:14], number[14:] )
+ return separator.join(x for x in number if x)
Added: python-stdnum/tests/test_imei.doctest
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ python-stdnum/tests/test_imei.doctest Fri Aug 20 16:16:55 2010
(r26)
@@ -0,0 +1,65 @@
+test_imei.doctest - more detailed doctests for stdnum.imei 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.imei module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import imei
+
+
+Should be valid numbers:
+
+>>> imei.is_valid('49-015420-323751')
+True
+>>> imei.is_valid('35-209900-176148-1')
+True
+>>> imei.is_valid('35-209900-176148-23')
+True
+>>> imei.is_valid('350077-52-323751-3')
+True
+>>> imei.is_valid('354178036859789')
+True
+
+
+These are normal variations that should just work. Getting the type:
+
+>>> imei.imei_type('35686800-004141-20')
+'IMEISV'
+>>> imei.imei_type('35-417803-685978-9')
+'IMEI'
+>>> imei.imei_type('35-417803-685978-2') is None # invalid check digit
+True
+>>> imei.imei_type('3568680000414120')
+'IMEISV'
+
+
+The is_valid() method should be fairly robust against invalid junk passed:
+
+>>> imei.is_valid(None)
+False
+>>> imei.is_valid('')
+False
+>>> imei.is_valid(0)
+False
+>>> imei.is_valid(object())
+False
+>>> imei.is_valid('3abc6800-0041X1-20')
+False
--
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: r26 - in python-stdnum: . stdnum tests,
Commits of the python-stdnum project