Coding style
It's a bit hard to read this code because it doesn't follow PEP8. The violations that stick in the eye the most:
- No spacing around commas:
- bad :
{"breweries":[],"beers":[]} - good:
{"breweries": [], "beers": []}
- bad :
- No line breaks after
:, and unconventional spacing inifstatements, for example inif "ABV" in label.text: key = "abv"
There are everywhere in the code.
I suggest to get the pep8 command line tool (pip install pep8),
run it on your project and correct all the violations.
Even with all PEP8 violations fixed,
the could would benefit from more generous use of vertical spacing.
For example the beer and reviews methods are too dense.
It would be better to put some blank lines occasionally to create a sense of visual grouping of tightly related code,
separating from loosely related ones.
Mutually exclusive if statements
It seems to me that these conditions are mutually exclusive:
if "RATINGS" in label.text: key = "num_ratings" if "CALORIES" in label.text: key = "calories" if "ABV" in label.text: key = "abv" if "SEASONAL" in label.text: key = "season" if "IBU" in label.text: key = "ibu"
As such, it's a waste to make the program evaluate them all unnecessarily.
These should be chained together with elif.
Don't repeat yourself
This piece of code appears in many places:
soup = BeautifulSoup(r.text, "lxml")
It would be better to create a helper method for this:
def get_soup(text):
return BeautifulSoup(text, "lxml")
Other issues
Remove unused imports:
import exceptions
This docstring is wrong:
>>> summit_epa = RateBeer().beer("summit extra pale ale")
Should have been:
>>> RateBeer().search("summit extra pale ale")
>>> summit_epa = RateBeer().beer("/beer/summit-extra-pale-ale/7344/")
Modern style classes should extend object:
class RateBeer(object):