Skip to main content
3 of 3
added 4 characters in body
BeetDemGuise
  • 4.2k
  • 12
  • 29

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:

  1. Your variables asin_regex and isbn_regex are slightly misleading. In Python there is a distinction between regular expression patterns and regular expression objects. The suffix _regex implies that, by itself, the variable can recognize expressions which, in turn, implies the variable is a regular expression object. Because your variables cannot, by themselves, recognize expressions, I would use the suffix _pattern.

  2. Your logic in get_amazon_id is completely fine. Its my personal preference, in this case where you are simply returning inside the if blocks, to just use if statements, no elif or else blocks:

    if asin_search:
        return asin_search.group(1)
    if isbn_search:
        return isbn_search.group(1)
    
    return None
    

    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 None
    

    The 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_search to the for loop list.

  3. I would change your regex patterns to use the \w and \d characters:

    asin_regex = r'/(\w{10})'
    isbn_regex = r'/(\d{10})' 
    

    The \d character would just make your currently short regex even shorter, while the \w character helps protect against the case that for some unknown reason the ASIN or ISBN contains lower-case characters.

  4. Finally, I would capitalize the first word of all your comments (unless it is a variable name).

BeetDemGuise
  • 4.2k
  • 12
  • 29