How to view variables within a function when warning is thrown pdb in python?
<p>Im running into a problem where</p>

<pre><code>cds_to_protein = str(feature.extract(record).seq.translate(to_stop = True))
</code></pre>

<p>will throw a Biopython warning.  So I need to see what record and feature is being passed to it that causes that warning.</p>

<p>Is there a way to use pdb or another debugger to view parameters within a function?</p>

<p>EDIT: I'm using <code>python -Werror -m pdb script.py</code> which works to stop pdb when the warning appears</p>

<p>I've also added</p>

<pre><code>    saved_record = record
    saved_feature = feature
</code></pre>

<p>in order to save them but I don't know if they're necessary.  Should I make them global in order to view them?</p>

<p>Here's the function:</p>

<pre><code>def validate_cds(record, feature):

    saved_record = record
    saved_feature = feature

    protein_in_file = str(feature.qualifiers.get('translation', 'no_translation')).strip('\'[]')

    cds_to_protein = str(feature.extract(record).seq.translate(to_stop = True))

    return
</code></pre>

<p>Try adding a trace to your function:</p>

<pre><code>def validate_cds(record, feature):
    import pdb; pdb.set_trace()
    saved_record = record
    saved_feature = feature

    protein_in_file = str(feature.qualifiers.get('translation', 'no_translation')).strip('\'[]')

    cds_to_protein = str(feature.extract(record).seq.translate(to_stop = True))

    return
</code></pre>

<p>Now when your function is called you will be popped into <code>pdb</code> and can interactively step through the function line by line, inspect variables, etc. Keep in mind though, that when you first see the <code>pdb</code> prompt, the variables you are interested in will not yet be initialized, so you can start stepping through the function with <code>n</code>, until you pass the points in the function that you are interested in.</p>

<p>I like having the import and the function call on the same line like this because there is only one line you have to clean up after debugging, and also, many syntax checkers will flag the line as since pep 8 discourages compound statements like this, so it will be a reminder to get rid of the line later.</p>

python python-2.7 biopython