Some more code

This commit is contained in:
Alexis Métaireau 2023-10-11 11:12:23 +02:00
parent 1e94c4e407
commit c754671610
2 changed files with 65 additions and 0 deletions

16
Pipfile Normal file
View file

@ -0,0 +1,16 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
pelican = "*"
markdown = "*"
datefinder = "*"
typogrify = "*"
ghp-import = "*"
[dev-packages]
[requires]
python_version = "3.9"

View file

@ -0,0 +1,49 @@
---
title: Convert string to duration
tags: python, conversion
---
I found myself wanting to convert a string to a duration (int), for some configuration.
Something you can call like this:
```python
string_to_duration("1d", target="days") # returns 1
string_to_duration("1d", target="hours") # returns 24
string_to_duration("3m", target="hours") # returns 3 * 24 * 30
```
The code :
```python
from typing import Literal
def string_to_duration(value: str, target: Literal["days", "hours"]):
"""Convert a string to a number of hours, or days"""
num = int("".join(filter(str.isdigit, value)))
if target == "hours":
reconvert = True
if "h" in value:
if target == "days":
raise ValueError("Invalid duration value", value)
num = num
reconvert = False
elif "d" in value:
num = num
elif "w" in value:
num = num * 7
elif "m" in value:
num = num * 30
elif "y" in value:
num = num * 365
else:
raise ValueError("Invalid duration value", value)
if target == "hours" and reconvert:
num = num * 24
return num
``````