This should be a short review mainly because your code looks pretty nice.
I don't have any experience with the Amazon API, but from what I can tell you are using it as I would expect it to be used.
The only points that I want to speak on and can nit-pick are:
Your variables
asin_regexandisbn_regexare slightly misleading. In Python there is a distinction between regular expression patterns and regular expression objects. The suffix_regeximplies that, by itself, the variable can recognize expressions which, in turn, implies the variable is a regular expression object. Because your variablevariables cannot, by themselves, recognize expressions, I would use the suffix_pattern.Your logic in
get_amazon_idis completely fine. Its my personal preference, in this case where you are simply returning inside the if blocks, to just use if statements, noeliforelseblocks:if asin_search: return asin_search.group(1) if isbn_search: return isbn_search.group(1) return NoneThe above is strictly personal preference, simply getting another version out there.
The above is strictly personal preference, simply getting another version out there.
However, what I would recommend doing is use a for loop to reduce some of the minor code repetition and make your code more flexible:
for search in asin_search, isbn_search: if search: return search.group(1) return NoneThe above code would be best if, say, for some reason another ID type came out. So, instead of having to add another if statement, all you need to do is add
new_id_searchto the for looplist.I would change your regex patterns to use the
\wand\dcharacters:asin_regex = r'/(\w{10})' isbn_regex = r'/(\d{10})'The
\dcharacter would just make your currently short regex even shorter, while the\wcharacter helps protect against the case that for some unknown reason the ASIN or ISBN contains lower-case characters.Finally, I would capitalize the first word of all your comments (unless it is a variable name).