trestle.common.list_utils
trestle.common.list_utils ¤
Trestle List Utils.
Attributes¤
Classes¤
Functions:¤
as_dict(dict_or_none) ¤
Convert dict or None object to itself or an empty dict if none.
Source code in trestle/common/list_utils.py
64 65 66 | |
as_filtered_list(list_or_none, filter_condition) ¤
Convert to list and filter based on the condition.
Source code in trestle/common/list_utils.py
57 58 59 60 61 | |
as_list(list_or_none) ¤
Convert list or None object to itself or an empty list if none.
Use this for safe iteration and list operations.
For Pydantic v2 models with min_length constraints, use this pattern: temp_list = as_list(obj.field) # Get working list temp_list.append(item) # Modify it obj.field = none_if_empty(temp_list) # Assign back, converting [] to None
Source code in trestle/common/list_utils.py
24 25 26 27 28 29 30 31 32 33 34 | |
comma_colon_sep_to_dict(string_or_none) ¤
Convert optional comma and colon-sep list to dict.
Source code in trestle/common/list_utils.py
43 44 45 46 47 48 49 50 51 52 53 54 | |
comma_sep_to_list(string_or_none) ¤
Convert optional comma-sep string to list of strings and strip.
Source code in trestle/common/list_utils.py
37 38 39 40 | |
deep_append(dic, path, value) ¤
Append to list in dict.
Source code in trestle/common/list_utils.py
195 196 197 198 199 200 201 202 203 204 | |
deep_get(dic, path, default=None) ¤
Get value from deep in dictionary.
Source code in trestle/common/list_utils.py
174 175 176 177 178 179 180 181 182 | |
deep_set(dic, path, value, pop_if_none=True) ¤
Set value deep in dictionary.
pop_if_none will cause the key to be removed if value is None
Source code in trestle/common/list_utils.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
deep_update(dic, path, dic_value) ¤
Update the dict based on path.
Source code in trestle/common/list_utils.py
185 186 187 188 189 190 191 192 | |
delete_item_from_list(item_list, value, key) ¤
Remove the first matching item if it is present in a list based on the callable key matching the query value.
Source code in trestle/common/list_utils.py
116 117 118 119 120 121 122 | |
delete_list_from_list(item_list, indices) ¤
Delete a list of items from a list based on indices.
Source code in trestle/common/list_utils.py
146 147 148 149 | |
get_default(item, default) ¤
Return the default value for the item if it is not set.
Source code in trestle/common/list_utils.py
83 84 85 | |
get_item_from_list(item_list, value, key, remove=False) ¤
Get first item from list if present based on key matching value with option to remove it from the list.
Source code in trestle/common/list_utils.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
is_ordered_sublist(needle, haystack) ¤
Determine if needle is exactly contained in haystack.
The needle list comprises an ordered list of strings. The haystack list comprises an ordered list of strings that is to be searched. If the strings in the needle appear in the haystack in that exact order then return true, else false.
Examples: needle=['a','b','c'], haystack=['x','y','a','b','c','z'], result = True needle=['a','b','c'], haystack=['x','y','a','b','z','c'], result = False
Source code in trestle/common/list_utils.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
join_key_to_list_dicts(dict1, dict2) ¤
Join two dicts of str to List.
Source code in trestle/common/list_utils.py
107 108 109 110 111 112 113 | |
merge_dicts(dest, src) ¤
Merge the two dicts with priority to src.
Source code in trestle/common/list_utils.py
152 153 154 | |
none_if_empty(list_) ¤
Convert to None if empty list, preserving None input.
This is the complement to as_list() for Pydantic v2 compatibility. Use after building a list to ensure empty lists become None for fields with min_length constraints.
Pattern for Pydantic v2 field assignment
temp_list = as_list(obj.field) # Get list ([] if None) temp_list.append(item) # Modify obj.field = none_if_empty(temp_list) # Assign (None if empty)
Source code in trestle/common/list_utils.py
69 70 71 72 73 74 75 76 77 78 79 80 | |
pop_item_from_list(item_list, value, key) ¤
Pop first matching item from a list if it is present based on the key matching the value.
Source code in trestle/common/list_utils.py
141 142 143 | |
set_or_pop(dic, key, value) ¤
Set if value is non-empty list or not None otherwise remove.
Source code in trestle/common/list_utils.py
207 208 209 210 211 212 | |
handler: python