[Tutor] How to compile "odbchelper.py" app used in "diveintopython" book ?

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 11 Feb 2001 13:46:53 -0800 (PST)


On Sun, 11 Feb 2001, Hitesh N Brahmbhatt wrote:

>     print buildConnectionString(myParams)
> 
> output of "python odbchelper.py" :
> 
>   File "odbchelper.py", line 5
>     return ";".join(["%s=%s" % (k, params[k]) for k in params.keys()])
>                                                 ^
> SyntaxError: invalid syntax 

Do you have Python 2.0?  It looks like odbchelper.py was written using the
"list comprehensions" feature within Python 2.0.  One equivalent
way of writing that in Python 1.52 format is:

###
    from string import join
    return join(map(lambda k: "%s=%s" % (k, params[k]), params.keys()),
                ';')
###

But it might just be cleaner to write your own function to break that
complicated statement down:

###
def makeNameValuePairs(dict):
    result = []
    for key, value in dict.items():
          result.append("%s=%s" % (key, value))
    return result
###

Then the statement above will look like this:

    return join(makeNameValuePairs(params), ';')

which is definitely much cleaner to read.

Hope this helps!