I am currently designing a configuration screen for my application. There are about 50 configuration parameters, which I have described in a CSV file, and read in Python.
Currently the fields for each parameter row are:
csv_row_count
cat_name
attr_name
disp_str
unit
data_type
min_val
max_val
def_val
incr
cur_val
desc
So a few examples for a parameter could be:
Every single parameter is then drawn in my application as UI for the user to interact with, and looks like this:
The csv_row_count
is later used as an idx
for looping. If I ever want to add another parameter to be configured, I would have to use a unique one and add the parameter to the bottom of the CSV.
I currently have a class which stores all the information into attributes and can be stored back to the CSV after changes by the user.
Now, the problem I am having is that I need to react to the changes the user makes in the configuration screen (uniquely for every single parameter).
So my idea was to create an ConfigEventHandler
class, that has an Event
for when the parameter is created (on boot of the application, to draw it to the UI for example) and an Event
which is fired when the value has been changed (to make changes to the value in the UI, and also react correspondingly for this specific setting that has been changed).
That class looks like this:
from axel import Event
import logging as log
class ConfigEventHandler:
def __init__(self):
# Make an Event for when a config row is created (usually only at boot of application)
self.on_create_ev = Event()
self.on_create_ev += self._on_create_event
# Make an Event for when a config row has changed. Fire-ing of this event will be after a change in the attribute.
self.on_change_ev = Event()
self.on_change_ev += self._on_value_change_event
def _on_create_event(self, idx):
pass
# log.info("Event OnCreate fired for IDX " + str(idx))
def _on_value_change_event(self, idx, user_dict):
log.info("Event On Value Change fired for IDX " + str(idx))
log.info("With user dict: " + str(user_dict))
And I fire these events on creation of the class and when the values change. The only way for this events to identify which value has been changed, is by the idx
value which correlates to the csv_row_count
attribute as described in the CSV.
This would mean that if I want to react to each parameter being changed, I would need to fill the _on_value_change_event
method with a lot of if
-statements, e.g.:
if idx == 0:
react_to_param0()
elif idx == 1:
react_to_param1()
etc...
I feel like this is really bad practice (in any program language), and would easily break if someone screwed around with the idx
numbering in the CSV file (E.G. me in a few months, or the person that will take over my project).
So my question is, what would be a good alternative for fixing this relationship issue in a way that is more clean than a lot of if
-statements and a relationship between a list idx
and CSV index to make it more future proof, and keep the code readable?
I have thought of making a seperate class instance for each congifurable parameter, but don't really see how that would break the 'fragile' seeming relation between the csv_row_count
/idx
and the unique functions that react to changes to one specific parameter.