Skip to content

Commit

Permalink
Refactor string parsers for more readable an idiomatic code (Delgan#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
interrogator authored and Delgan committed Nov 24, 2019
1 parent b052dc0 commit 8a3d0d5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
42 changes: 19 additions & 23 deletions loguru/_string_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,31 @@ def parse_duration(duration):


def parse_frequency(frequency):
frequencies = {
"hourly": Frequencies.hourly,
"daily": Frequencies.daily,
"weekly": Frequencies.weekly,
"monthly": Frequencies.monthly,
"yearly": Frequencies.yearly,
}
frequency = frequency.strip().lower()

if frequency == "hourly":

return Frequencies.hourly
elif frequency == "daily":

return Frequencies.daily
elif frequency == "weekly":

return Frequencies.weekly
elif frequency == "monthly":

return Frequencies.monthly
elif frequency == "yearly":

return Frequencies.yearly

return None
return frequencies.get(frequency, None)


def parse_day(day):
days = {
"monday": 0,
"tuesday": 1,
"wednesday": 2,
"thursday": 3,
"friday": 4,
"saturday": 5,
"sunday": 6,
}
day = day.strip().lower()
days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
reg = re.compile(r"^w\d+$", flags=re.I)

if day in days:
day = days.index(day)
elif reg.match(day):
return days[day]
elif day.startswith("w") and day[1:].isdigit():
day = int(day[1:])
if not 0 <= day < 7:
raise ValueError("Invalid weekday value while parsing day (expected [0-6]): '%d'" % day)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_filesink_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_invalid_compression(compression):
logger.add("test.log", compression=compression)


@pytest.mark.parametrize("compression", ["rar", ".7z", "tar.zip"])
@pytest.mark.parametrize("compression", ["rar", ".7z", "tar.zip", "__dict__"])
def test_unknown_compression(compression):
with pytest.raises(ValueError):
logger.add("test.log", compression=compression)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_filesink_retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def test_invalid_retention(retention):


@pytest.mark.parametrize(
"retention", ["W5", "monday at 14:00", "sunday", "nope", "5 MB", "3 hours 2 dayz", "d", "H"]
"retention",
["W5", "monday at 14:00", "sunday", "nope", "5 MB", "3 hours 2 dayz", "d", "H", "__dict__"],
)
def test_unkown_retention(retention):
with pytest.raises(ValueError):
Expand Down
1 change: 1 addition & 0 deletions tests/test_filesink_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ def test_invalid_rotation(rotation):
"foobar",
"w5 at [not|a|time]",
"[not|a|day] at 12:00",
"__dict__",
],
)
def test_unknown_rotation(rotation):
Expand Down

0 comments on commit 8a3d0d5

Please sign in to comment.