How to convert a Python object into the correct string representation
<p>Debate abounds about using "hasattr" or "isinstance to determine the type of an object. However, I have a situation where I am trying to build a query for <code>numexpr</code> and I need to quote strings, but not other values. I was going to use the following code:</p>

<pre><code>def get_query(key_map):
    """
    Generate an equality query, based on key/value
    pair in key_map
    """
    stmnt = ""
    for k,v in key_map.items():
        value = '"%s"' % v if isinstance(v, str) else v
        if not stmnt:
            # first equality in statement
            stmnt = "'(%s == %s)" % (k,value)
        else:
            stmnt += " &amp; (%s == %s)" % (k,value)

    if stmnt:
        # close statement
        stmnt += "'"
    return stmnt
</code></pre>

<p>I considered using <code>hasattr</code> as well:</p>

<pre><code>value = '"%v"' % v if hasattr(v,'lower') else v
</code></pre>

<p>I had a music teacher once tell me it's OK to break the rules as long as you know how to break them correctly. This seems like one of those cases to me. Does anyone have a suggestion for a more Pythonic way to implement this bit of code?</p>

<p>Sample Inputs:</p>

<pre><code>key_map = {'symbol':'APPL', 'price':698.38'}
</code></pre>

<p>In the sample, above I want to quote APPL because it is a string, but not 698.38, because it is a float.</p>

<p>Regards</p>
