v4.1.0 #189
@ -20,7 +20,7 @@ def config_get(key: str, *path: str, config_file: str | Path = DEFAULT_CONFIG_LO
|
||||
|
||||
### Args:
|
||||
* key (`str`): Key that contains the value
|
||||
* *path (`str`): Path to the key that contains the value
|
||||
* *path (`str`): Path to the key that contains the value (pass *[] or don't pass anything at all to get on the top/root level)
|
||||
* config_file (`str | Path`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
|
||||
|
||||
### Returns:
|
||||
@ -59,7 +59,7 @@ async def config_get(key: str, *path: str, config_file: str | Path = DEFAULT_CON
|
||||
|
||||
### Args:
|
||||
* key (`str`): Key that contains the value
|
||||
* *path (`str`): Path to the key that contains the value
|
||||
* *path (`str`): Path to the key that contains the value (pass *[] or don't pass anything at all to get on the top/root level)
|
||||
* config_file (`str | Path`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
|
||||
|
||||
### Returns:
|
||||
@ -98,7 +98,7 @@ def config_set(key: str, value: Any, *path: str, config_file: str | Path = DEFAU
|
||||
### Args:
|
||||
* key (`str`): Key that leads to the value
|
||||
* value (`Any`): Any JSON serializable data
|
||||
* *path (`str`): Path to the key of the target
|
||||
* *path (`str`): Path to the key of the target (pass *[] or don't pass anything at all to set on the top/root level)
|
||||
* config_file (`str | Path`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
|
||||
|
||||
### Raises:
|
||||
@ -116,7 +116,7 @@ async def config_set(
|
||||
### Args:
|
||||
* key (`str`): Key that leads to the value
|
||||
* value (`Any`): Any JSON serializable data
|
||||
* *path (`str`): Path to the key of the target
|
||||
* *path (`str`): Path to the key of the target (pass *[] or don't pass anything at all to set on the top/root level)
|
||||
* config_file (`str | Path`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
|
||||
|
||||
### Raises:
|
||||
@ -136,7 +136,7 @@ def config_delete(
|
||||
|
||||
### Args:
|
||||
* key (`str`): Key to delete
|
||||
* *path (`str`): Path to the key of the target
|
||||
* *path (`str`): Path to the key of the target (pass *[] or don't pass anything at all to delete on the top/root level)
|
||||
* missing_ok (`bool`): Do not raise an exception if the key is missing. Defaults to `False`
|
||||
* config_file (`str | Path`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
|
||||
|
||||
@ -165,7 +165,7 @@ async def config_delete(
|
||||
|
||||
### Args:
|
||||
* key (`str`): Key to delete
|
||||
* *path (`str`): Path to the key of the target
|
||||
* *path (`str`): Path to the key of the target (pass *[] or don't pass anything at all to delete on the top/root level)
|
||||
* missing_ok (`bool`): Do not raise an exception if the key is missing. Defaults to `False`
|
||||
* config_file (`str | Path`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
|
||||
|
||||
|
@ -3,11 +3,11 @@ from typing import Any, Dict
|
||||
from typing import Callable
|
||||
|
||||
|
||||
def supports_argument(func: Callable, arg_name: str) -> bool:
|
||||
def supports_argument(func: Callable[..., Any], arg_name: str) -> bool:
|
||||
"""Check whether a function has a specific argument
|
||||
|
||||
### Args:
|
||||
* func (`Callable`): Function to be inspected
|
||||
* func (`Callable[..., Any]`): Function to be inspected
|
||||
* arg_name (`str`): Argument to be checked
|
||||
|
||||
### Returns:
|
||||
@ -24,11 +24,13 @@ def supports_argument(func: Callable, arg_name: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def nested_set(target: dict, value: Any, *path: str, create_missing=True) -> Dict[str, Any]:
|
||||
def nested_set(
|
||||
target: Dict[str, Any], value: Any, *path: str, create_missing: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""Set the key by its path to the value
|
||||
|
||||
### Args:
|
||||
* target (`dict`): Dictionary to perform modifications on
|
||||
* target (`Dict[str, Any]`): Dictionary to perform modifications on
|
||||
* value (`Any`): Any data
|
||||
* *path (`str`): Path to the key of the target
|
||||
* create_missing (`bool`, *optional*): Create keys on the way if they're missing. Defaults to `True`
|
||||
@ -39,29 +41,29 @@ def nested_set(target: dict, value: Any, *path: str, create_missing=True) -> Dic
|
||||
### Returns:
|
||||
* `Dict[str, Any]`: Changed dictionary
|
||||
"""
|
||||
d = target
|
||||
target_copy: Dict[str, Any] = target
|
||||
|
||||
for key in path[:-1]:
|
||||
if key in d:
|
||||
d = d[key]
|
||||
if key in target_copy:
|
||||
target_copy = target_copy[key]
|
||||
elif create_missing:
|
||||
d = d.setdefault(key, {})
|
||||
target_copy = target_copy.setdefault(key, {})
|
||||
else:
|
||||
raise KeyError(
|
||||
f"Key '{key}' is not found under path provided ({path}) and create_missing is False"
|
||||
)
|
||||
|
||||
if path[-1] in d or create_missing:
|
||||
d[path[-1]] = value
|
||||
if path[-1] in target_copy or create_missing:
|
||||
target_copy[path[-1]] = value
|
||||
|
||||
return target
|
||||
|
||||
|
||||
def nested_delete(target: dict, *path: str) -> Dict[str, Any]:
|
||||
def nested_delete(target: Dict[str, Any], *path: str) -> Dict[str, Any]:
|
||||
"""Delete the key by its path
|
||||
|
||||
### Args:
|
||||
* target (`dict`): Dictionary to perform modifications on
|
||||
* target (`Dict[str, Any]`): Dictionary to perform modifications on
|
||||
|
||||
### Raises:
|
||||
* `KeyError`: Key is not found under path provided
|
||||
@ -69,16 +71,16 @@ def nested_delete(target: dict, *path: str) -> Dict[str, Any]:
|
||||
### Returns:
|
||||
`Dict[str, Any]`: Changed dictionary
|
||||
"""
|
||||
d = target
|
||||
target_copy: Dict[str, Any] = target
|
||||
|
||||
for key in path[:-1]:
|
||||
if key in d:
|
||||
d = d[key]
|
||||
if key in target_copy:
|
||||
target_copy = target_copy[key]
|
||||
else:
|
||||
raise KeyError(f"Key '{key}' is not found under path provided ({path})")
|
||||
|
||||
if path[-1] in d:
|
||||
del d[path[-1]]
|
||||
if path[-1] in target_copy:
|
||||
del target_copy[path[-1]]
|
||||
else:
|
||||
raise KeyError(f"Key '{path[-1]}' is not found under path provided ({path})")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user