Last active
March 16, 2023 18:06
-
-
Save DalyaG/a0c39494942b0cb06cc6750cc8445861 to your computer and use it in GitHub Desktop.
One param class for the entire project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import os | |
| from datetime import datetime | |
| from optparse import OptionParser | |
| from dataclasses import dataclass | |
| @dataclass | |
| class Params: | |
| """ | |
| src_path (str, default = '.'): Location of data, by default you should run from this folder | |
| user_input (int, mandatory - no default): A mandatory input from the user. | |
| some_magic_number (float, default = -3.1): In some very internal component we use this magic number because it works. | |
| If you REALLY know what you are doing, you can edit this parameter from here. | |
| some_list_param_str (str, default = '1,2.2,3'): Since lists are mutable, we don't want to have a default list value. | |
| Instead, let's use a string, separated by commas, and in the post-init, we will fill the default value | |
| of some_list_param. | |
| This means that the default value of some_list_param is [1, 2.2, 3], but to change it you need to change the | |
| default value of some_list_param_str. | |
| """ | |
| src_path: str = '.' | |
| output_folder_name_prefix: str = 'output' | |
| start_time_str : str = None | |
| output_folder_name: str = None | |
| output_folder_path: str = None | |
| user_input: int = None | |
| some_magic_number: float = -3.1 | |
| some_list_param_str: str = '1,2.2,3' | |
| some_list_param: [float] = None | |
| def __post_init__(self): | |
| if self.user_input is None: | |
| raise ValueError("Please specify a value for user_input!") | |
| self.start_time_str = datetime.today().strftime('%d_%m_%Y__%H_%M_%S') | |
| self.output_folder_name = f'{self.output_folder_name_prefix}_{self.start_time_str}' | |
| self.output_folder_path = os.path.join(self.src_path, self.output_folder_name) | |
| self.some_list_param = [float(x) for x in self.some_list_param_str.split(',')] | |
| def to_dict(self): | |
| return self.__dict__.copy() | |
| def to_json(self, file_path: str = None): | |
| d = self.to_dict() | |
| sorted_dict = {k: d[k] for k in sorted(d.keys())} | |
| json.dump(sorted_dict, open(file_path, 'w'), indent=4) | |
| @staticmethod | |
| def from_dict(d: dict): | |
| return Params(**d) | |
| @staticmethod | |
| def from_json(file_path: str): | |
| d = json.load(open(file_path, 'r')) | |
| return Params.from_dict(d) | |
| @staticmethod | |
| def from_parser(parser: OptionParser): | |
| input_params, _ = parser.parse_args() | |
| kwargs = {key: value for key, value in input_params.__dict__.items() if value is not None} | |
| params = Params(**kwargs) | |
| return params |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment