Skip to content

Commit

Permalink
fix(configuration_parser): use __post_init__ method instead of replac…
Browse files Browse the repository at this point in the history
…ing default dataclass constructor
  • Loading branch information
Iamhexi committed Oct 26, 2024
1 parent fdd037d commit 39e55a6
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions knowledge_verificator/utils/configuration_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,33 @@ class Configuration:
"""

learning_materials: Path
logging_level: str
mode: OperatingMode
experiment_implementation: Path
experiment_results: Path
production_mode: bool
backend_address: str
backend_port: int
frontend_address: str
frontend_port: int
question_generation_model: QuestionGenerationModel
natural_language_inference_model: NaturalLanguageInferenceModel
protocol: str

def __init__(
self,
**kwargs, # Records received from a YAML file.
) -> None:
# Assign values to attributes.
for attribute, value in kwargs.items():
self.__setattr__(attribute, value)

logging_level: str = 'WARNING'
mode: OperatingMode = OperatingMode.CLIENT_SERVER
production_mode: bool = True
backend_address: str = '127.0.0.1'
backend_port: int = 8000
frontend_address: str = '127.0.0.1'
frontend_port: int = 3000
protocol: str = 'http'

def __post_init__(self) -> None:
logger = logging.Logger('Configuration parser', level=logging.DEBUG)
# Convert to a proper datatypes.
try:
if not isinstance(self.natural_language_inference_model, str):
raise ValueError(
'Incorrect value for `natural_language_inference_model` '
'configuration option: '
f'{self.natural_language_inference_model}.'
)

self.natural_language_inference_model = (
NaturalLanguageInferenceModel[
kwargs['natural_language_inference_model'].upper()
self.natural_language_inference_model.upper() # pylint: disable=no-member
]
)

Expand All @@ -93,8 +93,14 @@ def __init__(
sys.exit(1)

try:
if not isinstance(self.question_generation_model, str):
raise ValueError(
'Incorrect value for `question_generation_model` '
'configuration option: '
f'{self.question_generation_model}.'
)
self.question_generation_model = QuestionGenerationModel[
kwargs['question_generation_model'].upper()
self.question_generation_model.upper() # pylint: disable=no-member
]
except KeyError as e:
logger.critical(
Expand All @@ -103,11 +109,9 @@ def __init__(
)
sys.exit(1)

self.mode: OperatingMode = OperatingMode(kwargs['mode'])
self.experiment_implementation = Path(
kwargs['experiment_implementation']
)
self.experiment_results = Path(kwargs['experiment_results'])
self.mode: OperatingMode = OperatingMode(self.mode)
self.experiment_implementation = Path(self.experiment_implementation)
self.experiment_results = Path(self.experiment_results)


class ConfigurationParser:
Expand Down

0 comments on commit 39e55a6

Please sign in to comment.