Generating python data structures from parsed text without eval()
<p>I'm trying to build a tool to generate datastructures from a text config.  I use<code>pyparsing</code> to parse the commands...</p>

<p><strong>CONFIG FILE:</strong></p>

<pre><code>add Iteration name = "Cisco 10M/half"
    append Observation name = "packet loss"
        assign Observation results_text = 0.0
        assign Observation results_bool = True
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets
</code></pre>

<p><strong>PYPARSING COMMANDS</strong></p>

<pre><code>['add', 'Iteration', ['name', 'Cisco 10M/half']]
['append', 'Observation', ['name', 'packet loss']]
['assign', 'Observation', ['results_text', '0']]
['assign', 'Observation', ['results_bool', 'True']]
['append', 'DataPoint']
['assign', 'DataPoint', ['metric', 'txpackets']]
['assign', 'DataPoint', ['units', 'packets']]
</code></pre>

<p>The first column is a command verb, and the second is the name of a class I've already defined.</p>

<p>Right now, the only solution I can come up with for building the resultant data structure looks like this:</p>

<pre><code>if cmd == 'add':
    obj = eval('%s()' % (txtobj))  # Create the TestData object
</code></pre>

<p>In the case of the first command, it needs to call <code>Iteration()</code>, which is the name of a class I have imported into the parser.</p>

<p>Is there a way to build the classes from the text config without resorting to <code>eval()</code>?  If so, how would you do it?</p>

<p><strong>EDIT:</strong></p>

<p>Sven's answer assumed I already had the classes created, but dynamically creating classes was actually part of the problem.</p>

<p>This is how I can create a new class named 'Child' dynamically, assuming a parent class named Parent is already in my code...</p>

<pre><code>&gt;&gt;&gt; child_name = "Child"
&gt;&gt;&gt; child_parents = (Parent,)
&gt;&gt;&gt; child body = """
def __init__(self, arg1):
    # Initialization for the Child class
    self.foo = do_something(arg1)
"""
&gt;&gt;&gt; child_dict = {}
&gt;&gt;&gt; exec(child_body, globals(), child_dict)
&gt;&gt;&gt; childobj = type(child_name, child_parents, child_dict)
&gt;&gt;&gt; childobj.__name__
'Child'
&gt;&gt;&gt; childobj.__bases__
(&lt;type 'object'&gt;,)
&gt;&gt;&gt; # Instantiating the new Child object...
&gt;&gt;&gt; childinst = childobj()
&gt;&gt;&gt; childinst
&lt;__main__.Child object at 0x1c91710&gt;
&gt;&gt;&gt;
</code></pre>

<p>If I need to create a global or local variable dynamically, I should avoid <code>globals()</code> and use a <code>dict()</code>...</p>

<pre><code>&gt;&gt;&gt; vars = {}
&gt;&gt;&gt; varname = 'foo'
&gt;&gt;&gt; vars[varname] = True
&gt;&gt;&gt; 
</code></pre>

<p>In a comment, I also asked how I could generate dynamic keywords for method calls... 
such as:</p>

<pre><code>eval('log.append(%s._replace(%s = val))' % (objtxt, key)
</code></pre>

<p>The solution is to use Sven's answer (below) for the class instance, and a <a href="http://stackoverflow.com/questions/5092336/a-better-way-than-eval-when-translating-keyword-arguments-in-querysets-python/5092385#5092385">mapping</a> (i.e. dict) in the method args...</p>

<pre><code>classes = dict((c.__name__, c) for c in [Iteration,Observation,DataPoint])
# ...
obj = classes[txtobj]()
log.append(obj._replace(**{"%s" % key: val}))
</code></pre>
