File: Dictionary.py

package info (click to toggle)
uranium 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,304 kB
  • sloc: python: 31,765; sh: 132; makefile: 12
file content (24 lines) | stat: -rw-r--r-- 862 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Copyright (c) 2019 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.

from typing import Dict, Any

"""Helper functions for dealing with Python dictionaries."""


def findKey(dictionary: Dict[Any, Any], search_value: Any) -> Any:
    """Find the key corresponding to a certain value

    :param dictionary: :type{dict} The dictionary to search for the value
    :param search_value: The value to search for.

    :return: The key matching to value. Note that if the dictionary contains multiple instances of value it is undefined which exact key is returned.

    :exception ValueError: is raised when the value is not found in the dictionary.
    """

    for key, value in dictionary.items():
        if value == search_value:
            return key

    raise ValueError("Value {0} not found in dictionary".format(search_value))