Instantiate a model when the kind of model needed is represented as a string argument
- by indiehacker
My input data is a string representing the kind of datastore model I want to make.
In python, I am using the eval() function to instantiate the model (below code), but this seems overly complex so I was wondering if there is a simpler way people normally do this?
>>>model_kind="TextPixels"
>>>key_name_eval="key_name"
>>>key_name="key_name"
>>>kwargs
{'lat': [0, 1, 2, 3], 'stringText': 'boris,ted', 'lon': [0, 1, 2, 8], 'zooms': [0, 10]}
>>>obj=eval( model_type + '(key_name='+tester+ ',**kwargs )' )
>>>obj
<datamodel.TextPixels object at 0xed8808c>