Fumagalli_Motta_Tarantino_2020.Configurations.LoadConfig
1import csv 2import os.path 3from typing import Optional 4 5import Fumagalli_Motta_Tarantino_2020.Configurations.StoreConfig as StoreConfig 6import Fumagalli_Motta_Tarantino_2020.Configurations.ConfigExceptions as Exceptions 7import Fumagalli_Motta_Tarantino_2020.Models.Types as Types 8 9 10class LoadParameters: 11 """ 12 Loads a specific configuration from a file. 13 """ 14 15 file_name: str = "params.csv" 16 """Filename of the configuration file.""" 17 18 def __init__(self, config_id: int, file_path: Optional[str] = None): 19 """ 20 Initializes a valid object with a valid path to the configuration file and a valid id for the configuration. 21 22 Parameters 23 ---------- 24 config_id: int 25 ID of the configuration (Fumagalli_Motta_Tarantino_2020.Configurations) 26 file_path: str 27 Path to configuration file, if not set, then the standard file is used. 28 """ 29 self._id = config_id 30 self._file_path = self._set_path(file_path) 31 self.params: StoreConfig.ParameterModel = self._select_configuration() 32 33 @staticmethod 34 def _set_path(file_path: Optional[str]) -> str: 35 return ( 36 os.path.join(os.path.dirname(__file__), LoadParameters.file_name) 37 if file_path is None 38 else file_path 39 ) 40 41 def adjust_parameters(self, **kwargs) -> None: 42 """ 43 Change parameter values of the configuration. 44 45 You can change as many values as you wish with one call. 46 47 Parameters 48 ---------- 49 **kwargs 50 Form: {"name_of_parameter": new_value_of_parameter, ...} 51 """ 52 for key, value in kwargs.items(): 53 self.params.set(key, value) 54 55 def _select_configuration(self) -> StoreConfig.ParameterModel: 56 configs = self._parse_file() 57 for config in configs: 58 if config["id"] == self._id: 59 return StoreConfig.ParameterModel( 60 merger_policy=Types.MergerPolicies.Strict, 61 development_costs=config["K"], 62 startup_assets=config["A"], 63 success_probability=config["p"], 64 development_success=True, 65 private_benefit=config["B"], 66 consumer_surplus_without_innovation=config["CSm"], 67 incumbent_profit_without_innovation=config["PmI"], 68 consumer_surplus_duopoly=config["CSd"], 69 incumbent_profit_duopoly=config["PdI"], 70 startup_profit_duopoly=config["PdS"], 71 consumer_surplus_with_innovation=config["CSM"], 72 incumbent_profit_with_innovation=config["PMI"], 73 ) 74 raise Exceptions.IDNotAvailableError("No configuration with this ID found.") 75 76 def _parse_file(self): 77 with open(file=self._file_path, newline="") as f: 78 configs = [] 79 for row in csv.DictReader(f, skipinitialspace=True): 80 if not self._is_comment_row(row): 81 tmp = {} 82 for k, v in row.items(): 83 tmp.update({k: self._parse_value(v)}) 84 configs.append(tmp) 85 return configs 86 87 @staticmethod 88 def _is_comment_row(row: dict[str, str]) -> bool: 89 if row["id"].strip() == "#": 90 return True 91 return False 92 93 def toggle_development_success(self) -> None: 94 """ 95 Changes the value of the development success (if attempted) to the exact opposite. 96 97 - False $\\Rightarrow$ True 98 - True $\\Rightarrow$ False 99 """ 100 self.params.set( 101 "development_success", not self.params.get("development_success") 102 ) 103 104 def set_startup_assets(self, value: float): 105 """ 106 Sets the value of the start-up assets. 107 """ 108 self.params.set("startup_assets", value) 109 110 def set_merger_policy(self, value: Types.MergerPolicies): 111 """ 112 Sets the merger policy. 113 """ 114 self.params.merger_policy = value 115 116 @staticmethod 117 def _parse_value(value): 118 try: 119 return int(value) 120 except ValueError: 121 return float(value) 122 123 def __call__(self, *args, **kwargs) -> dict: 124 """ 125 Returns a dict containing all the parameters and their values. 126 """ 127 return self.params()
class
LoadParameters:
11class LoadParameters: 12 """ 13 Loads a specific configuration from a file. 14 """ 15 16 file_name: str = "params.csv" 17 """Filename of the configuration file.""" 18 19 def __init__(self, config_id: int, file_path: Optional[str] = None): 20 """ 21 Initializes a valid object with a valid path to the configuration file and a valid id for the configuration. 22 23 Parameters 24 ---------- 25 config_id: int 26 ID of the configuration (Fumagalli_Motta_Tarantino_2020.Configurations) 27 file_path: str 28 Path to configuration file, if not set, then the standard file is used. 29 """ 30 self._id = config_id 31 self._file_path = self._set_path(file_path) 32 self.params: StoreConfig.ParameterModel = self._select_configuration() 33 34 @staticmethod 35 def _set_path(file_path: Optional[str]) -> str: 36 return ( 37 os.path.join(os.path.dirname(__file__), LoadParameters.file_name) 38 if file_path is None 39 else file_path 40 ) 41 42 def adjust_parameters(self, **kwargs) -> None: 43 """ 44 Change parameter values of the configuration. 45 46 You can change as many values as you wish with one call. 47 48 Parameters 49 ---------- 50 **kwargs 51 Form: {"name_of_parameter": new_value_of_parameter, ...} 52 """ 53 for key, value in kwargs.items(): 54 self.params.set(key, value) 55 56 def _select_configuration(self) -> StoreConfig.ParameterModel: 57 configs = self._parse_file() 58 for config in configs: 59 if config["id"] == self._id: 60 return StoreConfig.ParameterModel( 61 merger_policy=Types.MergerPolicies.Strict, 62 development_costs=config["K"], 63 startup_assets=config["A"], 64 success_probability=config["p"], 65 development_success=True, 66 private_benefit=config["B"], 67 consumer_surplus_without_innovation=config["CSm"], 68 incumbent_profit_without_innovation=config["PmI"], 69 consumer_surplus_duopoly=config["CSd"], 70 incumbent_profit_duopoly=config["PdI"], 71 startup_profit_duopoly=config["PdS"], 72 consumer_surplus_with_innovation=config["CSM"], 73 incumbent_profit_with_innovation=config["PMI"], 74 ) 75 raise Exceptions.IDNotAvailableError("No configuration with this ID found.") 76 77 def _parse_file(self): 78 with open(file=self._file_path, newline="") as f: 79 configs = [] 80 for row in csv.DictReader(f, skipinitialspace=True): 81 if not self._is_comment_row(row): 82 tmp = {} 83 for k, v in row.items(): 84 tmp.update({k: self._parse_value(v)}) 85 configs.append(tmp) 86 return configs 87 88 @staticmethod 89 def _is_comment_row(row: dict[str, str]) -> bool: 90 if row["id"].strip() == "#": 91 return True 92 return False 93 94 def toggle_development_success(self) -> None: 95 """ 96 Changes the value of the development success (if attempted) to the exact opposite. 97 98 - False $\\Rightarrow$ True 99 - True $\\Rightarrow$ False 100 """ 101 self.params.set( 102 "development_success", not self.params.get("development_success") 103 ) 104 105 def set_startup_assets(self, value: float): 106 """ 107 Sets the value of the start-up assets. 108 """ 109 self.params.set("startup_assets", value) 110 111 def set_merger_policy(self, value: Types.MergerPolicies): 112 """ 113 Sets the merger policy. 114 """ 115 self.params.merger_policy = value 116 117 @staticmethod 118 def _parse_value(value): 119 try: 120 return int(value) 121 except ValueError: 122 return float(value) 123 124 def __call__(self, *args, **kwargs) -> dict: 125 """ 126 Returns a dict containing all the parameters and their values. 127 """ 128 return self.params()
Loads a specific configuration from a file.
LoadParameters(config_id: int, file_path: Optional[str] = None)
19 def __init__(self, config_id: int, file_path: Optional[str] = None): 20 """ 21 Initializes a valid object with a valid path to the configuration file and a valid id for the configuration. 22 23 Parameters 24 ---------- 25 config_id: int 26 ID of the configuration (Fumagalli_Motta_Tarantino_2020.Configurations) 27 file_path: str 28 Path to configuration file, if not set, then the standard file is used. 29 """ 30 self._id = config_id 31 self._file_path = self._set_path(file_path) 32 self.params: StoreConfig.ParameterModel = self._select_configuration()
Initializes a valid object with a valid path to the configuration file and a valid id for the configuration.
Parameters
- config_id (int): ID of the configuration (Fumagalli_Motta_Tarantino_2020.Configurations)
- file_path (str): Path to configuration file, if not set, then the standard file is used.
def
adjust_parameters(self, **kwargs) -> None:
42 def adjust_parameters(self, **kwargs) -> None: 43 """ 44 Change parameter values of the configuration. 45 46 You can change as many values as you wish with one call. 47 48 Parameters 49 ---------- 50 **kwargs 51 Form: {"name_of_parameter": new_value_of_parameter, ...} 52 """ 53 for key, value in kwargs.items(): 54 self.params.set(key, value)
Change parameter values of the configuration.
You can change as many values as you wish with one call.
Parameters
- **kwargs: Form: {"name_of_parameter": new_value_of_parameter, ...}
def
toggle_development_success(self) -> None:
94 def toggle_development_success(self) -> None: 95 """ 96 Changes the value of the development success (if attempted) to the exact opposite. 97 98 - False $\\Rightarrow$ True 99 - True $\\Rightarrow$ False 100 """ 101 self.params.set( 102 "development_success", not self.params.get("development_success") 103 )
Changes the value of the development success (if attempted) to the exact opposite.
- False $\Rightarrow$ True
- True $\Rightarrow$ False