module pycode.py3to2
¶
Short summary¶
module pyquickhelper.pycode.py3to2
Helper to convert a script written in Python 3 to Python 2
Classes¶
class |
truncated documentation |
---|---|
exception raised for an exception happening during the conversion |
Functions¶
function |
truncated documentation |
---|---|
converts a script into from python 3 to python 2 |
|
Converts files in a folder and its subfolders from python 3 to python 2, the function only considers python script (verifying … |
|
checks that import |
|
See function |
|
Removes expression such as: |
Documentation¶
Helper to convert a script written in Python 3 to Python 2
- exception pyquickhelper.pycode.py3to2.Convert3to2Exception[source]¶
Bases:
Exception
exception raised for an exception happening during the conversion
- pyquickhelper.pycode.py3to2.py3to2_convert(script, unittest_modules)[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.
- 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)|(tmpl))$', unittest_modules=None, fLOG=<function noLOG>)[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 patternut_.*
.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.
- pyquickhelper.pycode.py3to2.py3to2_future(content)[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
- pyquickhelper.pycode.py3to2.py3to2_imported_local_modules(content, unittest_modules)[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