Instantiate a model when the kind of model needed is represented as a string argument

Posted by indiehacker on Stack Overflow See other posts from Stack Overflow or by indiehacker
Published on 2010-04-21T09:42:29Z Indexed on 2010/04/21 9:43 UTC
Read the original article Hit count: 215

Filed under:

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>

© Stack Overflow or respective owner

Related posts about google-datastore