2019-09-10 15:17:35 +02:00
|
|
|
"Script which contains several utils for Haldis"
|
|
|
|
|
2022-04-22 01:15:54 +02:00
|
|
|
import re
|
|
|
|
from typing import Iterable, Optional
|
2020-01-27 02:31:02 +01:00
|
|
|
|
2019-09-10 15:17:35 +02:00
|
|
|
|
2022-05-11 02:43:08 +02:00
|
|
|
def euro_string(value: Optional[int], unit="€ ") -> str:
|
2019-08-28 03:46:04 +02:00
|
|
|
"""
|
|
|
|
Convert cents to string formatted euro
|
|
|
|
"""
|
2022-05-11 02:43:08 +02:00
|
|
|
if value is None:
|
|
|
|
return "✗"
|
2020-08-14 04:57:02 +02:00
|
|
|
euro, cents = divmod(value, 100)
|
|
|
|
if cents:
|
2022-04-22 01:15:54 +02:00
|
|
|
return f"{unit}{euro}.{cents:02}"
|
|
|
|
return f"{unit}{euro}"
|
|
|
|
|
|
|
|
|
|
|
|
def parse_euro_string(value: str) -> Optional[int]:
|
|
|
|
m = re.fullmatch("(?:€ ?)?([0-9]+)(?:[.,]([0-9]+))?", value)
|
|
|
|
if not m:
|
|
|
|
return None
|
|
|
|
cents_02 = "{:0<2.2}".format(m.group(2)) if m.group(2) else "00"
|
|
|
|
return int(m.group(1)) * 100 + int(cents_02)
|
2020-01-27 02:31:02 +01:00
|
|
|
|
2020-07-17 11:40:15 +02:00
|
|
|
|
2020-02-29 21:56:04 +01:00
|
|
|
def price_range_string(price_range, include_upper=False):
|
2022-04-19 23:20:03 +02:00
|
|
|
"Convert a price range to a string formatted euro"
|
2020-02-29 21:56:04 +01:00
|
|
|
if price_range[0] == price_range[1]:
|
|
|
|
return euro_string(price_range[0])
|
2020-07-17 11:40:15 +02:00
|
|
|
return ("{}—{}" if include_upper else "from {}").format(
|
|
|
|
*map(euro_string, price_range)
|
|
|
|
)
|
2020-02-29 21:56:04 +01:00
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
|
|
|
|
def first(iterable: Iterable, default=None):
|
|
|
|
"""
|
|
|
|
Return first element of iterable
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return next(iter(iterable))
|
|
|
|
except StopIteration:
|
|
|
|
return default
|
|
|
|
|
2020-02-24 00:31:14 +01:00
|
|
|
|
|
|
|
def ignore_none(iterable: Iterable):
|
2022-04-19 23:20:03 +02:00
|
|
|
"Filter to ignore None objects"
|
2020-02-24 00:31:14 +01:00
|
|
|
return filter(lambda x: x is not None, iterable)
|