Dynamic create questions with inquirer
<p>i have the following object:</p>

<pre><code>questions = [
   {      'type': 'Text'
     ,    'name': 'input_file'
     , 'message': 'Input file'
   }
]
</code></pre>

<p>And i want transform into this:</p>

<pre><code>inquirer.Text(name = 'input_file', message = 'Input file')
</code></pre>

<p>So, i'm using the following code:</p>

<pre><code>for question in questions:
    q.append(inquirer[question['type']](question['name'], question['message']))
</code></pre>

<p>But this wont work:</p>

<pre><code>Traceback (most recent call last):
  File "Funds/__main__.py", line 4, in &lt;module&gt;
    main()
  File "/Users/MarceloAlves/DEV/LACLAW/Funds/funds/funds.py", line 30, in main
    ask_questions_to_user()
  File "/Users/MarceloAlves/DEV/LACLAW/Funds/funds/funds.py", line 11, in ask_questions_to_user
    q.append(inquirer[question['type']]('name', message="What's your name"))
TypeError: 'module' object has no attribute '__getitem__'
</code></pre>

<p>Any ideas? (I have a background in js)</p>

<p>You need to use <code>getattr</code>:</p>

<pre><code>for question in questions:
    q.append(getattr(inquirer, question['type'])(question['name'], question['message']))
</code></pre>

<p>You can read more about it <a href="http://stackoverflow.com/questions/7119235/in-python-what-is-the-difference-between-an-object-and-a-dictionary">here</a></p>

python