Files
shadow/tests/system/framework/misc/__init__.py
2025-08-26 22:55:14 +02:00

55 lines
1.4 KiB
Python

"""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