diff --git a/src/ecalc/libraries/libecalc/common/libecalc/dto/utils/string_utils.py b/src/ecalc/libraries/libecalc/common/libecalc/common/string_utils.py similarity index 67% rename from src/ecalc/libraries/libecalc/common/libecalc/dto/utils/string_utils.py rename to src/ecalc/libraries/libecalc/common/libecalc/common/string_utils.py index b8d0f045f0..a91bda4a76 100644 --- a/src/ecalc/libraries/libecalc/common/libecalc/dto/utils/string_utils.py +++ b/src/ecalc/libraries/libecalc/common/libecalc/common/string_utils.py @@ -21,3 +21,17 @@ def generate_id(*args: str) -> str: """ full_string = "-".join(args) return hashlib.md5(full_string.encode()).hexdigest() # noqa: S324 - insecure hash for ids + + +def to_camel_case(string: str) -> str: + """Convert string from snake_case to camelCase + + Args: + string: String in snake_case format + + Returns: + String in camelCase format + + """ + string_split = string.replace("__", "_").split("_") + return string_split[0] + "".join(word[0].upper() + word[1:] for word in string_split[1:]) diff --git a/src/ecalc/libraries/libecalc/common/libecalc/core/models/results/base.py b/src/ecalc/libraries/libecalc/common/libecalc/core/models/results/base.py index a397039635..bf0625b050 100644 --- a/src/ecalc/libraries/libecalc/common/libecalc/core/models/results/base.py +++ b/src/ecalc/libraries/libecalc/common/libecalc/core/models/results/base.py @@ -7,9 +7,9 @@ import numpy as np from libecalc.common.logger import logger +from libecalc.common.string_utils import to_camel_case from libecalc.common.units import Unit from libecalc.dto import SingleSpeedChart, VariableSpeedChart -from libecalc.dto.utils.camelcase import to_camel_case from pydantic import BaseModel from pydantic.json import custom_pydantic_encoder diff --git a/src/ecalc/libraries/libecalc/common/libecalc/dto/base.py b/src/ecalc/libraries/libecalc/common/libecalc/dto/base.py index 75b296b323..c5e7a55d78 100644 --- a/src/ecalc/libraries/libecalc/common/libecalc/dto/base.py +++ b/src/ecalc/libraries/libecalc/common/libecalc/dto/base.py @@ -4,7 +4,7 @@ from functools import partial from typing import Optional -from libecalc.dto.utils.camelcase import to_camel_case +from libecalc.common.string_utils import to_camel_case from libecalc.expression import Expression from orjson import orjson from pydantic import BaseModel, Extra diff --git a/src/ecalc/libraries/libecalc/common/libecalc/dto/components.py b/src/ecalc/libraries/libecalc/common/libecalc/dto/components.py index 6ffd3b602f..13ad810747 100644 --- a/src/ecalc/libraries/libecalc/common/libecalc/dto/components.py +++ b/src/ecalc/libraries/libecalc/common/libecalc/dto/components.py @@ -4,6 +4,7 @@ import networkx as nx from libecalc import dto +from libecalc.common.string_utils import generate_id, get_duplicates from libecalc.common.temporal_model import TemporalExpression, TemporalModel from libecalc.common.units import Unit from libecalc.common.utils.rates import TimeSeriesFloat, TimeSeriesRate @@ -28,7 +29,6 @@ from libecalc.dto.models.compressor import CompressorModel from libecalc.dto.models.pump import PumpModel from libecalc.dto.types import ConsumptionType, EnergyUsageType, FuelType -from libecalc.dto.utils.string_utils import generate_id, get_duplicates from libecalc.dto.utils.validators import ( ComponentNameStr, EmissionNameStr, diff --git a/src/ecalc/libraries/libecalc/common/libecalc/dto/utils/camelcase.py b/src/ecalc/libraries/libecalc/common/libecalc/dto/utils/camelcase.py deleted file mode 100644 index 966050feab..0000000000 --- a/src/ecalc/libraries/libecalc/common/libecalc/dto/utils/camelcase.py +++ /dev/null @@ -1,3 +0,0 @@ -def to_camel_case(string: str) -> str: - string_split = string.replace("__", "_").split("_") - return string_split[0] + "".join(word[0].upper() + word[1:] for word in string_split[1:]) diff --git a/src/ecalc/libraries/libecalc/common/libecalc/input/mappers/create_references.py b/src/ecalc/libraries/libecalc/common/libecalc/input/mappers/create_references.py index d264ed9321..f9fdcd16c6 100644 --- a/src/ecalc/libraries/libecalc/common/libecalc/input/mappers/create_references.py +++ b/src/ecalc/libraries/libecalc/common/libecalc/input/mappers/create_references.py @@ -1,7 +1,7 @@ from typing import Dict, List from libecalc.common.logger import logger -from libecalc.dto.utils.string_utils import get_duplicates +from libecalc.common.string_utils import get_duplicates from libecalc.input.mappers.facility_input import FacilityInputMapper from libecalc.input.mappers.fuel_and_emission_mapper import FuelMapper from libecalc.input.mappers.model import ModelMapper diff --git a/src/ecalc/libraries/libecalc/common/libecalc/output/flow_diagram/fde_models.py b/src/ecalc/libraries/libecalc/common/libecalc/output/flow_diagram/fde_models.py index 5a13cbe711..aa932b09a7 100644 --- a/src/ecalc/libraries/libecalc/common/libecalc/output/flow_diagram/fde_models.py +++ b/src/ecalc/libraries/libecalc/common/libecalc/output/flow_diagram/fde_models.py @@ -3,6 +3,7 @@ from datetime import datetime from typing import List, Optional, Union +from libecalc.common.string_utils import to_camel_case from pydantic import BaseModel @@ -35,20 +36,6 @@ class FlowType: ELECTRICITY = "electricity-flow" -def to_camel_case(string: str) -> str: - """Convert string from snake_case to camelCase - - Args: - string: String in snake_case format - - Returns: - String in camelCase format - - """ - string_split = string.split("_") - return string_split[0] + "".join(word.capitalize() for word in string_split[1:]) - - class FlowDiagramBaseModel(BaseModel): """Pydantic basemodel for FDE pydantic models diff --git a/src/ecalc/libraries/libecalc/common/tests/common/test_string_utils.py b/src/ecalc/libraries/libecalc/common/tests/common/test_string_utils.py new file mode 100644 index 0000000000..2b8b1dd13f --- /dev/null +++ b/src/ecalc/libraries/libecalc/common/tests/common/test_string_utils.py @@ -0,0 +1,38 @@ +import pytest +from libecalc.common.string_utils import generate_id, get_duplicates, to_camel_case + + +class TestGetDuplicates: + def test_no_duplicates(self): + assert get_duplicates(["name1", "name2", "name3"]) == set() + + def test_duplicates(self): + assert get_duplicates(["name1", "name2", "name1"]) == {"name1"} + + +class TestGenerateId: + def test_single_string(self): + assert isinstance(generate_id("some_name"), str) + + def test_multiple_strings(self): + assert isinstance(generate_id("some_prefix", "some_type", "some_name"), str) + + +test_data = [ + # "snake_case, camel_case" + ("my_camel_case", "myCamelCase"), + ("m_c", "mC"), + ("m_C", "mC"), + ("M_C", "MC"), + # ("_C", "C"), + # ("_c", "C"), + # ("m_", "m"), + # ("M_", "M"), + ("my_cAmeLCase", "myCAmeLCase"), +] + + +class TestToCamelCase: + @pytest.mark.parametrize("snake_case, camel_case", test_data) + def test_to_camel_case(self, snake_case: str, camel_case: str): + assert to_camel_case(snake_case) == camel_case diff --git a/src/ecalc/libraries/libecalc/common/tests/dto/utils/test_camelcase.py b/src/ecalc/libraries/libecalc/common/tests/dto/utils/test_camelcase.py deleted file mode 100644 index f7f01e3540..0000000000 --- a/src/ecalc/libraries/libecalc/common/tests/dto/utils/test_camelcase.py +++ /dev/null @@ -1,20 +0,0 @@ -import pytest -from libecalc.dto.utils.camelcase import to_camel_case - -test_data = [ - # "snake_case, camel_case" - ("my_camel_case", "myCamelCase"), - ("m_c", "mC"), - ("m_C", "mC"), - ("M_C", "MC"), - # ("_C", "C"), - # ("_c", "C"), - # ("m_", "m"), - # ("M_", "M"), - ("my_cAmeLCase", "myCAmeLCase"), -] - - -@pytest.mark.parametrize("snake_case, camel_case", test_data) -def test_to_camel_case(snake_case: str, camel_case: str): - assert to_camel_case(snake_case) == camel_case diff --git a/src/ecalc/libraries/libecalc/common/tests/dto/utils/test_string_utils.py b/src/ecalc/libraries/libecalc/common/tests/dto/utils/test_string_utils.py deleted file mode 100644 index 7cf6719ca0..0000000000 --- a/src/ecalc/libraries/libecalc/common/tests/dto/utils/test_string_utils.py +++ /dev/null @@ -1,17 +0,0 @@ -from libecalc.dto.utils.string_utils import generate_id, get_duplicates - - -class TestGetDuplicates: - def test_no_duplicates(self): - assert get_duplicates(["name1", "name2", "name3"]) == set() - - def test_duplicates(self): - assert get_duplicates(["name1", "name2", "name1"]) == {"name1"} - - -class TestGenerateId: - def test_single_string(self): - assert isinstance(generate_id("some_name"), str) - - def test_multiple_strings(self): - assert isinstance(generate_id("some_prefix", "some_type", "some_name"), str)