Skip to content

Commit

Permalink
new recursive_valmap function
Browse files Browse the repository at this point in the history
  • Loading branch information
RichieHakim committed May 6, 2024
1 parent c0156c4 commit 88ef4c0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions bnpm/container_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import Dict, Any, Optional, Union, List, Tuple, Callable, Iterable, Iterator, Type
import itertools

import toolz

"""
This module is intended to have minimal dependencies.
It is called by server.py which is intended to run
Expand Down Expand Up @@ -475,6 +477,46 @@ def make_serializable_dict(obj, depth=0, max_depth=100, name=None):
return serializable_dict


def recursive_valmap(
func: Callable[[Any], Any],
d: Dict,
depth: int = -1,
):
"""
Apply a function to all values in a nested dictionary. \n
If item is a dictionary, then apply the function to all values in the
dictionary. \n
Uses toolz.dicttoolz.valmap. \n
RH 2024
Args:
func (Callable):
Function to apply to all values.
d (Dict):
Dictionary to apply the function to.
factory (Type[Dict]):
Type of dictionary to use for the output.
depth (int):
Maximum depth to apply the function.
If -1, then apply to all depths.
If 1, then apply to the top level only.
If 0, then return the dictionary as is.
Returns:
output (Dict):
Dictionary with the function applied to all values.
"""
def helper_recursive_valmap(func, d, depth):
if depth == 0:
return d
return toolz.valmap(
lambda x: helper_recursive_valmap(func, x, depth-1) if isinstance(x, dict) else func(x),
d
)

return helper_recursive_valmap(func, d, depth)


############################################################
################# PARAMETER DICTIONARIES ###################
############################################################
Expand Down

0 comments on commit 88ef4c0

Please sign in to comment.