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'
Parameters:

n (float) – The number of bytes

Returns:

A human readable string

Return type:

str

anemoi.utils.humanize.bytes(n: float) str

Deprecated function to convert bytes to a human readable string.

Parameters:

n (float) – The number of bytes

Returns:

A human readable string

Return type:

str

anemoi.utils.humanize.base2_to_human(n: float) str

Convert a number to a human readable string using base 2 units.

Parameters:

n (float) – The number to convert

Returns:

A human readable string

Return type:

str

anemoi.utils.humanize.base2(n: float) str

Deprecated function to convert a number to a human readable string using base 2 units.

Parameters:

n (float) – The number to convert

Returns:

A human readable string

Return type:

str

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'
Parameters:

seconds (float) – The number of seconds

Returns:

A human readable string

Return type:

str

anemoi.utils.humanize.seconds(seconds: float) str

Deprecated function to convert seconds to a human readable string.

Parameters:

seconds (float) – The number of seconds

Returns:

A human readable string

Return type:

str

anemoi.utils.humanize.plural(value: int, what: str) str

Return a string with the value and the pluralized form of what.

Parameters:
  • value (int) – The value

  • what (str) – The string to pluralize

Returns:

The value and the pluralized form of what

Return type:

str

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:

str

anemoi.utils.humanize.string_distance(s: str, t: str) int

Calculate the Levenshtein distance between two strings.

Parameters:
  • s (str) – The first string

  • t (str) – The second string

Returns:

The Levenshtein distance

Return type:

int

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'
Parameters:
  • word (str) – The word to look for

  • vocabulary (list of str) – The list of known words

Returns:

The closest word in the vocabulary

Return type:

str

anemoi.utils.humanize.dict_to_human(query: dict[str, Any]) str

Convert a dictionary to a human readable string.

Parameters:

query (dict) – The dictionary to convert

Returns:

A human readable string

Return type:

str

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'
Parameters:
  • lst (list of str) – The list of strings to concatenate

  • conjunction (str, optional) – The word to connect the last word in the list (like “or” or “and”), by default “and”

Returns:

Human readable string of list

Return type:

str

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str) – The name of the value

  • units (dict) – The units to use for conversion

  • none_ok (bool) – Whether None is an acceptable value

Returns:

The converted value

Return type:

int or None

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str, optional) – The name of the value

  • units (dict, optional) – The units to use for conversion

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value

Return type:

int or None

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value in seconds

Return type:

int or None

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value in seconds

Return type:

int or None

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value in percentage

Return type:

int or None

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value in percentage

Return type:

int or None

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value in bytes

Return type:

int or None

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.

Parameters:
  • value (str or int) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value in bytes

Return type:

int or None

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:
  • value (str) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value as a timedelta

Return type:

datetime.timedelta

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:
  • value (str) – The value to convert

  • name (str, optional) – The name of the value

  • none_ok (bool, optional) – Whether None is an acceptable value

Returns:

The converted value as a timedelta

Return type:

datetime.timedelta

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:

datetime.datetime

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.

Parameters:
  • obj (Any) – The object to be dumped as JSON.

  • max_line_length (int, optional) – Maximum allowed line length for pretty-printing. Default is 120.

  • default (function, optional) – Default function to convert non-serializable objects. Default is str.

Returns:

JSON string.

Return type:

str

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.

Parameters:
  • lst (list or tuple) – The list to be shortened.

  • max_length (int, optional) – Maximum length of the shortened list. Default is 5.

Returns:

Shortened list.

Return type:

list or tuple

anemoi.utils.humanize.compress_dates(dates: list[datetime | str]) str

Compress a list of dates into a human-readable format.

Parameters:

dates (list) – A list of dates, as datetime objects or strings.

Returns:

A human-readable string representing the compressed dates.

Return type:

str

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:

list[int]

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]