From 19570140a1c80627187778c22894e5a0b50c4bb5 Mon Sep 17 00:00:00 2001 From: "M. A. Reza" Date: Wed, 27 Aug 2025 17:25:00 +0330 Subject: [PATCH] new changes --- common_utils.py | 18 ------- pyproject.toml | 1 + utils/base_main.py | 7 +++ utils/common_utils.py | 9 ++++ utils/csv_utils.py | 50 +++++++++++++++++++ excel_utls.py => utils/excel_utls.py | 0 .../python_bash_utils.py | 0 .../read_wirte_file_utils.py | 0 utils/sequence_utils.py | 6 +++ utils/string_utils.py | 4 ++ uv.lock | 15 +++++- 11 files changed, 91 insertions(+), 19 deletions(-) delete mode 100644 common_utils.py create mode 100644 utils/base_main.py create mode 100644 utils/common_utils.py create mode 100644 utils/csv_utils.py rename excel_utls.py => utils/excel_utls.py (100%) rename python_bash_utils.py => utils/python_bash_utils.py (100%) rename read_wirte_file_utils.py => utils/read_wirte_file_utils.py (100%) create mode 100644 utils/sequence_utils.py create mode 100644 utils/string_utils.py diff --git a/common_utils.py b/common_utils.py deleted file mode 100644 index 20cca1d..0000000 --- a/common_utils.py +++ /dev/null @@ -1,18 +0,0 @@ -import sys -from pathlib import Path - -BASE_DIR = Path(__file__).resolve().parent - - -def boolean_to_emooji(bool_val, primary_emooji: bool): - if primary_emooji: - return "✅" if bool_val else "❌" - return "☑️" if bool_val else "✖️" - - -def confirm_exit_terminal(): - user_input = input('\n\nType "q" to exit.\n') - if user_input != "q": - return confirm_exit_terminal() - else: - sys.exit() diff --git a/pyproject.toml b/pyproject.toml index c676b25..b06a6db 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,4 +6,5 @@ readme = "README.md" requires-python = ">=3.11" dependencies = [ "openpyxl>=3.1.5", + "python-dotenv>=1.1.1", ] diff --git a/utils/base_main.py b/utils/base_main.py new file mode 100644 index 0000000..2aebafb --- /dev/null +++ b/utils/base_main.py @@ -0,0 +1,7 @@ +from pathlib import Path + +from dotenv import load_dotenv + +load_dotenv() + +BASE_DIR = Path(__file__).resolve().parent diff --git a/utils/common_utils.py b/utils/common_utils.py new file mode 100644 index 0000000..eb17425 --- /dev/null +++ b/utils/common_utils.py @@ -0,0 +1,9 @@ +import sys + + +def confirm_exit_terminal(): + user_input = input('\n\nType "q" to exit.\n') + if user_input != "q": + return confirm_exit_terminal() + else: + sys.exit() diff --git a/utils/csv_utils.py b/utils/csv_utils.py new file mode 100644 index 0000000..87aed6f --- /dev/null +++ b/utils/csv_utils.py @@ -0,0 +1,50 @@ +import csv +from pathlib import Path + + +def read_csv_file(file_path, parse_to_dict: bool = False): + with open(file_path, "r", newline="") as csvfile: + if parse_to_dict: + return list(csv.DictReader(csvfile)) + else: + return list(csv.reader(csvfile)) + + +def write_to_csv_file(rows, file_path: Path, rows_type, fieldnames=None): + file_path.parent.mkdir(parents=True, exist_ok=True) + with open(file_path, mode="w", newline="") as csvfile: + if rows_type is list: + csv_writer = csv.writer(csvfile) + return csv_writer.writerows(rows) + elif rows_type is dict: + csv_writer = csv.DictWriter(csvfile, fieldnames=fieldnames) + csv_writer.writeheader() + csv_writer.writerows(rows) + else: + raise Exception("rows_type value is invalid!") + + +def append_to_csv_file(rows, file_path: Path): + file_path.parent.mkdir(parents=True, exist_ok=True) + with open(file_path, mode="a", newline="") as csvfile: + csv_writer = csv.writer(csvfile) + return csv_writer.writerows(rows) + + +def convert_config_to_key_val_dict(config: list): + res = dict() + for row in config: + match row["value_type"]: + case "int": + value = int(row["value"]) + case _: + raise Exception("Invalid value_type.") + res[row["key"]] = value + return res + + +def convert_config_dict_to_list_of_key_values(config: dict): + result = list() + for k, v in config.items(): + result.append({"key": k, "value": v, "value_type": type(v).__name__}) + return result diff --git a/excel_utls.py b/utils/excel_utls.py similarity index 100% rename from excel_utls.py rename to utils/excel_utls.py diff --git a/python_bash_utils.py b/utils/python_bash_utils.py similarity index 100% rename from python_bash_utils.py rename to utils/python_bash_utils.py diff --git a/read_wirte_file_utils.py b/utils/read_wirte_file_utils.py similarity index 100% rename from read_wirte_file_utils.py rename to utils/read_wirte_file_utils.py diff --git a/utils/sequence_utils.py b/utils/sequence_utils.py new file mode 100644 index 0000000..10cf899 --- /dev/null +++ b/utils/sequence_utils.py @@ -0,0 +1,6 @@ +import functools +import operator + + +def flatten_list(list_var): + return functools.reduce(operator.iconcat, list_var, []) diff --git a/utils/string_utils.py b/utils/string_utils.py new file mode 100644 index 0000000..9581350 --- /dev/null +++ b/utils/string_utils.py @@ -0,0 +1,4 @@ +def boolean_to_emooji(bool_val, primary_emooji: bool): + if primary_emooji: + return "✅" if bool_val else "❌" + return "☑️" if bool_val else "✖️" diff --git a/uv.lock b/uv.lock index ec63c89..1c14626 100644 --- a/uv.lock +++ b/uv.lock @@ -23,13 +23,26 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/c0/da/977ded879c29cbd04de313843e76868e6e13408a94ed6b987245dc7c8506/openpyxl-3.1.5-py2.py3-none-any.whl", hash = "sha256:5282c12b107bffeef825f4617dc029afaf41d0ea60823bbb665ef3079dc79de2", size = 250910, upload-time = "2024-06-28T14:03:41.161Z" }, ] +[[package]] +name = "python-dotenv" +version = "1.1.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f6/b0/4bc07ccd3572a2f9df7e6782f52b0c6c90dcbb803ac4a167702d7d0dfe1e/python_dotenv-1.1.1.tar.gz", hash = "sha256:a8a6399716257f45be6a007360200409fce5cda2661e3dec71d23dc15f6189ab", size = 41978, upload-time = "2025-06-24T04:21:07.341Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5f/ed/539768cf28c661b5b068d66d96a2f155c4971a5d55684a514c1a0e0dec2f/python_dotenv-1.1.1-py3-none-any.whl", hash = "sha256:31f23644fe2602f88ff55e1f5c79ba497e01224ee7737937930c448e4d0e24dc", size = 20556, upload-time = "2025-06-24T04:21:06.073Z" }, +] + [[package]] name = "python-utils" version = "0.1.0" source = { virtual = "." } dependencies = [ { name = "openpyxl" }, + { name = "python-dotenv" }, ] [package.metadata] -requires-dist = [{ name = "openpyxl", specifier = ">=3.1.5" }] +requires-dist = [ + { name = "openpyxl", specifier = ">=3.1.5" }, + { name = "python-dotenv", specifier = ">=1.1.1" }, +]