ats_utilities.utils.dicts module

Module

dicts.py

Copyright

Copyright (C) 2017 - 2026 Vladimir Roncevic <elektron.ronca@gmail.com> ats_utilities is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ats_utilities is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Info

Defines factory dict utility functions.

ats_utilities.utils.dicts.cherry_pick_dict(source: Mapping[Any, Any], keys: frozenset[str]) dict[Any, Any][source]

Cherry picks keys from a source dictionary.

Parameters:
  • source (<Mapping[Any, Any]>) – Source dictionary from which to pick keys.

  • keys (<frozenset[str]>) – Set of keys to pick from the source dictionary.

Returns:

Dictionary with cherry picked keys.

Return type:

<dict[Any, Any]>

Exceptions:

None.

ats_utilities.utils.dicts.get_first_available(source: Mapping[Any, Any], keys: Sequence[Any], exc_message: str | None = None) Any | None[source]

Retrieves the first available value from a list of keys in priority order. Simulates the logic of: source.get(key1) or source.get(key2) …

Parameters:
  • source (<Mapping[Any, Any]>) – Source dictionary/mapping to search.

  • keys (<Sequence[Any]>) – Sequence of keys to check in order of priority.

  • exc_message (<str | None>) – Message to include in the exception message.

Returns:

The first non-empty value found, or None if none of the keys exist/have value.

Return type:

<Any | None>

Exceptions:
ATSTypeError: Parameters (source and keys) types validation failed.
ats_utilities.utils.dicts.has_required_keys(source: Mapping[Any, Any], keys: frozenset[str]) bool[source]

Checks if all required keys are present in the source dictionary.

Parameters:
  • source (<dict[Any, Any]>) – Source dictionary to check.

  • keys (<frozenset[str]>) – Set of mandatory keys.

Returns:

True (passed), False (failed).

Return type:

<bool>

Exceptions:

None.

ats_utilities.utils.dicts.require_keys(source: ~collections.abc.Mapping[~typing.Any, ~typing.Any], keys: frozenset[str], exc_message: str | None = None, exception_class: type[BaseException] = <class 'ats_utilities.exceptions.ats_value_error.ATSValueError'>) None[source]

Requires all keys to be present in the source dictionary.

Parameters:
  • source (<Mapping[Any, Any]>) – Source dictionary to check.

  • keys (<frozenset[str]>) – Set of mandatory keys.

  • exc_message (<str | None>) – Message to include in the exception message.

  • exception_class (<type[Exception]> (default ATSValueError)) – The exception class to raise if value is None.

Exceptions:
ATSTypeError: Parameters (source and keys) types validation failed.
Dynamically raises the provided exception_class (e.g., ATSValueError).