Original code copyright (C) 2009-2022 Rudolf Cardinal (

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Functions to help transform data.

cardinal_pythonlib.datamapping.dict_to_map(d: Dict[Any, Any]) List[Tuple[List[Any], Any]][source]

Converts a dictionary into a structure usable by map_value().

cardinal_pythonlib.datamapping.map_value(value: Any, maplist: Iterable[Tuple[Iterable[Any], Any]], transmit_none: bool = True, required: bool = True, default: Any | None = None, name: str | None = None) Any[source]
  1. If the value is None and transmit_none is true, return None.

  2. Map value to a result, via mapping, which is a list of tuples possibilities, result. Work through maplist in sequence, and if value is in possibilities, return result.

  3. If no result was found, then:

    • if required is False, return default;

    • otherwise, raise ValueError, using name to describe the problem.