]> git.mxchange.org Git - fba.git/blob - fba/helpers/dicts.py
Continued:
[fba.git] / fba / helpers / dicts.py
1 # Copyright (C) 2023 Free Software Foundation
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU Affero General Public License as published
5 # by the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU Affero General Public License for more details.
12 #
13 # You should have received a copy of the GNU Affero General Public License
14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 import logging
17
18 logging.basicConfig(level=logging.INFO)
19 logger = logging.getLogger(__name__)
20
21 def has_key(lists: list, key: str, value: any) -> bool:
22     logger.debug("lists()=%d,key='%s',value[]='%s' - CALLED!", len(lists), key, type(value))
23
24     if not isinstance(lists, list):
25         raise ValueError(f"Parameter lists[]='{type(lists)}' is not of type 'list'")
26     elif not isinstance(key, str):
27         raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'")
28     elif key == "":
29         raise ValueError("Parameter 'key' is empty")
30
31     has = False
32     logger.debug("Checking lists()=%d ...", len(lists))
33     for row in lists:
34         logger.debug("row[%s]='%s", type(row), row)
35
36         if not isinstance(row, dict):
37             raise ValueError(f"row[]='{type(row)}' is not of type 'dict'")
38         elif not key in row:
39             raise KeyError(f"Cannot find key='{key}'")
40         elif row[key] == value:
41             logger.debug("row[%s][]='%s' matches value[]='%s'", key, type(row[key]), type(value))
42             has = True
43             break
44
45     logger.debug("has='%s' - EXIT!", has)
46     return has