Neat way to port nonpolyglot code from Py2 to Py3

非 Y 不嫁゛ 提交于 2020-07-16 04:26:45

问题


In many of my python modules, I am using

from itertools import izip_longest

But now I am moving my codebase to Py3 (compatible with Py2 at the same time). And in Py3 izip_longest is renamed to zip_longest. One answer to fix this is recommended in Failing to import itertools in Python 3.5.2, i.e. to change import statement to following.

try:
    # Python 3
    from itertools import zip_longest as izip_longest
except ImportError:
    # Python 2
    from itertools import izip_longest 

However, changing this in 20+ modules looks a bit odd to me. What is the neat way of doing this?


回答1:


You can wrap that exact code inside a module of your own, say itertools_compat.py, and just write

from itertools_compat import izip_longest

wherever you need this function.




回答2:


Making python2 code work with python3 is not for free. And there is a lot of grunt work involved. I think it is difficult to avoid this and in any case you have to do proper testing to be sure your code is working with both versions.

I don't know your code and your project, it's exact contents, whether this project should stay alive for much longer or if you just want to make it survive for a little longer. Thus I'm not sure what's best in your scenario.

In general I would suggest to change your code such, that it looks like python3 code and still runs with python2 and not to write code that looks like python2 code but also runs with python3. (But all depends on your context)

You might want to try out the package future https://python-future.org/ Which provides helpers for writing code, that's running with both versions.

future also contains tools, that try to automatically change code in a way, that it is more likely to run with both versions. Depending on the complexity of your code you still have to do quite some things manually (especially for unicode issues)

The commands are called futurize (make code look like python3 but run with python2) or pasteurize (make code look like python2 but run also with python3)

make backups of all your files (or use some version control like git) before trying this out.

The simple use case is futurize --stage1 yourfile.py

There is one interesting chapter / cheatsheet with many examples, that is worth reading https://python-future.org/compatible_idioms.html?highlight=zip

If you do not want to use the future package or if you encounter situations, that future doesn't handle well I would write an adapter module and use that in your code.

e.g. py2py3compat.py:

try: 
    # Python 3 
    from itertools import zip_longest
except ImportError: 
    # Python 2 
    from itertools import izip_longest as zip_longest

yourpyfile.py:

from py2py3compat import zip_longest

and perform a global search and replace to replace izip_longest with zip_longest




回答3:


I see your other question got closed so I'll post here. So, there's a couple files, and naming is important!

fixer.py

#!/usr/bin/env python3
# you'll need 2to3 and python-modernize installed
from __future__ import absolute_import
import sys
from lib2to3.main import main
import libmodernize

sys.path.append(".")
sys.exit(main('fixers'))

fixers/fix_iziplongest.py

from lib2to3 import fixer_base
import libmodernize

class FixIziplongest(fixer_base.BaseFix):
    PATTERN = """
    power< 'izip_longest' trailer< '(' any* ')' > >
    | import_from< 'from' 'itertools' 'import' 'izip_longest' >
    """

    # This function is only called on matches to our pattern, which should
    # be usage of izip_longest, and the itertools import
    def transform(self, node, results):
        # add the new import (doesn't matter if we do this multiple times
        libmodernize.touch_import('itertools_compat', 'zip_longest', node)
        # remove the old import
        if node.type == syms.import_from:
            node.parent.remove()
            return node
        
        # rename to izip_longest
        node.children[0].value = 'zip_longest'
        return node

Usage is the same as 2to3 - python ./fixer.py -f iziplongest file_to_fix.py (more flags if you want it to apply changes, that'll just show the diff) So, what this does is it would covert this:

from itertools import izip_longest
for x in izip_longest(a, b):
     print(x)

To this:

from itertools_compat import zip_longest
for x in zip_longest(a, b):
     print(x)


来源:https://stackoverflow.com/questions/62521462/neat-way-to-port-nonpolyglot-code-from-py2-to-py3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!