mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Add a new utils.algorithms file for code helpers.
The first helper, recursive_apply, goes through recursive dict/lists to apply a function on non-recursable types
This commit is contained in:
parent
38db30cd70
commit
dc8f51e2f8
1 changed files with 13 additions and 0 deletions
13
src/utils/algorithms.py
Normal file
13
src/utils/algorithms.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from typing import Any, Callable
|
||||||
|
|
||||||
|
|
||||||
|
def recursive_apply(function: Callable, data: Any) -> Any:
|
||||||
|
if isinstance(data, dict): # FIXME: hashable?
|
||||||
|
return {key: recursive_apply(value, function) for key, value in data.items()}
|
||||||
|
|
||||||
|
if isinstance(data, list): # FIXME: iterable?
|
||||||
|
return [recursive_apply(value, function) for value in data]
|
||||||
|
|
||||||
|
return function(data)
|
Loading…
Add table
Reference in a new issue