vansh-tiwari / trashtalk

Codechef profile comparator
Apache License 2.0
1 stars 0 forks source link

Code Readability #1

Closed harshraj22 closed 4 years ago

harshraj22 commented 4 years ago

https://github.com/vansh-tiwari/trashtalk/blob/449a887368f8ac7dd7b977690c3c66c87c79745c/codechef.py#L30

This section of code can be made more readable, original code:

        for i in range(1, len(infoTag.section.ul) - 2, 2):
            key = infoTag.section.ul.contents[i].label.text[:-1]
            value = infoTag.section.ul.contents[i].span.text
            if key == "Username":
                userInfo[key] = value[2:]
                userInfo["Star"] = value[0:2]
            elif key=="Country":
                userInfo[key] = value[2:]
            else:
                userInfo[key] = infoTag.section.ul.contents[i].span.text

Converted code


        for li in infoTag.section.ul.findAll('li'):
            key, value = li.text.strip().split(':', 1)
            userInfo[key] = value.strip()
        userInfo["Star"] = userInfo["Username"][:2]
        userInfo["Username"] = userInfo["Username"][2:]

==============================================================================

Similarly https://github.com/vansh-tiwari/trashtalk/blob/449a887368f8ac7dd7b977690c3c66c87c79745c/codechef.py#L69

        m, n = 0, 0
        for i in range(0, len(rank) - 2, 2):
            a = conRate[1].tbody.contents[i]
            for x in range(1, len(a.contents), 2):
                #         print(a.contents[x].text, x, type(a.contents[x].text))
                #         print(a.contents[1].text)
                #         print(rankTitle[x].text)
                #         rankData[a.contents[1].text][rankTitle[x].text] = a.contents[x].text
                rankList[m][n] = a.contents[x].text
                n += 1
            m += 1
            n = 0

can be modified to

        rankList = []
        for ranks in rank:
            try:
                contestRank = [details.text for details in ranks.find_all('td')]
                rankList.append(contestRank)
            except AttributeError as e:
                continue

P.S. Is the repo open for contributions ?

vansh-tiwari commented 4 years ago

https://github.com/vansh-tiwari/trashtalk/blob/449a887368f8ac7dd7b977690c3c66c87c79745c/codechef.py#L30

This section of code can be made more readable, original code:

        for i in range(1, len(infoTag.section.ul) - 2, 2):
            key = infoTag.section.ul.contents[i].label.text[:-1]
            value = infoTag.section.ul.contents[i].span.text
            if key == "Username":
                userInfo[key] = value[2:]
                userInfo["Star"] = value[0:2]
            elif key=="Country":
                userInfo[key] = value[2:]
            else:
                userInfo[key] = infoTag.section.ul.contents[i].span.text

Converted code

        for li in infoTag.section.ul.findAll('li'):
            key, value = li.text.strip().split(':', 1)
            userInfo[key] = value.strip()
        userInfo["Star"] = userInfo["Username"][:2]
        userInfo["Username"] = userInfo["Username"][2:]

==============================================================================

Similarly

https://github.com/vansh-tiwari/trashtalk/blob/449a887368f8ac7dd7b977690c3c66c87c79745c/codechef.py#L69

        m, n = 0, 0
        for i in range(0, len(rank) - 2, 2):
            a = conRate[1].tbody.contents[i]
            for x in range(1, len(a.contents), 2):
                #         print(a.contents[x].text, x, type(a.contents[x].text))
                #         print(a.contents[1].text)
                #         print(rankTitle[x].text)
                #         rankData[a.contents[1].text][rankTitle[x].text] = a.contents[x].text
                rankList[m][n] = a.contents[x].text
                n += 1
            m += 1
            n = 0

can be modified to

        rankList = []
        for ranks in rank:
            try:
                contestRank = [details.text for details in ranks.find_all('td')]
                rankList.append(contestRank)
            except AttributeError as e:
                continue

P.S. Is the repo open for contributions ?

Yes, Please pull the requests for the same so that I can merge them. Thank you.