Skip to content

Commit

Permalink
Add new utils
Browse files Browse the repository at this point in the history
  • Loading branch information
selfkilla666 committed Dec 21, 2023
1 parent ef828c9 commit eef4cc3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
33 changes: 33 additions & 0 deletions crasher/utils/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Code by @selfkilla666
# https://github.com/witch-software/crasher
# MIT License

from __future__ import annotations

from typing import Any
from pathlib import Path

from json import load, dump


def load_json_file(path: Path) -> dict[Any, Any]:
"""
Load dictionary from the JSON file.
:param path: Path to JSON file.
:return: Dictionary loaded from JSON file.
"""

with open(path, 'r') as file:
return load(file)

def save_json_file(dictionary: dict[Any, Any], path: Path) -> None:
"""
Save dictionary to the JSON file.
:param dictionary: Dictionary to be saved.
:param path: Path to JSON file.
"""

with open(path, 'w') as file:
dump(dictionary, file, indent=4)
49 changes: 49 additions & 0 deletions crasher/utils/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Code by @selfkilla666
# https://github.com/witch-software/crasher
# MIT License

from __future__ import annotations

from typing import Any


def are_keys_present(dictionary_to_check: dict[Any, Any], dictionary_pattern: dict[Any, Any]) -> bool:
"""
Check if all keys and their corresponding values in the `dictionary_pattern` are present
in the `dictionary_to_check`. If a value in `dictionary_pattern` is not `None`, the function
recursively checks the presence of keys and values in nested dictionaries.
:param dictionary_to_check: Dictionary to check for the presence of keys and values.
:param dictionary_pattern: Dictionary containing keys and values to be checked for presence.
:return: True if all keys and values (if not None) in `dictionary_pattern` are present
in `dictionary_to_check`, False otherwise.
"""

for key, value in dictionary_pattern.items():
if key not in dictionary_to_check or not are_keys_present(dictionary_to_check[key], value):
return False

return True

def add_missing_values(dictionary_to_check: dict[Any, Any], dictionary_pattern: dict[Any, Any]) -> dict[Any, Any]:
"""
Add missing keys and their corresponding values from `dictionary_pattern` to
`dictionary_to_check`. If a key is already present in `dictionary_to_check`, and both the
value in `dictionary_pattern` and the corresponding value in `dictionary_to_check` are
dictionaries, the function recursively adds missing keys in the nested dictionaries.
:param dictionary_to_check: Dictionary to which missing keys and values will be added.
:param dictionary_pattern: Dictionary containing keys and values to be added if missing.
:return: A new dictionary containing all keys and values from `dictionary_to_check`,
with missing keys and values added from `dictionary_pattern`
"""

result = dictionary_to_check.copy()

for key, value in dictionary_pattern.items():
if key not in result:
result[key] = value
elif isinstance(value, dict) and isinstance(result[key], dict):
result[key] = add_missing_values(result[key], value)

return result

0 comments on commit eef4cc3

Please sign in to comment.