I am writing a library that can be used with some GUI and would like to be able to build an interface where user can see and/or change most of the object's parameters, and also write some of these parameters into the separate tables of SQL database.
Instead of hard-coding GUI representations for each object, I want to create them from objects parameters, so each parameter should have a corresponding type of GUI element associated with it. In a same manner, I want to write parameters to SQL tables based on the sql_tables property of parameter.
The best I came up so far is the following:
class Param:
def __init__(name, value, gui_element, sql_tables):
self.name = name
self.value = value
self.gui_element = gui_element
# Names of the tables into which the parameter will be written
self.sql_tables = sql_tables
class FruitParams:
def __init__(fruit_type, init_amount, sold_amount):
self.fruit_type = Param(
'Fruit type', fruit_type, 'Label', ('Fruits', 'Inventory'))
self.init_amount = Param('Amount harvested', init_amount, 'EditableLabel', ('Inventory',) )
self.sold_amount = Param('Amount sold', sold_amount, 'Slider', ('Inventory',))
class Fruit:
def __init__(self, params):
self.params = params
apple_params = FruitParams('Apple', 100, 0)
apple = Fruit(apple_params)
# proceed to Building GUI from apple.params ...
There will be dozens of parameters for each object. I wonder if there is a better approach than the one I am thinking about? It seems like a fairly common problem, so I don't want to re-invent the wheel. Also, I already have a big chunk of library written, so if I am to change all parameters into the proposed form, I would have to add .value to every already existing usage of each parameter, which I would prefer to avoid.
Or would some dictionaries that match parameters to their corresponding elements of other objects will be better in this case? Or should I add some factory object and somehow use it for the matching?