I'm just looking for a cleaner way. Obviously I could store a variable for some of the int parts. Personally, I think the one-liner looks better and everyone's definition of what constitutes clean readable code differs.
def gen_isbn(isbn):
for i, val in enumerate(isbn):
if len(isbn) == 9:
yield (i + 1) * int(val)
else:
yield int(val) * 3 if (i + 1) % 2 == 0 else int(val) * 1
def isbn10(isbn):
mod = sum(gen_isbn(isbn[:-1])) % 11
return mod == int(isbn[-1]) if isbn[-1] != 'X' else mod == 10
def isbn13(isbn):
mod = sum(gen_isbn(isbn[:-1])) % 10
return 10 - mod == int(isbn[-1]) if mod != 10 else int(isbn[-1]) == 0
I'm really just wondering if there's a regex or cleaner one liners etc since this is already pretty short and works. It should be for both ISBN 10 and 13.
Preferably, I would prefer pure Python. These are grabbed from web-scraping and hence I have enough external libraries floating around than throwing another one in just for ISBN validation.