module pycode.py3to2

Inheritance diagram of pyquickhelper.pycode.py3to2

Short summary

module pyquickhelper.pycode.py3to2

Helper to convert a script written in Python 3 to Python 2

source on GitHub

Classes

class

truncated documentation

Convert3to2Exception

exception raised for an exception happening during the conversion

Functions

function

truncated documentation

py3to2_convert

converts a script into from python 3 to python 2

py3to2_convert_tree

Converts files in a folder and its subfolders from python 3 to python 2, the function only considers python script (verifying …

py3to2_future

checks that import from __future__ import unicode_literals is always present, the function assumes it is a python …

py3to2_imported_local_modules

See function py3to2_convert_tree() and documentation about parameter unittest_modules.

py3to2_remove_raise_from

Removes expression such as: raise Exception ("...") from e. The function is very basic. It should be done with a …

Documentation

Helper to convert a script written in Python 3 to Python 2

source on GitHub

exception pyquickhelper.pycode.py3to2.Convert3to2Exception[source][source]

Bases: Exception

exception raised for an exception happening during the conversion

source on GitHub

pyquickhelper.pycode.py3to2.py3to2_convert(script, unittest_modules)[source][source]

converts a script into from python 3 to python 2

Parameters
  • script – script or filename

  • unittest_modules – modules used during unit test but not installed, py3to2_convert_tree

Returns

string

See see @fn py3to2_convert_tree for more information.

source on GitHub

pyquickhelper.pycode.py3to2.py3to2_convert_tree(folder, dest, encoding='utf8', pattern='.*[.]py$', pattern_copy='.*[.]((ico)|(7z)|(dll)|(so)|(yml)|(rst)|(ipynb)|(gif)|(jpg)|(jpeg)|(png)|(txt)|(zip)|(gz)|(html)|(exe)|(js)|(css)|(tex)|(data)|(csv)|(tpl))$', unittest_modules=None, fLOG=<function noLOG>)[source][source]

Converts files in a folder and its subfolders from python 3 to python 2, the function only considers python script (verifying pattern).

Parameters
  • folder – folder

  • dest – destination

  • encoding – all files will be saved with this encoding

  • pattern – pattern to find source code

  • pattern_copy – copy these files, do not modify them

  • fLOG – logging function

  • unittest_modules – modules used during unit tests but not installed

Returns

list of copied files

If a folder does not exists, it will create it. The function excludes all files in subfolders starting by dist, _doc, build, extensions, nbextensions. The function also exclude subfolders inside subfolders following the pattern ut_.*.

There are some issues difficult to solve with strings. Python 2.7 is not friendly with strings. Some needed pieces of code:

if sys.version_info[0]==2:
    from codecs import open

You can also read blog post 2015-04-17 Producing a version for Python 2.7 from Python 3.

The variable unittest_modules indicates the list of modules which are not installed in Python distribution but still used and placed in the same folder as the same which has to converted.

unittest_modules can be either a list or a tuple (module, alias). Then the alias appears instead of the module name.

The function does not convert the exception FileNotFoundError which only exists in Python 3. The module will fail in version 2.7 if this exception is raised.

The following page Cheat Sheet: Writing Python 2-3 compatible code gives the difference between the two versions of Python and how to write compatible code.

source on GitHub

pyquickhelper.pycode.py3to2.py3to2_future(content)[source][source]

checks that import from __future__ import unicode_literals is always present, the function assumes it is a python code

Parameters

content – file content

Returns

new content

source on GitHub

pyquickhelper.pycode.py3to2.py3to2_imported_local_modules(content, unittest_modules)[source][source]

See function py3to2_convert_tree and documentation about parameter unittest_modules.

Parameters
  • content – script or filename

  • unittest_modules – modules used during unit test but not installed, py3to2_convert_tree

source on GitHub

pyquickhelper.pycode.py3to2.py3to2_remove_raise_from(content)[source][source]

Removes expression such as: raise Exception ("...") from e. The function is very basic. It should be done with a grammar.

Parameters

content – file content

Returns

script

source on GitHub