Last active
January 15, 2019 02:21
-
-
Save Shylock-Hg/c48e60de4945f14e97a1f6a9b5d70644 to your computer and use it in GitHub Desktop.
Some tools of IMEI code.
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
#! /usr/bin/env python3 | |
class imei(): | |
@staticmethod | |
def calculus(imei): | |
''' | |
@brief calculus check digit of imei numbers | |
@param imei imei serial numbers | |
@retval check digit characters | |
''' | |
# check format | |
if not imei.isdigit() or 14 != len(imei): | |
raise Exception("Invalid imei format!") | |
digits = [(1+i%2)*int(d) for i,d in enumerate(imei)] | |
''' | |
digits = [] | |
for i,d in enumerate(imei): | |
if i % 2 == 1: | |
digits.append(2*int(d)) | |
else: | |
digits.append(int(d)) | |
''' | |
#print(digits) | |
digits = map(lambda d : sum(divmod(d,10)), digits) | |
result = (10 - (sum(digits) % 10))%10 | |
return result | |
@staticmethod | |
def check(calculus,imei): | |
''' | |
@brief check imei serial numbers | |
@param calculue imei check digit calculus function | |
@param imei imei serial numbers | |
@retval bool value | |
''' | |
# check imei format | |
if not imei.isdigit() or 15 != len(imei): | |
raise Exception("Invalid imei format!") | |
cd = calculus(imei[:-1]) | |
return int(imei[-1]) == cd | |
if '__main__' == __name__: | |
print(imei.calculus("35145120840121")) | |
print(imei.check(imei.calculus,"351451208401216")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment