haldis/app/utils.py

37 lines
861 B
Python
Raw Permalink Normal View History

2019-09-10 13:17:35 +00:00
"Script which contains several utils for Haldis"
from typing import Iterable
2019-09-10 13:17:35 +00:00
2019-09-07 23:58:21 +00:00
def euro_string(value: int) -> str:
"""
Convert cents to string formatted euro
"""
2020-08-14 02:57:02 +00:00
euro, cents = divmod(value, 100)
if cents:
return "{}.{:02}".format(euro, cents)
else:
return "{}".format(euro)
2020-07-17 09:40:15 +00:00
2020-02-29 20:56:04 +00:00
def price_range_string(price_range, include_upper=False):
if price_range[0] == price_range[1]:
return euro_string(price_range[0])
2020-07-17 09:40:15 +00:00
return ("{}{}" if include_upper else "from {}").format(
*map(euro_string, price_range)
)
2020-02-29 20:56:04 +00:00
def first(iterable: Iterable, default=None):
"""
Return first element of iterable
"""
try:
return next(iter(iterable))
except StopIteration:
return default
def ignore_none(iterable: Iterable):
return filter(lambda x: x is not None, iterable)