Skip to content

Commit

Permalink
update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorb1 committed Aug 12, 2023
1 parent 35339e7 commit 8fb88d5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
19 changes: 12 additions & 7 deletions src/otoole/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

import pandas as pd

from otoole.exceptions import OtooleIndexError, OtooleConfigFileError
from otoole.exceptions import OtooleConfigFileError, OtooleIndexError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -556,7 +556,8 @@ def _get_missing_input_dataframes(
return input_data

def _compare_read_to_expected(
self, names: List[str], short_names: bool = False) -> None:
self, names: List[str], short_names: bool = False
) -> None:
"""Compares input data definitions to config file definitions
Arguments:
Expand All @@ -567,7 +568,7 @@ def _compare_read_to_expected(
If should be checking short_names from config file
"""
user_config = self.input_config

if short_names:
expected = []
for name in user_config:
Expand All @@ -579,13 +580,17 @@ def _compare_read_to_expected(
expected = [x for x in user_config]

errors = list(set(expected).symmetric_difference(set(names)))

if errors:
for error in errors:
logger.warning(f"The parameter '{error}' is not found in both the input data and configuration file")
# only raise error if extra name is in the config file
logger.warning(
f"The parameter '{error}' is not found in both the input data and configuration file"
)
# only raise error if extra name is in the config file
if any(error in user_config for error in errors):
raise OtooleConfigFileError("Ensure all parameters and sets defined in the configuration file exist in input data")
raise OtooleConfigFileError(
"Ensure all parameters and sets defined in the configuration file exist in input data"
)

@abstractmethod
def read(
Expand Down
17 changes: 10 additions & 7 deletions src/otoole/read_strategies.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import logging
import os
from typing import Any, Dict, List, Optional, TextIO, Tuple, Union, TextIO
from io import StringIO
from typing import Any, Dict, List, Optional, TextIO, Tuple, Union

import pandas as pd
from amply import Amply
from amply import Amply, AmplyError
from flatten_dict import flatten

from otoole.exceptions import OtooleDeprecationError, OtooleError, OtooleConfigFileError
from otoole.exceptions import OtooleConfigFileError, OtooleDeprecationError, OtooleError
from otoole.input import ReadStrategy
from otoole.preprocess.longify_data import check_datatypes, check_set_datatype
from otoole.utils import create_name_mappings
from amply import AmplyError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -280,21 +279,25 @@ def read(

config = self.user_config
default_values = self._read_default_values(config)

try:
amply_datafile = self.read_in_datafile(filepath, config)
inputs = self._convert_amply_to_dataframe(amply_datafile, config)
except AmplyError as ex:
print(ex)
raise OtooleConfigFileError("Ensure all parameters and set names match between the configuration file and input data")
raise OtooleConfigFileError(
"Ensure all parameters and set names match between the configuration file and input data"
)

self._compare_read_to_expected(names=list(inputs.keys()))
for config_type in ["param", "set"]:
inputs = self._get_missing_input_dataframes(inputs, config_type=config_type)
inputs = self._check_index(inputs)
return inputs, default_values

def read_in_datafile(self, path_to_datafile: Union[str, TextIO], config: Dict) -> Amply:
def read_in_datafile(
self, path_to_datafile: Union[str, TextIO], config: Dict
) -> Amply:
"""Read in a datafile using the Amply parsing class
Arguments
Expand Down
2 changes: 1 addition & 1 deletion tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pandas.testing import assert_frame_equal
from pytest import fixture, mark, raises

from otoole.exceptions import OtooleIndexError, OtooleConfigFileError
from otoole.exceptions import OtooleConfigFileError, OtooleIndexError
from otoole.input import ReadStrategy, WriteStrategy


Expand Down
9 changes: 5 additions & 4 deletions tests/test_read_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pytest import mark, raises

from otoole import ReadCsv, ReadDatafile, ReadExcel, ReadMemory
from otoole.exceptions import OtooleDeprecationError, OtooleError, OtooleConfigFileError
from otoole.exceptions import OtooleConfigFileError, OtooleDeprecationError, OtooleError
from otoole.preprocess.longify_data import check_datatypes
from otoole.results.results import (
ReadCbc,
Expand Down Expand Up @@ -891,20 +891,21 @@ def test_catch_error_no_parameter(self, caplog, user_config):
"Parameter ResultsPath could not be found in the configuration."
in caplog.text
)

def test_catch_error_missing_data(self, user_config):
"""Catches error where input data does not match config"""
reader = ReadDatafile(user_config=user_config)

input_file = """
param NotRealParameter default 1 :=
;
"""

with raises(OtooleConfigFileError):
with StringIO(input_file) as file_buffer:
reader.read(file_buffer)


class TestReadExcel:
def test_read_excel_yearsplit(self, user_config):
""" """
Expand Down

0 comments on commit 8fb88d5

Please sign in to comment.