views:

7

answers:

1

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>
A: 

Nick Johnson answered another question of mine which also answers this question. The key portion of which is below. Basically a factory model or function or dictionary is needed, which is a key advantage of Python, which most know about but people like me forget about....

He wrote:

Instead, you should probably define a factory method, like so:

class MyModel(db.PolyModel):
  @classmethod
  def create(cls, foo, bar):
    # Do some stuff
    return cls(foo, bleh)
indiehacker