From 47374152cc4e97496fe80b032d3d6b66a2ade255 Mon Sep 17 00:00:00 2001 From: validcube Date: Mon, 20 Nov 2023 21:36:49 +0700 Subject: [PATCH] refactor: remove lib in ignore list --- .gitignore | 2 +- lib/__init__.py | 2 ++ lib/cleanup.py | 28 ++++++++++++++++++++++++++++ lib/formatter.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lib/__init__.py create mode 100644 lib/cleanup.py create mode 100644 lib/formatter.py diff --git a/.gitignore b/.gitignore index e00bbc1..38eb99b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,7 +18,7 @@ dist/ downloads/ eggs/ .eggs/ -lib/ +# lib/ # We use this lib64/ parts/ sdist/ diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..7168f5c --- /dev/null +++ b/lib/__init__.py @@ -0,0 +1,2 @@ +from .cleanup import remove_empty # noqa: F401 +# from .formatter import format_json # noqa: F401 diff --git a/lib/cleanup.py b/lib/cleanup.py new file mode 100644 index 0000000..486accf --- /dev/null +++ b/lib/cleanup.py @@ -0,0 +1,28 @@ +"""Recursively remove empty items from nested objects (dictionaries and lists) + +⚙️ Arg: + data(dict): JSON data + +↪️ Return: + str: JSON data without empty keys, list, dict, and array +""" + +from typing import Any, Dict + + +def remove_empty(data: Dict[Any, Any]) -> Dict[Any, Any]: + """Recursively remove empty items from nested objects (dictionaries and lists) + + ⚙️ Arg: + data(dict): JSON data + + ↪️ Return: + str: JSON data without empty keys, list, dict, and array + """ + + if isinstance(data, dict): + return {k: remove_empty(v) for k, v in data.items() if v is not None and v != "" and remove_empty(v) != {}} + elif isinstance(data, list): + return [remove_empty(v) for v in data if v is not None and v != "" and remove_empty(v) != []] + else: + return data diff --git a/lib/formatter.py b/lib/formatter.py new file mode 100644 index 0000000..9c86e89 --- /dev/null +++ b/lib/formatter.py @@ -0,0 +1,28 @@ +#"""Format JSON data to be more readable +# +#⚙️ Arg: +# data(dict): JSON data +# indent(int): Indentation level +# +#↪️ Return: +# str: Formatted JSON data with 4 indents +#""" +# +#import json +# +# +#def format_json( +# data: dict, +# indent: int = 2, +# ) -> dict: +# """Format JSON data to be more readable +# +# ⚙️ Arg: +# data(dict): JSON data +# indent(int): Indentation level +# +# ↪️ Return: +# str: Formatted JSON data with specified indentation +# """ +# +# return json.dumps(data, indent=indent, ensure_ascii=False)