mirror of
https://github.com/almet/notmyidea.git
synced 2025-04-28 19:42:37 +02:00
Update code
This commit is contained in:
parent
5b7c8df389
commit
145fc73707
1 changed files with 30 additions and 19 deletions
|
@ -8,9 +8,10 @@ I found myself wanting to convert a string to a duration (int), for some configu
|
||||||
Something you can call like this:
|
Something you can call like this:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
string_to_duration("1d", target="days") # returns 1
|
string_to_duration("1d", target="days")
|
||||||
string_to_duration("1d", target="hours") # returns 24
|
string_to_duration("1d", target="hours")
|
||||||
string_to_duration("3m", target="hours") # returns 3 * 24 * 30
|
string_to_duration("3m", target="hours")
|
||||||
|
string_to_duration("3m", target="minutes")
|
||||||
```
|
```
|
||||||
|
|
||||||
The code :
|
The code :
|
||||||
|
@ -18,32 +19,42 @@ The code :
|
||||||
```python
|
```python
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
|
def string_to_duration(value: str, target: Literal["days", "hours", "minutes"]):
|
||||||
def string_to_duration(value: str, target: Literal["days", "hours"]):
|
"""Convert a string to a number of hours, days or minutes"""
|
||||||
"""Convert a string to a number of hours, or days"""
|
|
||||||
num = int("".join(filter(str.isdigit, value)))
|
num = int("".join(filter(str.isdigit, value)))
|
||||||
|
|
||||||
if target == "hours":
|
# It's not possible to convert from a smaller unit to a greater one:
|
||||||
reconvert = True
|
# - hours and minutes cannot be converted to days
|
||||||
|
# - minutes cannot be converted to hours
|
||||||
|
if (target == "days" and ("h" in value or "m" in value.replace("mo", ""))) or (
|
||||||
|
target == "hours" and "m" in value.replace("mo", "")
|
||||||
|
):
|
||||||
|
msg = (
|
||||||
|
"Durations cannot be converted from a smaller to a greater unit. "
|
||||||
|
f"(trying to convert '{value}' to {target})"
|
||||||
|
)
|
||||||
|
raise ValueError(msg, value)
|
||||||
|
|
||||||
|
# Consider we're converting to minutes, do the eventual multiplication at the end.
|
||||||
if "h" in value:
|
if "h" in value:
|
||||||
if target == "days":
|
num = num * 60
|
||||||
raise ValueError("Invalid duration value", value)
|
|
||||||
num = num
|
|
||||||
reconvert = False
|
|
||||||
elif "d" in value:
|
elif "d" in value:
|
||||||
num = num
|
num = num * 60 * 24
|
||||||
elif "w" in value:
|
elif "w" in value:
|
||||||
num = num * 7
|
num = num * 60 * 24 * 7
|
||||||
elif "m" in value:
|
elif "mo" in value:
|
||||||
num = num * 30
|
num = num * 60 * 24 * 30 # considers 30d in a month
|
||||||
elif "y" in value:
|
elif "y" in value:
|
||||||
num = num * 365
|
num = num * 60 * 24 * 365
|
||||||
|
elif "m" in value:
|
||||||
|
num = num
|
||||||
else:
|
else:
|
||||||
raise ValueError("Invalid duration value", value)
|
raise ValueError("Invalid duration value", value)
|
||||||
|
|
||||||
if target == "hours" and reconvert:
|
if target == "hours":
|
||||||
num = num * 24
|
num = num / 60
|
||||||
|
elif target == "days":
|
||||||
|
num = num / 60 / 24
|
||||||
|
|
||||||
return num
|
return num
|
||||||
```
|
```
|
Loading…
Reference in a new issue