Adding Macros to Python
<p>I would like to invoke the following code <strong>in-situ</strong> wherever I refer to <code>MY_MACRO</code> in my code below.</p>

<pre><code># MY_MACRO
frameinfo = getframeinfo(currentframe())
msg = 'We are on file ' + frameinfo.filename + ' and line ' +  str(frameinfo.lineno)
# Assumes access to namespace and the variables in which `MY_MACRO` is called. 
current_state = locals().items()
</code></pre>

<p>Here is some code that would use <code>MY_MACRO</code>:</p>

<pre><code>def some_function:
    MY_MACRO

def some_other_function:
    some_function()
    MY_MACRO

class some_class:
  def some_method:
     MY_MACRO
</code></pre>

<p>In case it helps:</p>

<ol>
<li>One of the reasons why I would like to have this ability is because I would like to avoid repeating the code of <code>MY_MACRO</code> wherever I need it. Having something short and easy would be very helpful. </li>
<li>Another reason is because I want to embed an IPython shell wihthin the macro and I would like to have access to all variables in <code>locals().items()</code> (see <a href="http://stackoverflow.com/questions/15669186/using-ipython-as-an-effective-debugger">this other question</a>)</li>
</ol>

<p>Is this at all possible in Python? What would be the easiest way to get this to work?</p>

<p>Please <strong>NOTE</strong> that the macro assumes access to the entire namespace of the scope in which it's called (i.e. <strong>merely placing</strong> the code <code>MY_MACRO</code> in <strong>a function would not work</strong>). Note also that if I place <code>MY_MACRO</code> in a function, <code>lineno</code> would output the wrong line number.</p>
