]> 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 def has_key(lists: list, key: str, value: any) -> bool:
17     # DEBUG: print(f"DEBUG: lists()={len(lists)},key='{key}',value[]='{type(value)}' - CALLED!")
18     if not isinstance(lists, list):
19         raise ValueError(f"Parameter lists[]='{type(lists)}' is not 'list'")
20     elif not isinstance(key, str):
21         raise ValueError(f"Parameter key[]='{type(key)}' is not 'str'")
22     elif key == "":
23         raise ValueError("Parameter 'key' is empty")
24
25     has = False
26     # DEBUG: print(f"DEBUG: Checking lists()={len(lists)} ...")
27     for row in lists:
28         # DEBUG: print(f"DEBUG: row['{type(row)}']={row}")
29         if not isinstance(row, dict):
30             raise ValueError(f"row[]='{type(row)}' is not 'dict'")
31         elif not key in row:
32             raise KeyError(f"Cannot find key='{key}'")
33         elif row[key] == value:
34             has = True
35             break
36
37     # DEBUG: print(f"DEBUG: has={has} - EXIT!")
38     return has