humanize
Generate human readable strings.
- anemoi.utils.humanize.bytes_to_human(n: float) str
Convert a number of bytes to a human readable string.
>>> bytes_to_human(4096) '4 KiB'
>>> bytes_to_human(4000) '3.9 KiB'
- anemoi.utils.humanize.bytes(n: float) str
Deprecated function to convert bytes to a human readable string.
- anemoi.utils.humanize.base2_to_human(n: float) str
Convert a number to a human readable string using base 2 units.
- anemoi.utils.humanize.base2(n: float) str
Deprecated function to convert a number to a human readable string using base 2 units.
- anemoi.utils.humanize.seconds_to_human(seconds: float | timedelta) str
Convert a number of seconds to a human readable string.
>>> seconds_to_human(4000) '1 hour 6 minutes 40 seconds'
- anemoi.utils.humanize.seconds(seconds: float) str
Deprecated function to convert seconds to a human readable string.
- anemoi.utils.humanize.plural(value: int, what: str) str
Return a string with the value and the pluralized form of what.
- anemoi.utils.humanize.when(then: datetime, now: datetime | None = None, short: bool = True, use_utc: bool = False) str
Generate a human readable string for a date, relative to now.
>>> when(datetime.datetime.now() - datetime.timedelta(hours=2)) '2 hours ago'
>>> when(datetime.datetime.now() - datetime.timedelta(days=1)) 'yesterday at 08:46'
>>> when(datetime.datetime.now() - datetime.timedelta(days=5)) 'last Sunday'
>>> when(datetime.datetime.now() - datetime.timedelta(days=365)) 'last year'
>>> when(datetime.datetime.now() + datetime.timedelta(days=365)) 'next year'
- Parameters:
then (datetime.datetime) – A datetime
now (datetime.datetime, optional) – The reference date, by default NOW
short (bool, optional) – Generate shorter strings, by default True
use_utc (bool, optional) – Use UTC time, by default False
- Returns:
A human readable string
- Return type:
- anemoi.utils.humanize.string_distance(s: str, t: str) int
Calculate the Levenshtein distance between two strings.
- anemoi.utils.humanize.did_you_mean(word: str, vocabulary: list[str]) str
Pick the closest word in a vocabulary.
>>> did_you_mean("aple", ["banana", "lemon", "apple", "orange"]) 'apple'
- anemoi.utils.humanize.dict_to_human(query: dict[str, Any]) str
Convert a dictionary to a human readable string.
- anemoi.utils.humanize.list_to_human(lst: list[str], conjunction: str = 'and') str
Convert a list of strings to a human readable string.
>>> list_to_human(["banana", "lemon", "apple", "orange"]) 'banana, lemon, apple and orange'
- anemoi.utils.humanize.human_to_number(value: str | int, name: str, units: dict[str, int], none_ok: bool) int | None
Convert a human readable string to a number.
- anemoi.utils.humanize.as_number(value: str | int, name: str | None = None, units: dict[str, int] | None = None, none_ok: bool = False) int | None
Deprecated function to convert a human readable string to a number.
- anemoi.utils.humanize.human_seconds(value: str | int, name: str | None = None, none_ok: bool = False) int | None
Convert a human readable string to seconds.
- anemoi.utils.humanize.as_seconds(value: str | int, name: str | None = None, none_ok: bool = False) int | None
Deprecated function to convert a human readable string to seconds.
- anemoi.utils.humanize.human_to_percent(value: str | int, name: str | None = None, none_ok: bool = False) int | None
Convert a human readable string to a percentage.
- anemoi.utils.humanize.as_percent(value: str | int, name: str | None = None, none_ok: bool = False) int | None
Deprecated function to convert a human readable string to a percentage.
- anemoi.utils.humanize.human_to_bytes(value: str | int, name: str | None = None, none_ok: bool = False) int | None
Convert a human readable string to bytes.
- anemoi.utils.humanize.as_bytes(value: str | int, name: str | None = None, none_ok: bool = False) int | None
Deprecated function to convert a human readable string to bytes.
- anemoi.utils.humanize.human_to_timedelta(value: str, name: str | None = None, none_ok: bool = False) timedelta
Convert a human readable string to a timedelta.
- Parameters:
- Returns:
The converted value as a timedelta
- Return type:
- anemoi.utils.humanize.as_timedelta(value: str, name: str | None = None, none_ok: bool = False) timedelta
Deprecated function to convert a human readable string to a timedelta.
- Parameters:
- Returns:
The converted value as a timedelta
- Return type:
- anemoi.utils.humanize.rounded_datetime(d: datetime) datetime
Round a datetime to the nearest second.
- Parameters:
d (datetime.datetime) – The datetime to round
- Returns:
The rounded datetime
- Return type:
- anemoi.utils.humanize.json_pretty_dump(obj: Any, max_line_length: int = 120, default: Callable = <class 'str'>) str
Custom JSON dump function that keeps dicts and lists on one line if they are short enough.
- anemoi.utils.humanize.shorten_list(lst: list[Any] | tuple[Any], max_length: int = 5) list[Any] | tuple[Any]
Shorten a list to a maximum length.
- anemoi.utils.humanize.compress_dates(dates: list[datetime | str]) str
Compress a list of dates into a human-readable format.
- anemoi.utils.humanize.print_dates(dates: list[datetime | str]) None
Print a list of dates in a human-readable format.
- Parameters:
dates (list) – A list of dates, as datetime objects or strings.
- anemoi.utils.humanize.make_list_int(value: str | list[int] | tuple[int] | int) list[int]
Convert a value to a list of integers.
Handles slash-separated strings including MARS-style range notation:
"1/2/3","1/to/3", and"1/to/10/by/2".- Parameters:
value (str, list, tuple, or int) – The value to convert to a list of integers.
- Returns:
A list of integers.
- Return type:
- Raises:
ValueError – If the value cannot be converted to a list of integers.
Examples
>>> make_list_int("1/2/3") [1, 2, 3] >>> make_list_int("0/to/6") [0, 1, 2, 3, 4, 5, 6] >>> make_list_int("0/to/12/by/6") [0, 6, 12]