Source code for diva.config.config_object
# Copyright 2024 Mews
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
from typing import Union
[docs]
class Config:
"""
Config object, containing all graph parameters found in the user prompt.
All the parameters are private attributes (to avoid overwriting them by accident).
Values are assigned to these attributes with setters and taken for usages external to the class with getters.
(e.g. for self.__location, setter is set_location(self, value) with value the value to pass to self.__location
and getter is def location(self) -> self.__location with the decorator @property)
Methods
-------
__str__()
returns a dictionary with the main graph parameters (the main private attributes of the class).
__repr__()
returns other information about the class such as the run time to create it, the user prompt, and the missing
parameters.
"""
def __init__(self):
self.__location: str = "Unknown"
self.__time_expression: str = "Unknown"
self.__start_time: str = "Unknown"
self.__end_time: str = "Unknown"
self.__climate_variable: str = "Unknown"
self.__graph_type: str = "Unknown"
self.__aggregation_type: str = "raw"
self.__aggregation_operator: str = "mean"
self.__anomaly: bool = False
self.__last: Union['Config', None] = None
self.__creation_time: float | None = None
self.__user_prompt: str | None = None
self.__missings: list[str] = []
self.__not_in_shp: list[str] = []
def __str__(self):
""" returns a text string displaying a dictionary with the main graph parameters (the main private attributes
of the class). """
dict_all = {
"starttime": self.start_time,
"endtime": self.end_time,
"location": self.location,
"climate_variable": self.climate_variable,
"graph_type": self.graph_type,
"aggreg_type": self.aggregation_type,
"agg_operator": self.aggregation_operator,
}
return f"{dict_all}"
def __repr__(self):
""" Returns a text string with other information about the class. The creation time is the run time
of prompt_to_config in diva.config.services.creation. Also returns the user prompt from which the
Config object was created, and the missing mandatory graph parameters, not found in the user prompt """
return (f"Config creation time: {self.__creation_time}, "
f"from user prompt: {self.__user_prompt}, "
f"missings: {self.__missings})")
[docs]
def set_location(self, value: str):
self.__location = value
[docs]
def set_time_expression(self, value: str | list[str]):
self.__time_expression = value
[docs]
def set_start_time(self, value: str):
self.__start_time = value
[docs]
def set_end_time(self, value: str):
self.__end_time = value
[docs]
def set_climate_variable(self, value: str):
self.__climate_variable = value
if "Unknown" not in value:
self.__climate_variable = self.__climate_variable.lower()
[docs]
def set_graph_type(self, value: str):
self.__graph_type = value
[docs]
def set_agg_operator(self, value: str):
self.__aggregation_operator = value
[docs]
def set_aggregation_type(self, value: str):
self.__aggregation_type = value
[docs]
def set_anomaly(self, value: bool):
self.__anomaly = value
[docs]
def set_last(self, value: object):
self.__last = value
[docs]
def set_creation_time(self, value: float):
self.__creation_time = value
[docs]
def set_user_prompt(self, value: str):
self.__user_prompt = value
[docs]
def set_missings(self, value: list):
if type(value) is list:
self.__missings = value
else:
print("Warning: missings not set. Value argument is not of type list.")
[docs]
def add_not_in_shp(self, text: str):
""" Acts as a setters. Appends in a list, in the private attributes, the locations not found in the
shapefiles """
if type(text) is str:
self.__not_in_shp.append(text)
else:
print("Warning: value is not a character string and could not be added to not_in_shp.")
# getters
@property
def location(self) -> str:
return self.__location
@property
def time_expression(self) -> str:
return self.__time_expression
@property
def start_time(self) -> str:
return self.__start_time
@property
def end_time(self) -> str:
return self.__end_time
@property
def climate_variable(self) -> str:
return self.__climate_variable
@property
def graph_type(self) -> str:
return self.__graph_type
@property
def aggregation_operator(self) -> str:
return self.__aggregation_operator
@property
def aggregation_type(self) -> str:
return self.__aggregation_type
@property
def anomaly(self) -> bool:
return self.__anomaly
@property
def creation_time(self) -> float:
return self.__creation_time
@property
def last(self) -> 'Config':
return self.__last
@property
def not_in_shp(self) -> list[str]:
return self.__not_in_shp