Skip to content

Commit dd68fa9

Browse files
committed
add comments
1 parent e2fb6ea commit dd68fa9

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

bim2sim/sim_settings.py

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,15 @@
2626

2727

2828
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
3030
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
6431
"""
6532

6633
def __init__(cls, name, bases, namespace):
6734
super(AutoSettingNameMeta, cls).__init__(name, bases, namespace)
6835
# get all namespace objects
6936
for name, obj in namespace.items():
70-
# filter for settings of simulaiton
37+
# filter for settings of simulation
7138
if isinstance(obj, Setting):
7239
# provide name of the setting as attribute
7340
obj.name = name
@@ -99,37 +66,63 @@ def _create_settings(self):
9966
# Loads setting by name
10067
setting = getattr(type(self.bound_simulation_settings), name)
10168
self[name] = setting
69+
70+
# Store predefined default values in the defaults dict,
71+
# so they can be set back to the default later
10272
self.defaults[setting.name] = setting.value
10373

10474
@property
10575
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."""
10977
bound_simulation_settings_class = type(self.bound_simulation_settings)
11078

11179
for attribute_name in dir(bound_simulation_settings_class):
112-
# Retrieve the attribute from the class using the name
11380
attribute = getattr(bound_simulation_settings_class, attribute_name)
11481

11582
if isinstance(attribute, Setting):
116-
# If it is, yield the name of the attribute
11783
yield attribute_name
11884

11985

12086
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+
122102
name: str = Field(default="set automatically")
123103
description: Optional[str] = None
124104
for_frontend: bool = Field(default=False)
125105
any_string: bool = Field(default=False)
126106
mandatory: bool = Field(default=False)
127107

128-
129108
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+
"""
130122
bound_simulation_settings.manager[self.name].value = value
131123

132124
def __get__(self, bound_simulation_settings, owner):
125+
"""Allows direct access to the setting's value"""
133126
if bound_simulation_settings is None:
134127
return self
135128

@@ -204,7 +197,6 @@ def check_content(self):
204197
for val in self.value:
205198
self._check_for_value_in_choices(val)
206199
else:
207-
# Todo (chg-ext): Check for multiple choices allowed but only one choice given?
208200
self._check_for_value_in_choices(self.value)
209201

210202
return self
@@ -228,7 +220,7 @@ def check_value(cls, value):
228220
for i, guid in enumerate(value):
229221
if not check_guid(guid):
230222
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]
232224
return value
233225

234226

0 commit comments

Comments
 (0)