Skip to content

Commit

Permalink
Add json.load and pickle.load
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya-mohammadi committed Sep 3, 2024
1 parent 46d02a4 commit 39afe8a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion deep_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .utils.lib_utils.integeration_utils import import_lazy_module

# Deep Utils version number
__version__ = "1.3.42"
__version__ = "1.3.43"

from .utils.constants import DUMMY_PATH, Backends

Expand Down
18 changes: 18 additions & 0 deletions deep_utils/utils/decorators/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import time
from functools import wraps
from typing import Callable
Expand All @@ -24,6 +25,23 @@ def wrapper(self, *args, **kwargs):
return wrapper


def method_deprecated(method, version: str = "") -> Callable:
"""
Add a deprecated warning
:param method: The method that will be measured
:param version: In which version it will be deprecated
:return:
"""

@wraps(method)
def wrapper(self, *args, **kwargs):
logging.warning(f"This method: {method.__name__} is deprecated. {f'This will be removed in version: {version}!' if version else 'This will be removed in the upcoming releases!'}")
results = method(self, *args, **kwargs)
return results

return wrapper


def get_func_time(func) -> Callable:
"""
Gets the elapsed time of a class
Expand Down
10 changes: 8 additions & 2 deletions deep_utils/utils/json_utils/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from typing import Union
from pathlib import Path

from deep_utils.utils.decorators.main import method_deprecated


class JsonUtils:
@staticmethod
def load_json(json_path: Union[str, Path], encoding="utf-8") -> dict:
def load(json_path: Union[str, Path], encoding="utf-8") -> dict:
"""
loads a json file
:param json_path: Path to json file to load
Expand All @@ -18,7 +20,7 @@ def load_json(json_path: Union[str, Path], encoding="utf-8") -> dict:
return json_data

@staticmethod
def dump_json(
def dump(
json_path: Union[str, Path], json_object: Union[list, dict], encoding="utf-8", ensure_ascii=True
) -> None:
"""
Expand All @@ -35,3 +37,7 @@ def dump_json(

with open(json_path, mode="w", encoding=encoding) as f:
json.dump(json_object, f, ensure_ascii=ensure_ascii)


JsonUtils.load_json = method_deprecated(JsonUtils.load)
JsonUtils.dump_json = method_deprecated(JsonUtils.dump)
10 changes: 8 additions & 2 deletions deep_utils/utils/pickle_utils/pickle_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import pickle

from deep_utils.utils.decorators.main import method_deprecated


class PickleUtils:

@staticmethod
def dump_pickle(file_path: str, file, mode: str = "wb"):
def dump(file_path: str, file, mode: str = "wb"):
with open(file_path, mode=mode) as f:
pickle.dump(file, f)

@staticmethod
def load_pickle(file_path: str, mode: str = "rb", encoding=""):
def load(file_path: str, mode: str = "rb", encoding=""):
with open(file_path, mode=mode) as f:
return pickle.load(f, encoding=encoding)


PickleUtils.load_pickle = method_deprecated(PickleUtils.load)
PickleUtils.dump_pickle = method_deprecated(PickleUtils.dump)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import setuptools

VERSION = "1.3.42"
VERSION = "1.3.43"

long_description = open("Readme.md", mode="r", encoding="utf-8").read()

Expand Down

0 comments on commit 39afe8a

Please sign in to comment.