Note that numbers at odd (from the end, starting from 0) postions are doubled, so you have to add it to your code, for example following code will return luhn checksum:
def luhn_residue(digits):
return sum(sum(divmod(int(d)*(1 + i%2), 10))
for i, d in enumerate(digits[::-1])) % 10
Here (1 + i%2)
multiplier equals 2
for odd positioned numbers, and 1
for even positioned. Then sum(divmod(..., 10))
returns sum of digits for a (possibly) two digit number, and outer sum sums resulting sequence.
You can use it to generate valid number sequences:
def getImei(N):
part = ''.join(str(random.randrange(0,9)) for _ in range(N-1))
res = luhn_residue('{}{}'.format(part, 0))
return '{}{}'.format(part, -res%10)
Demo:
>>> luhn_residue('79927398713')
0
>>> luhn_residue('05671564547361')
6
>>> luhn_residue(getImei(14))
0