1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
"""Miscellaneous functions."""
from __future__ import annotations
import datetime
from typing import Any
def to_list(value: Any | list[Any] | None) -> list[Any]:
"""
Convert value into a list.
- if value is ``None`` then return an empty list
- if value is already a list then return it unchanged
- if value is not a list then return ``[value]``
:param value: Value that should be converted to a list.
:type value: Any | list[Any] | None
:return: List with the value as an element.
:rtype: list[Any]
"""
if value is None:
return []
if isinstance(value, list):
return value
return [value]
def to_list_of_strings(value: Any | list[Any] | None) -> list[str]:
"""
Convert given list or single value to list of strings.
The ``value`` is first converted to a list and then ``str(item)`` is run on
each of its item.
:param value: Value to convert.
:type value: Any | list[Any] | None
:return: List of strings.
:rtype: list[str]
"""
return [str(x) for x in to_list(value)]
def days_since_epoch():
"""
Gets the current date and returns the number of days since 1970-01-01 UTC, this time is also referred to as the
Unix epoch.
"""
epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
now_utc = datetime.datetime.now(datetime.timezone.utc)
delta = now_utc - epoch
return delta.days
|