Skip to content

Instantly share code, notes, and snippets.

@sakku116
Last active March 9, 2023 02:23
Show Gist options
  • Save sakku116/11dd8dab9da09ecd502a6c6ee7b8e381 to your computer and use it in GitHub Desktop.
Save sakku116/11dd8dab9da09ecd502a6c6ee7b8e381 to your computer and use it in GitHub Desktop.
def generatePathsFromDictBasedPaths(d: dict, **kwargs):
"""
the point is just adding prefix to each key,
then call self function to proccess nested dict.
example:
```
{
"dashboard": {},
"help": {},
"others": {
"blog": {}
}
}
```
return <generator>.
just convert `list(<generator>)`
result example:
```
['/dashboard',
'/helo',
'/others',
'/others/blog']
```
"""
prefix = kwargs.get("prefix", "")
for key, value in d.items():
if not prefix:
result = f"/{key}"
else:
result = f"{prefix}/{key}"
yield result
if len(value) != 0:
yield from generatePathsFromDictBasedPaths(value, prefix=result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment