Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89705 > unrolled thread

Rewriting to Python 3

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-05-01 09:22 +0200
Last post2015-05-01 16:52 +0200
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Rewriting to Python 3 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-01 09:22 +0200
    Re: Rewriting to Python 3 Ben Finney <ben+python@benfinney.id.au> - 2015-05-01 17:56 +1000
    Re: Rewriting to Python 3 Peter Otten <__peter__@web.de> - 2015-05-01 10:09 +0200
    Re: Rewriting to Python 3 Chris Angelico <rosuav@gmail.com> - 2015-05-01 18:13 +1000
    Re: Rewriting to Python 3 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-01 09:10 +0100
    Re: Rewriting to Python 3 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-01 16:41 +0200
      Re: Rewriting to Python 3 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-01 16:52 +0200

#89705 — Rewriting to Python 3

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-01 09:22 +0200
SubjectRewriting to Python 3
Message-ID<87egn0rcc1.fsf@Equus.decebal.nl>
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?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#89707

FromBen Finney <ben+python@benfinney.id.au>
Date2015-05-01 17:56 +1000
Message-ID<mailman.1.1430467011.3347.python-list@python.org>
In reply to#89705
Cecil Westerhof <Cecil@decebal.nl> writes:

> 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

Are you in contact with the author of that third-party library? Why is
it in your Python 3.4 site-packages?

> In Python 3 that gives:
>     TypeError: unsupported operand type(s) for +: 'dict_keys' and 'dict_values'
>
> How should I rewrite this?

My attempt:

    PARSER_RE_STR = "/({keys}|{values})=".format(
            keys=DN_LUT.keys(), values=DN_LUT.values())

Explicit is better than implicit, especially in mini-languages like
string interpolation.

-- 
 \      “Often, the surest way to convey misinformation is to tell the |
  `\               strict truth.” —Mark Twain, _Following the Equator_ |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#89711

FromPeter Otten <__peter__@web.de>
Date2015-05-01 10:09 +0200
Message-ID<mailman.3.1430467780.3347.python-list@python.org>
In reply to#89705
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']
$

[toc] | [prev] | [next] | [standalone]


#89713

FromChris Angelico <rosuav@gmail.com>
Date2015-05-01 18:13 +1000
Message-ID<mailman.4.1430468012.3347.python-list@python.org>
In reply to#89705
On Fri, May 1, 2015 at 5:22 PM, Cecil Westerhof <Cecil@decebal.nl> 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?

The keys() and values() methods used to return lists, now they return
views. If you explicitly listify them, it should work; alternatively,
pipe-join each one separately and then pipe-join the result:

PARSER_RE_STR = '/(%s|%s)=' % ('|'.join(DN_LUT.keys()),
'|'.join(DN_LUT.values()))

Note that this is slightly different from the above; in the case of an
empty dict, the original produces empty parentheses, but my
alternative will place a junk pipe in there.

ChrisA

[toc] | [prev] | [next] | [standalone]


#89714

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-05-01 09:10 +0100
Message-ID<mailman.5.1430468106.3347.python-list@python.org>
In reply to#89705
On 01/05/2015 08:22, 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?
>

I'd run the entire package through the 2to3 tool that's part of the 
standard library, or uninstall that package and get an up to date 
version that actually supports Python 3.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#89739

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-01 16:41 +0200
Message-ID<87k2wsnyw7.fsf@Equus.decebal.nl>
In reply to#89705
Op Friday 1 May 2015 09:22 CEST schreef Cecil Westerhof:

> 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?

I am not the only person that has a problem:
    https://github.com/cedadev/ndg_httpsclient/issues/1

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#89741

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-01 16:52 +0200
Message-ID<87bni4nydg.fsf@Equus.decebal.nl>
In reply to#89739
Op Friday 1 May 2015 16:41 CEST schreef Cecil Westerhof:

> Op Friday 1 May 2015 09:22 CEST schreef Cecil Westerhof:
>
>> 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?
>
> I am not the only person that has a problem:
> https://github.com/cedadev/ndg_httpsclient/issues/1

Just personally contacting does make a difference. He made a patch. I
am going to test a later today.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web