Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit d5f0bb17 authored by paugier's avatar paugier
Browse files

2to3

parent 544d558e
......@@ -824,7 +824,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.7.2"
}
},
"nbformat": 4,
......
......@@ -211,7 +211,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.7.2"
}
},
"nbformat": 4,
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Port Python 2 code to Python 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once upon a time, it was very difficult to:\n",
"\n",
"- convert code written in Python 2\n",
"\n",
"- write code compatible for py2 and py3\n",
"\n",
"Dependencies + strong language incompatibilities (between Python 2.6 and 3.3).\n",
"\n",
"\n",
"Now, it much simpler!\n",
"\n",
"- main packages all work in Python 3\n",
"\n",
"- smaller language incompatibility (between Python 2.7 and >3.4) (for example `from __future__ import print_function, division`)\n",
"\n",
"- good tools to write compatible code ([six](https://pypi.org/project/six/) and [future](https://python-future.org))\n",
"\n",
"But... still some differences... For example for us in sciences:\n",
"\n",
"- division `int / int`\n",
"\n",
"- str are unicode in Python 3 but not in Python 2\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Steps to port Python 2 code to Python 3\n",
"\n",
"- Solidify the Python 2 code (write tests!)\n",
"\n",
"- Use [pyupgrade](https://github.com/asottile/pyupgrade) to modernize the code to Python 2.7 ➡️ test!\n",
"\n",
"- Use the automatic tool `futurize -w mymodule.py` (provided by [future](https://python-future.org)) ➡️ test!\n",
"\n",
"Today, it can make sense to stop supporting Python 2.7 (and even Python 3.5). See https://python3statement.org/. \n",
"\n",
"Main advantage: One can use nice modern features, for example:\n",
"\n",
"- f-strings\n",
"\n",
"- `from pathlib import Path`\n",
"\n",
"It can be done automatically with pyupgrade:\n",
"\n",
"`find -name \"*.py\" | xargs pyupgrade --py36-plus`\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment