Rewriting to Python 3

Peter Otten __peter__ at web.de
Fri May 1 04:09:24 EDT 2015


Cecil Westerhof wrote:

> On my system I have:
>     PARSER_RE_STR = '/(%s)=' % '|'.join(DN_LUT.keys() + DN_LUT.values())
> in:
>     /usr/lib/python3.4/site-
packages/ndg/httpsclient/ssl_peer_verification.py
> 
> In Python 3 that gives:
>     TypeError: unsupported operand type(s) for +: 'dict_keys' and
>     'dict_values'
> 
> How should I rewrite this?


There's a tool called 2to3 -- it doesn't produce perfect code but it can 
cope with the mechanical changes:
 
$ cat demo.py    
d = dict("aA bB cC".split())

try:
    print d.keys() + d.values()
except Exception, e:
    print e
$ 2to3 -w demo.py
[...]
$ cat demo.py
d = dict("aA bB cC".split())

try:
    print(list(d.keys()) + list(d.values()))
except Exception as e:
    print(e)
$ python3 demo.py
['c', 'a', 'b', 'C', 'A', 'B']
$




More information about the Python-list mailing list