Skip to content

Commit

Permalink
refactor: use common to_camel_case function (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolaas authored Sep 14, 2023
1 parent c45a7ac commit f5f0c2f
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ecalc/libraries/libecalc/common/libecalc/dto/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

This file was deleted.

0 comments on commit f5f0c2f

Please sign in to comment.