Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGC-2079 Logging level to base converter #219

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/power_grid_model_io/converters/base_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Abstract converter class
"""
import logging
from abc import ABC, abstractmethod
from typing import Generic, Optional, Tuple, TypeVar

Expand All @@ -20,11 +21,18 @@
class BaseConverter(Generic[T], ABC):
"""Abstract converter class"""

def __init__(self, source: Optional[BaseDataStore[T]] = None, destination: Optional[BaseDataStore[T]] = None):
def __init__(
self,
source: Optional[BaseDataStore[T]] = None,
destination: Optional[BaseDataStore[T]] = None,
log_level: int = logging.DEBUG,
):
"""
Initialize a logger
"""
self._log = structlog.get_logger(type(self).__name__)
self._logger = logging.getLogger(type(self).__name__)
self._logger.setLevel(log_level)
self._log = structlog.wrap_logger(self._logger, wrapper_class=structlog.make_filtering_bound_logger(log_level))
self._source = source
self._destination = destination
self._auto_id = AutoID()
Expand Down Expand Up @@ -146,6 +154,25 @@ def save(
else:
raise ValueError("No destination supplied!")

def set_log_level(self, log_level: int) -> None:
"""
Set the log level

Args:
log_level: int:
"""
self._logger.setLevel(log_level)
self._log = structlog.wrap_logger(self._logger, wrapper_class=structlog.make_filtering_bound_logger(log_level))

def get_log_level(self) -> int:
"""
Get the log level

Returns:
int:
"""
return self._logger.getEffectiveLevel()

def _load_data(self, data: Optional[T]) -> T:
if data is not None:
return data
Expand Down
2 changes: 1 addition & 1 deletion tests/data/pandapower/pgm_asym_output_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
[
{"energized": 1, "i_from": [12.266058718690484, 12.266058718690402, 24.17889769909201], "i_to": [66.02947091322386, 66.02947091322693, 136.6556434817352], "id": 9, "loading": 0.03883052565830863, "p_from": [765820.724989093, 765820.7249890427, -1529485.491438911], "p_to": [-730552.3008242727, -730552.300824217, 1567616.5274441042], "q_from": [-142683.60010968833, -142683.60010993064, -136520.3668874493], "q_to": [216633.7469512629, 216633.74695157827, 216173.52394822074], "s_from": [778999.3533778327, 778999.3533778277, 1535566.2405436018], "s_to": [761995.3048134763, 761995.3048135125, 1582451.4430377013], "id_reference": {"table": "trafo", "index": 0}, "pgm_input": {"from_node": 0, "to_node": 1}, "pp_input": {"df": 1.0}}
]
}
}
2 changes: 1 addition & 1 deletion tests/data/pandapower/pgm_output_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
[
{"energized": 1, "i_from": 20.545016973103277, "i_to": 62.76706374195551, "id": 12, "loading": 0.04892939318768632, "p_from": 1798665.9834270997, "p_to": -1765096.6771171836, "q_from": 3476628.8259518775, "q_to": -1169229.7700735242, "s_from": 3914351.4550149054, "s_to": 2117230.3924694424, "id_reference": {"table": "trafo", "index": 101}, "pgm_input": {"from_node": 0, "to_node": 1}, "pp_input": {"df": 1.0}}
]
}
}
15 changes: 15 additions & 0 deletions tests/unit/converters/test_base_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MPL-2.0

import logging
from typing import Dict, List
from unittest.mock import ANY, MagicMock

Expand All @@ -12,6 +13,9 @@


class DummyConverter(BaseConverter[Dict[str, List[Dict[str, int]]]]):
def __init__(self, source=None, destination=None, log_level=logging.ERROR):
super().__init__(source, destination, log_level)

def _parse_data(self, data, data_type, extra_info=None):
# No need to implement _parse_data() for testing purposes
pass
Expand Down Expand Up @@ -168,3 +172,14 @@ def test_load_data(converter: DummyConverter):
converter_2 = DummyConverter(source=source)
converter_2._load_data(data=None)
source.load.assert_called_once()


def test_base_converter_log_level():
converter = DummyConverter(log_level=logging.DEBUG)
assert converter.get_log_level() == logging.DEBUG

converter = DummyConverter()
assert converter.get_log_level() == logging.ERROR

converter.set_log_level(logging.DEBUG)
assert converter.get_log_level() == logging.DEBUG