Util Methods Module
filter_dicts_by_values(raw_dicts, filter_value, keep=None, drop=None)
Filters a list of dicts based whether contain a specific value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_dicts |
List[Dict]
|
The list of dicts to filter. |
required |
filter_value |
str | List | None
|
The value or a list of values to filter by. Can be |
required |
keep |
bool | None
|
Whether to keep the dictionaries that contain the |
None
|
drop |
bool | None
|
Whether to exclude the dictionaries that contain the |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If |
TypeError
|
If |
TypeError
|
If |
Returns:
| Type | Description |
|---|---|
List[Dict]
|
List[Dict]: The filtered list of dictionaries. |
Examples:
>>> filter_dicts_by_values([{"A": "1", "B": "2"}, {"C": "3", "D": "4"}], filter_value="3", keep=False)
[{'A': '1', 'B': '2'}]
>>> filter_dicts_by_values([{"A": "1", "B": "2"}, {"C": "3", "D": "4"}], filter_value="3", keep=True)
[{'C': '3', 'D': '4'}]
>>> filter_dicts_by_values([{"A": "1", "B": "2"}, {"C": "3", "D": "4"}], filter_value="3", drop=False)
[{'C': '3', 'D': '4'}]
>>> filter_dicts_by_values([{"A": "1", "B": "2"}, {"C": "3", "D": "4"}], filter_value="3", drop=True)
[{'A': '1', 'B': '2'}]
Source code in carol_app_monitor/utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
merge_dicts(*dicts)
Merge dicts into unique dict.
Raises:
| Type | Description |
|---|---|
TypeError
|
If any args aren't a dict, raises: Args must be a |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
Merged dict. |
Examples:
>>> merge_dicts({"A": 1, "B": 2},{"C": 3, "D": 4})
{'A': 1, 'B': 2, 'C': 3, 'D': 4}
Source code in carol_app_monitor/utils.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |