Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safer regex solution #1

Open
github-actions bot opened this issue Jun 28, 2022 · 0 comments
Open

safer regex solution #1

github-actions bot opened this issue Jun 28, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link

homeId = int(re.search(r"^/team/([0-9]+)-[\w-]+$", match["HomeTeam"]["Link"]).group(1)) # type: ignore # pylint: disable=line-too-long

awayId = int(re.search(r"^/team/([0-9]+)-[\w-]+$", match["AwayTeam"]["Link"]).group(1)) # type: ignore # pylint: disable=line-too-long

try: # TODO safer regex solution

    @property
    def pools(self) -> List[Pool]:
        return self._pools

    def get(self, index: int) -> Optional[Pool]:
        """pools[index]"""
        return self.pools[index] if index < len(self.pools) else None

    @staticmethod
    def ParsePool(pool: Dict[str, Any],
                  competition: CompetitionLink,
                  standings: Optional[StandingsPool]) -> Pool:
        pdex = DictEx(pool)
        draws: List[List[CalendarMatch]] = [ ]
        for match in pdex.ensureList("Results"):
            mdex = DictEx(match)
            homeId = 0
            awayId = 0
            try: # TODO safer regex solution
                homeId = int(re.search(r"^\/team\/([0-9]+)-[\w-]+$", match["HomeTeam"]["Link"]).group(1)) # type: ignore # pylint: disable=line-too-long
                awayId = int(re.search(r"^\/team\/([0-9]+)-[\w-]+$", match["AwayTeam"]["Link"]).group(1)) # type: ignore # pylint: disable=line-too-long
            except Exception as exc:
                print(exc)

            newMatch = CalendarMatch(mdex.ensureString("MatchCentreUrl"),
                CompetitionModel.BuildPlain(competition.name,
                    competition.gender,
                    season = str(competition.age) if competition.age else None,
                    phase = pdex.ensureString("Name"),
                    matchNumber = mdex.ensureString("MatchName")),
                Team.Build(mdex.ensureDictChain("HomeTeam").ensureString("Name"),
                    mdex.ensureDictChain("HomeTeam").ensureDictChain("Logo").ensureString("Name"),
                    "N/A",
                    True,
                    homeId),
                Team.Build(mdex.ensureDictChain("AwayTeam").ensureString("Name"),
                    mdex.ensureDictChain("AwayTeam").ensureDictChain("Logo").ensureString("Name"),
                    "N/A",
                    False,
                    awayId),
                mdex.ensureString("Location"),
                mdex.ensureString("MatchDateTime"),
                Result.ParseFromForm(match),
                mdex.ensureBool("IsComplete"))
            newDraw = True
            for draw in draws:
                if draw[0].awayTeam.name == newMatch.homeTeam.name and \
                   draw[0].homeTeam.name == newMatch.awayTeam.name:
                    newDraw = False
                    draw.append(newMatch)
                    break
            if newDraw:
                draws.append([newMatch])
        return Pool(pdex.ensureString("Name"),
                    [ Draw([ match for match in draw ])
                      for draw in draws ],
                    standings)

    def __repr__(self) -> str:
        return f"(cevlib.competitions.Round) {self._name} ({len(self.pools)} pools)"

    def toJson(self) -> Dict[str, Any]:
        return {
            "name": self._name,
            "pools": [ pool.toJson() for pool in self._pools ]
@github-actions github-actions bot added the todo label Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants