Ad
Web Scraping
  • Use textwrap.dedent().
  • Use response.raise_for_status(), but add helpful msg if 404.
Code
Diff
  • from textwrap import dedent
    import requests
    
    from bs4 import BeautifulSoup
    
    
    def _extract(username):
        response = requests.get(f'https://www.codewars.com/users/{username}')
        try:
            response.raise_for_status()
        except requests.exceptions.HTTPError as e:
            if response.status_code == 404:
                raise Exception(f"username is case-sensitive.", e)
            else:
                raise
        else:
            soup = BeautifulSoup(response.text, 'html.parser')
            stat_info = soup.findAll('div', class_='stat')
            return stat_info
    
    
    def _transform(username, stat_info):
        name, member, rank, honor, position, percentage, katas = (stat_info[0],
                                                                      stat_info[3],
                                                                      stat_info[9],
                                                                      stat_info[10],
                                                                      stat_info[11],
                                                                      stat_info[12],
                                                                      stat_info[13])
        return dedent(f"""\
                {username}'s Codewars stats:
                    {member.text}
                    {rank.text}
                    {honor.text}
                    {position.text}
                    {percentage.text}
                    {katas.text}""")
    
    
    def get_codewars_stats(username):
        """Scraps, and retrieves Codewars stats of given username."""
        stat_info = _extract(username)
        return _transform(username, stat_info)
    
    • from bs4 import BeautifulSoup
    • from textwrap import dedent
    • import requests
    • from bs4 import BeautifulSoup
    • def get_codewars_stats(username):
    • """Scraps, and retrieves Codewars stats of given username."""
    • source = requests.get(f'https://www.codewars.com/users/{username}')
    • # Verify request status:
    • if source.status_code == 404:
    • return 'Something went wrong, enter a valid Codewars username.'
    • def _extract(username):
    • response = requests.get(f'https://www.codewars.com/users/{username}')
    • try:
    • response.raise_for_status()
    • except requests.exceptions.HTTPError as e:
    • if response.status_code == 404:
    • raise Exception(f"username is case-sensitive.", e)
    • else:
    • raise
    • else:
    • soup = BeautifulSoup(source.text, 'html.parser')
    • soup = BeautifulSoup(response.text, 'html.parser')
    • stat_info = soup.findAll('div', class_='stat')
    • name, member, rank, honor, position, percentage, katas = (stat_info[0],
    • return stat_info
    • def _transform(username, stat_info):
    • name, member, rank, honor, position, percentage, katas = (stat_info[0],
    • stat_info[3],
    • stat_info[9],
    • stat_info[10],
    • stat_info[11],
    • stat_info[12],
    • stat_info[13])
    • return f"{username}'s Codewars stats:\n\t{member.text}\n\t{rank.text}\n\t{honor.text}\n\t{position.text}\n\t{percentage.text}\n\t{katas.text}"
    • return dedent(f"""\
    • {username}'s Codewars stats:
    • {member.text}
    • {rank.text}
    • {honor.text}
    • {position.text}
    • {percentage.text}
    • {katas.text}""")
    • def get_codewars_stats(username):
    • """Scraps, and retrieves Codewars stats of given username."""
    • stat_info = _extract(username)
    • return _transform(username, stat_info)

For me, this is easier to read.

Code
Diff
  • def is_leap_year(year: str) -> bool:
        """Returns True if year is a Leap Year else False."""
        if year % 400 == 0:
            return True
        elif year % 100 == 0:
            return False
        else:
            return year % 4 == 0
    
    • def is_leap_year(year: str) -> bool:
    • """Returns True if year is a Leap Year else False."""
    • case1: bool = year % 4 == 0
    • case2: bool = year % 100 == 0
    • case3: bool = year % 400 == 0
    • return case1 and not case2 or case3
    • if year % 400 == 0:
    • return True
    • elif year % 100 == 0:
    • return False
    • else:
    • return year % 4 == 0