File tree Expand file tree Collapse file tree 1 file changed +38
-1
lines changed
Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -180,7 +180,44 @@ def extract_keys(
180180
181181 return acc
182182
183-
183+
184+ def filter_keys (
185+ tree : dict ,
186+ include_keys : Optional [list [str ]] = None ,
187+ exclude_keys : Optional [list [str ]] = None ,
188+ include_keys_startswith : Optional [str ] = None
189+ ) -> dict :
190+ """
191+ Returns a copy of 'tree' that has had some of its top-level
192+ keys removed based on the applied filters.
193+
194+ Filters:
195+
196+ - include_keys: Provide a list of keys to include
197+ - exclude_keys: Provide a list of keys to exclude
198+ - include_keys_startswith: Provide a substring that
199+ occurs at the start of the keys. All matches will
200+ be included.
201+
202+ These filters are additive and are applied in the following
203+ order:
204+
205+ include_keys
206+ include_keys_startswith
207+ exclude_keys
208+ """
209+ include_keys = include_keys or []
210+ exclude_keys = exclude_keys or []
211+ filtered_keys = []
212+ for key in tree .keys ():
213+ if key in exclude_keys : continue
214+ if key .startswith (include_keys_startswith ):
215+ filtered_keys .append (key )
216+ elif key in include_keys :
217+ filtered_keys .append (key )
218+ filtered_tree = {k : v for k , v in filtered_tree .items () if k in filtered_keys }
219+ return filtered_tree
220+
184221
185222def merge_trees (trees : list [dict [str , dict ]]) -> dict [str , dict ]:
186223 """
You can’t perform that action at this time.
0 commit comments