26
26
27
27
28
28
class AutoSettingNameMeta (type ):
29
- """Adds the name to every SimulationSetting attribute based on its instance
29
+ """Sets the name to every SimulationSetting attribute based on its instance
30
30
name.
31
-
32
- This makes the definition of an extra attribute 'name' obsolete, as the
33
- attributes 'name' is automatic defined based on the instance name.
34
-
35
-
36
- Example:
37
- >>> # create new simulation settings for your awesome simulation
38
- >>> class MyAwesomeSimulationSettings(BaseSimSettings):
39
- ... def __init__(self):
40
- ... super().__init__()
41
-
42
- >>> # create a new simulation setting, name will be taken automatic
43
- from
44
- >>> # instance name
45
- >>> make_simulation_extra_fast = Setting(
46
- ... default=True,
47
- ... choices={
48
- ... True: 'This simulation will be incredible fast.',
49
- ... False: 'This simulation will be increbdile slow.'
50
- ... },
51
- ... description='Run the simulation in extra fast mode?',
52
- ... for_frontend=True
53
- ... )
54
-
55
- >>> # create a SimulationSettings instance and get the value
56
- >>> my_awesome_settings = MyAwesomeSimulationSettings()
57
- >>> # get initial value which is always none
58
- >>> print(my_awesome_settings.make_simulation_extra_fast)
59
- None
60
- >>> # set default values and get the value
61
- >>> my_awesome_settings.load_default_settings()
62
- >>> print(my_awesome_settings.make_simulation_extra_fast)
63
- True
64
31
"""
65
32
66
33
def __init__ (cls , name , bases , namespace ):
67
34
super (AutoSettingNameMeta , cls ).__init__ (name , bases , namespace )
68
35
# get all namespace objects
69
36
for name , obj in namespace .items ():
70
- # filter for settings of simulaiton
37
+ # filter for settings of simulation
71
38
if isinstance (obj , Setting ):
72
39
# provide name of the setting as attribute
73
40
obj .name = name
@@ -99,37 +66,63 @@ def _create_settings(self):
99
66
# Loads setting by name
100
67
setting = getattr (type (self .bound_simulation_settings ), name )
101
68
self [name ] = setting
69
+
70
+ # Store predefined default values in the defaults dict,
71
+ # so they can be set back to the default later
102
72
self .defaults [setting .name ] = setting .value
103
73
104
74
@property
105
75
def names (self ):
106
- """
107
- Returns a generator object with all settings that the bound_simulation_settings owns.
108
- """
76
+ """Returns a generator object with all settings that the bound_simulation_settings owns."""
109
77
bound_simulation_settings_class = type (self .bound_simulation_settings )
110
78
111
79
for attribute_name in dir (bound_simulation_settings_class ):
112
- # Retrieve the attribute from the class using the name
113
80
attribute = getattr (bound_simulation_settings_class , attribute_name )
114
81
115
82
if isinstance (attribute , Setting ):
116
- # If it is, yield the name of the attribute
117
83
yield attribute_name
118
84
119
85
120
86
class Setting (BaseModel , validate_assignment = True , validate_default = True ):
121
- #value: None
87
+ """ Base class for all simulation settings.
88
+ The attribute value which holds the payload of the setting is introduced in derived classes,
89
+ e.g. NumberSetting(Setting).
90
+
91
+ The value which is assigned to the attribute "value", when instancing the setting, serves
92
+ as a default and can be changed according to the use case, if necessary.
93
+
94
+ Args:
95
+ name: Name of the setting. Is set automatically by AutoSettingNameMeta(type)
96
+ description: description the setting
97
+ for_frontend: should this setting be shown in the frontend
98
+ any_string: any string is allowed instead of a given choice
99
+ mandatory: whether a setting needs to be set
100
+ """
101
+
122
102
name : str = Field (default = "set automatically" )
123
103
description : Optional [str ] = None
124
104
for_frontend : bool = Field (default = False )
125
105
any_string : bool = Field (default = False )
126
106
mandatory : bool = Field (default = False )
127
107
128
-
129
108
def __set__ (self , bound_simulation_settings , value ):
109
+ """Creates a new attribute with name and value of the complete setting instance,
110
+ stored in sim_settings. This makes it easier to access the value of a setting.
111
+
112
+ Example:
113
+
114
+ sim_settings = {e.g. BuildingSimSettings}
115
+ ...
116
+ example_setting_name = {bool}True
117
+ manager {SettingsManager}
118
+ 'example_setting_name' = {BooleanSetting}(name='ahu_heating_overwrite',value=True, ...)
119
+ ...
120
+ ...
121
+ """
130
122
bound_simulation_settings .manager [self .name ].value = value
131
123
132
124
def __get__ (self , bound_simulation_settings , owner ):
125
+ """Allows direct access to the setting's value"""
133
126
if bound_simulation_settings is None :
134
127
return self
135
128
@@ -204,7 +197,6 @@ def check_content(self):
204
197
for val in self .value :
205
198
self ._check_for_value_in_choices (val )
206
199
else :
207
- # Todo (chg-ext): Check for multiple choices allowed but only one choice given?
208
200
self ._check_for_value_in_choices (self .value )
209
201
210
202
return self
@@ -228,7 +220,7 @@ def check_value(cls, value):
228
220
for i , guid in enumerate (value ):
229
221
if not check_guid (guid ):
230
222
raise PydanticCustomError ("invalid_guid" ,
231
- f"Invalid IFC GUID format at index { i } : '{ guid } '" ) # type: ignore[misc]
223
+ f"Invalid IFC GUID format at index { i } : '{ guid } '" ) # type: ignore[misc]
232
224
return value
233
225
234
226
0 commit comments