Follow

Been porting a fair amount of code from to at @logilab recently. Here's a thread about how I usually proceed and what I learnt. (1/7)

First run flake8 with python3 and fix all spotted errors (e.g. "except ValueError, exc:"). Easy.
(2/7)

@logilab

Show thread

Try an automatic code porting tool, like python-modernize.readthedocs.i, to remove idioms. This is no silver bullet but it helps. For instance, it will detect things now in six.moves (like itertools.imap) or usage of .next() and update your code. On the other hand, it sometimes does useless things like changing "for k, v in d.iteritems():" into "for k, v in six.iteritems(d):".
(3/7)

@logilab

Show thread

Every time I see "open(filepath)", I change it into "io.open(filepath, 'rb')" to preserve behavior. This is fine most of the times; when it's not, it's usually because of some specific business logic you'll have to investigate on anyways. (Similarly, StringIO.StringIO -> io.BytesIO.)
(4/7)

@logilab

Show thread

Then, it's often time to dig further into "text versus binary data" issues. Clearly the most painful part of the process because it's often has to do with non-obvious business logic. Things get even worse when third party libraries (e.g. lxml) are in use. docs.python.org/3/howto/pyport

(5/7)

@logilab

Show thread

Finish with all oddities left like comparison of values of heterogeneous type ("1 < 'infinity'" is True on ) or silly things like ordering a list of dicts.
(6/7)

@logilab

Show thread

Once confident everything works fine with (tests suite is green, resolved all runtime errors spotted by, e.g., ): drop support.
(7/7)

@logilab

Show thread
Sign in to participate in the conversation
La Quadrature du Net - Mastodon - Media Fédéré

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!