Skip to content

Commit

Permalink
fix: fixed unsupported big float values (#171)
Browse files Browse the repository at this point in the history
* fix: fixed unsupported big float values

* fix: applied sourcery's suggestions. added caching on frequently called methods with few possible inputs.
  • Loading branch information
TeKrop authored Aug 2, 2024
1 parent 40d067a commit f466a43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
18 changes: 13 additions & 5 deletions app/parsers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from app.common.helpers import read_csv_data_file
from app.config import settings

DURATION_HOURS_PATTERN = re.compile(r"^(-?\d+,?\d*?):(\d+):(\d+)$")
DURATION_MINUTES_PATTERN = re.compile(r"^(-?\d+):(\d+)$")
INT_PATTERN = re.compile(r"^-?\d+(,\d+)*%?$")
FLOAT_PATTERN = re.compile(r"^-?\d+(,\d+)*\.\d+$")


def get_computed_stat_value(input_str: str) -> str | float | int:
"""Get computed value from player statistics : convert duration representations
Expand All @@ -16,24 +21,24 @@ def get_computed_stat_value(input_str: str) -> str | float | int:
"""

# Duration format in hour:min:sec => seconds
if result := re.match(r"^(-?\d+,?\d*?):(\d+):(\d+)$", input_str):
if result := DURATION_HOURS_PATTERN.match(input_str):
return (
int(result[1].replace(",", "")) * 3600
+ int(result[2]) * 60
+ int(result[3])
)

# Duration format in min:sec => seconds
if result := re.match(r"^(-?\d+):(\d+)$", input_str):
if result := DURATION_MINUTES_PATTERN.match(input_str):
return int(result[1]) * 60 + int(result[2])

# Int format
if re.match(r"^-?\d+(,\d+)*%?$", input_str):
if INT_PATTERN.match(input_str):
return int(input_str.replace("%", "").replace(",", ""))

# Float format
if re.match(r"^-?\d+\.\d+$", input_str):
return float(input_str)
if FLOAT_PATTERN.match(input_str):
return float(input_str.replace(",", ""))

# Return 0 value if :
# - Zero time fought with a character ("--")
Expand Down Expand Up @@ -61,6 +66,7 @@ def get_full_url(url: str) -> str:
return f"{settings.blizzard_host}{url}" if url.startswith("/") else url


@cache
def get_hero_keyname(input_str: str) -> str:
"""Returns Overwatch hero keyname using its fullname.
Example : ("Soldier: 76" -> "soldier-76")
Expand Down Expand Up @@ -94,12 +100,14 @@ def get_tier_from_icon(tier_url: str) -> int:
return 0


@cache
def remove_accents(input_str: str) -> str:
"""Removes accents from a string and return the resulting string"""
nfkd_form = unicodedata.normalize("NFKD", input_str)
return "".join([c for c in nfkd_form if not unicodedata.combining(c)])


@cache
def string_to_snakecase(input_str: str) -> str:
"""Returns a string transformed in snakecase format"""
cleaned_str = remove_accents(input_str).replace("- ", "")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "overfast-api"
version = "2.33.0"
version = "2.33.1"
description = "Overwatch API giving data about heroes, maps, and players statistics."
license = "MIT"
authors = ["Valentin PORCHET <valentin.porchet@proton.me>"]
Expand Down
14 changes: 8 additions & 6 deletions tests/parsers/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
("input_str", "result"),
[
# Time format in hour:min:sec => seconds
("1,448:50:56", 5215856),
("205:08:38", 738518),
("12:03:52", 43432),
("5:42:42", 20562),
("1,448:50:56", 5_215_856),
("205:08:38", 738_518),
("12:03:52", 43_432),
("5:42:42", 20_562),
("-0:00:00", 0),
# Time format in min:sec => seconds
("11:40", 700),
Expand All @@ -26,12 +26,14 @@
("-86", -86),
("46%", 46),
("208", 208),
("12,585", 12585),
("68,236,356", 68236356),
("12,585", 12_585),
("68,236,356", 68_236_356),
# Float format
("7.58", 7.58),
("37.89", 37.89),
("-86.96", -86.96),
("1,102.5", 1_102.5),
("1,234,567.89", 1_234_567.89),
# Zero time fought with a character
("--", 0),
# Invalid value (not a number)
Expand Down

0 comments on commit f466a43

Please sign in to comment.