Improved typing and linting

This commit is contained in:
Profitroll 2023-05-11 20:52:08 +02:00
parent 4bca6d97d4
commit 8a6cda55fe
2 changed files with 110 additions and 4 deletions

View File

@ -6,17 +6,62 @@ from ujson import dumps, loads
async def json_read(path: Union[str, Path]) -> Any: async def json_read(path: Union[str, Path]) -> Any:
"""Read contents of a JSON file
### Args:
* path (`Union[str, Path]`): Path-like object or path as a string
### Returns:
* `Any`: File contents
"""
async with aiofiles.open(str(path), mode="r", encoding="utf-8") as f: async with aiofiles.open(str(path), mode="r", encoding="utf-8") as f:
data = await f.read() data = await f.read()
return loads(data) return loads(data)
async def json_write(data: Any, path: Union[str, Path]) -> None: async def json_write(data: Any, path: Union[str, Path]) -> None:
"""Write contents to a JSON file
### Args:
* data (`Any`): Contents to write. Must be a JSON serializable
* path (`Union[str, Path]`): Path-like object or path as a string of a destination
"""
async with aiofiles.open(str(path), mode="w", encoding="utf-8") as f: async with aiofiles.open(str(path), mode="w", encoding="utf-8") as f:
await f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False)) await f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False))
async def config_get(key: str, *path: str, config_file: str = "config.json") -> Any: async def config_get(
key: str, *path: str, config_file: Union[str, Path] = "config.json"
) -> Any:
"""Get a value of the config key by its path provided
For example, `foo.bar.key` has a path of `"foo", "bar"` and the key `"key"`
### Args:
* key (`str`): Key that contains the value
* *path (`str`): Path to the key that contains the value
* config_file (`Union[str, Path]`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
### Returns:
* `Any`: Key's value
### Example:
Get the "salary" of "Pete" from this JSON structure:
```json
{
"users": {
"Pete": {
"salary": 10.0
}
}
}
```
This can be easily done with the following code:
```python
import libbot
salary = await libbot.config_get("salary", "users", "Pete")
```
"""
this_key = await json_read(config_file) this_key = await json_read(config_file)
for dict_key in path: for dict_key in path:
this_key = this_key[dict_key] this_key = this_key[dict_key]
@ -24,8 +69,16 @@ async def config_get(key: str, *path: str, config_file: str = "config.json") ->
async def config_set( async def config_set(
key: str, value: Any, *path: str, config_file: str = "config.json" key: str, value: Any, *path: str, config_file: Union[str, Path] = "config.json"
) -> None: ) -> None:
"""Set config's key by its path to the value
### Args:
* key (`str`): Key that leads to the value
* value (`Any`): Any JSON serializable data
* *path (`str`): Path to the key of the target
* config_file (`Union[str, Path]`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
"""
this_dict = await json_read(config_file) this_dict = await json_read(config_file)
string = "this_dict" string = "this_dict"
for arg in path: for arg in path:

View File

@ -5,17 +5,62 @@ from ujson import dumps, loads
def json_read(path: Union[str, Path]) -> Any: def json_read(path: Union[str, Path]) -> Any:
"""Read contents of a JSON file
### Args:
* path (`Union[str, Path]`): Path-like object or path as a string
### Returns:
* `Any`: File contents
"""
with open(str(path), mode="r", encoding="utf-8") as f: with open(str(path), mode="r", encoding="utf-8") as f:
data = f.read() data = f.read()
return loads(data) return loads(data)
def json_write(data: Any, path: Union[str, Path]) -> None: def json_write(data: Any, path: Union[str, Path]) -> None:
"""Write contents to a JSON file
### Args:
* data (`Any`): Contents to write. Must be a JSON serializable
* path (`Union[str, Path]`): Path-like object or path as a string of a destination
"""
with open(str(path), mode="w", encoding="utf-8") as f: with open(str(path), mode="w", encoding="utf-8") as f:
f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4)) f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4))
def config_get(key: str, *path: str, config_file: str = "config.json") -> Any: def config_get(
key: str, *path: str, config_file: Union[str, Path] = "config.json"
) -> Any:
"""Get a value of the config key by its path provided
For example, `foo.bar.key` has a path of `"foo", "bar"` and the key `"key"`
### Args:
* key (`str`): Key that contains the value
* *path (`str`): Path to the key that contains the value
* config_file (`Union[str, Path]`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
### Returns:
* `Any`: Key's value
### Example:
Get the "salary" of "Pete" from this JSON structure:
```json
{
"users": {
"Pete": {
"salary": 10.0
}
}
}
```
This can be easily done with the following code:
```python
import libbot
salary = libbot.sync.config_get("salary", "users", "Pete")
```
"""
this_key = json_read(config_file) this_key = json_read(config_file)
for dict_key in path: for dict_key in path:
this_key = this_key[dict_key] this_key = this_key[dict_key]
@ -23,8 +68,16 @@ def config_get(key: str, *path: str, config_file: str = "config.json") -> Any:
def config_set( def config_set(
key: str, value: Any, *path: str, config_file: str = "config.json" key: str, value: Any, *path: str, config_file: Union[str, Path] = "config.json"
) -> None: ) -> None:
"""Set config's key by its path to the value
### Args:
* key (`str`): Key that leads to the value
* value (`Any`): Any JSON serializable data
* *path (`str`): Path to the key of the target
* config_file (`Union[str, Path]`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"`
"""
this_dict = json_read(config_file) this_dict = json_read(config_file)
string = "this_dict" string = "this_dict"
for arg in path: for arg in path: