Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisioneRevisione precedente
Prossima revisione
Revisione precedente
python:porting_python_2_to_pyton_3 [2020/01/22 16:40] – WIP apressatopython:porting_python_2_to_pyton_3 [2020/01/22 18:01] (versione attuale) – WIP apressato
Linea 1: Linea 1:
 ====== Porting da Python 2 a Python 3  ======  ====== Porting da Python 2 a Python 3  ====== 
  
-<WRAP center round important 60%> 
-***WIP***: Quello seguente è un articolo in divenire 
-</WRAP> 
  
  
Linea 936: Linea 933:
  
 ===== Standard library ===== ===== Standard library =====
 +==== dbm modules ====
 +<code python>
 +# Python 2 only
 +import anydbm
 +import whichdb
 +import dbm
 +import dumbdbm
 +import gdbm
  
 +# Python 2 and 3: alternative 1
 +from future import standard_library
 +standard_library.install_aliases()
  
-===== Modernize =====+import dbm 
 +import dbm.ndbm 
 +import dbm.dumb 
 +import dbm.gnu
  
 +# Python 2 and 3: alternative 2
 +from future.moves import dbm
 +from future.moves.dbm import dumb
 +from future.moves.dbm import ndbm
 +from future.moves.dbm import gnu
  
 +# Python 2 and 3: alternative 3
 +from six.moves import dbm_gnu
 +# (others not supported)
 +</code>
  
 +==== commands / subprocess modules ====
 +<code python>
 +# Python 2 only
 +from commands import getoutput, getstatusoutput
  
 +# Python 2 and 3
 +from future import standard_library
 +standard_library.install_aliases()
  
 +from subprocess import getoutput, getstatusoutput
 +</code>
  
 +==== subprocess.check_output() ====
 +<code python>
 +# Python 2.7 and above
 +from subprocess import check_output
  
 +# Python 2.6 and above: alternative 1
 +from future.moves.subprocess import check_output
 +
 +# Python 2.6 and above: alternative 2
 +from future import standard_library
 +standard_library.install_aliases()
 +
 +from subprocess import check_output
 +</code>
 +
 +==== Collections: Counter and OrderedDict ====
 +<code python>
 +# Python 2.7 and above
 +from collections import Counter, OrderedDict
 +
 +# Python 2.6 and above: alternative 1
 +from future.moves.collections import Counter, OrderedDict
 +
 +# Python 2.6 and above: alternative 2
 +from future import standard_library
 +standard_library.install_aliases()
 +
 +from collections import Counter, OrderedDict
 +</code>
 +
 +==== StringIO module ====
 +<code python>
 +# Python 2 only
 +from StringIO import StringIO
 +from cStringIO import StringIO
 +
 +# Python 2 and 3
 +from io import BytesIO
 +# and refactor StringIO() calls to BytesIO() if passing byte-strings
 +</code>
 +
 +==== http module ====
 +<code python>
 +# Python 2 only:
 +import httplib
 +import Cookie
 +import cookielib
 +import BaseHTTPServer
 +import SimpleHTTPServer
 +import CGIHttpServer
 +
 +# Python 2 and 3 (after ``pip install future``):
 +import http.client
 +import http.cookies
 +import http.cookiejar
 +import http.server
 +</code>
 +
 +==== xmlrpc module ====
 +<code python>
 +# Python 2 only:
 +import DocXMLRPCServer
 +import SimpleXMLRPCServer
 +
 +# Python 2 and 3 (after ``pip install future``):
 +import xmlrpc.server
 +</code>
 +
 +<code python>
 +# Python 2 only:
 +import xmlrpclib
 +
 +# Python 2 and 3 (after ``pip install future``):
 +import xmlrpc.client
 +</code>
 +
 +==== html escaping and entities ====
 +<code python>
 +# Python 2 and 3:
 +from cgi import escape
 +
 +# Safer (Python 2 and 3, after ``pip install future``):
 +from html import escape
 +
 +# Python 2 only:
 +from htmlentitydefs import codepoint2name, entitydefs, name2codepoint
 +
 +# Python 2 and 3 (after ``pip install future``):
 +from html.entities import codepoint2name, entitydefs, name2codepoint
 +</code>
 +
 +==== html parsing ====
 +<code python>
 +# Python 2 only:
 +from HTMLParser import HTMLParser
 +
 +# Python 2 and 3 (after ``pip install future``)
 +from html.parser import HTMLParser
 +
 +# Python 2 and 3 (alternative 2):
 +from future.moves.html.parser import HTMLParser
 +</code>
 +
 +==== urllib module ====
 +''urllib'' è uno dei moduli più complessi da usare in compatibilità fra 2 e 3.
 +
 +<code python>
 +# Python 2 only:
 +from urlparse import urlparse
 +from urllib import urlencode
 +from urllib2 import urlopen, Request, HTTPError
 +</code>
 +
 +<code python>
 +# Python 3 only:
 +from urllib.parse import urlparse, urlencode
 +from urllib.request import urlopen, Request
 +from urllib.error import HTTPError
 +</code>
 +
 +<code python>
 +# Python 2 and 3: easiest option
 +from future.standard_library import install_aliases
 +install_aliases()
 +
 +from urllib.parse import urlparse, urlencode
 +from urllib.request import urlopen, Request
 +from urllib.error import HTTPError
 +</code>
 +
 +<code python>
 +# Python 2 and 3: alternative 2
 +from future.standard_library import hooks
 +
 +with hooks():
 +    from urllib.parse import urlparse, urlencode
 +    from urllib.request import urlopen, Request
 +    from urllib.error import HTTPError
 +</code>
 +
 +<code python>
 +# Python 2 and 3: alternative 3
 +from future.moves.urllib.parse import urlparse, urlencode
 +from future.moves.urllib.request import urlopen, Request
 +from future.moves.urllib.error import HTTPError
 +</code>
 +
 +<code python>
 +# Python 2 and 3: alternative 4
 +try:
 +    from urllib.parse import urlparse, urlencode
 +    from urllib.request import urlopen, Request
 +    from urllib.error import HTTPError
 +except ImportError:
 +    from urlparse import urlparse
 +    from urllib import urlencode
 +    from urllib2 import urlopen, Request, HTTPError
 +</code>
 +
 +==== Tkinter ====
 +<code python>
 +# Python 2 only:
 +import Tkinter
 +import Dialog
 +import FileDialog
 +import ScrolledText
 +import SimpleDialog
 +import Tix
 +import Tkconstants
 +import Tkdnd
 +import tkColorChooser
 +import tkCommonDialog
 +import tkFileDialog
 +import tkFont
 +import tkMessageBox
 +import tkSimpleDialog
 +import ttk
 +
 +# Python 2 and 3 (after ``pip install future``):
 +import tkinter
 +import tkinter.dialog
 +import tkinter.filedialog
 +import tkinter.scrolledtext
 +import tkinter.simpledialog
 +import tkinter.tix
 +import tkinter.constants
 +import tkinter.dnd
 +import tkinter.colorchooser
 +import tkinter.commondialog
 +import tkinter.filedialog
 +import tkinter.font
 +import tkinter.messagebox
 +import tkinter.simpledialog
 +import tkinter.ttk
 +</code>
 +
 +==== socketserver ====
 +<code python>
 +# Python 2 only:
 +import SocketServer
 +
 +# Python 2 and 3 (after ``pip install future``):
 +import socketserver
 +</code>
 +
 +==== copy_reg, copyreg ====
 +<code python>
 +# Python 2 only:
 +import copy_reg
 +
 +# Python 2 and 3 (after ``pip install future``):
 +import copyreg
 +</code>
 +
 +==== configparser ====
 +<code python>
 +# Python 2 only:
 +from ConfigParser import ConfigParser
 +
 +# Python 2 and 3 (after ``pip install configparser``):
 +from configparser import ConfigParser
 +</code>
 +
 +==== queue ====
 +<code python>
 +# Python 2 only:
 +from Queue import Queue, heapq, deque
 +
 +# Python 2 and 3 (after ``pip install future``):
 +from queue import Queue, heapq, deque
 +</code>
 +
 +==== repr, reprlib ====
 +<code python>
 +# Python 2 only:
 +from repr import aRepr, repr
 +
 +# Python 2 and 3 (after ``pip install future``):
 +from reprlib import aRepr, repr
 +</code>
 +
 +==== UserDict, UserList, UserString ====
 +<code python>
 +# Python 2 only:
 +from UserDict import UserDict
 +from UserList import UserList
 +from UserString import UserString
 +
 +
 +# Python 3 only:
 +from collections import UserDict, UserList, UserString
 +
 +
 +# Python 2 and 3: alternative 1
 +from future.moves.collections import UserDict, UserList, UserString
 +
 +
 +# Python 2 and 3: alternative 2
 +from six.moves import UserDict, UserList, UserString
 +
 +
 +# Python 2 and 3: alternative 3
 +from future.standard_library import install_aliases
 +install_aliases()
 +from collections import UserDict, UserList, UserString
 +</code>
 +
 +==== itertools: filterfalse, zip_longest ====
 +<code python>
 +# Python 2 only:
 +from itertools import ifilterfalse, izip_longest
 +
 +# Python 3 only:
 +from itertools import filterfalse, zip_longest
 +
 +# Python 2 and 3: alternative 1
 +from future.moves.itertools import filterfalse, zip_longest
 +
 +# Python 2 and 3: alternative 2
 +from six.moves import filterfalse, zip_longest
 +
 +# Python 2 and 3: alternative 3
 +from future.standard_library import install_aliases
 +install_aliases()
 +from itertools import filterfalse, zip_longest
 +</code>
 +
 +
 +
 +===== Python-Modernize =====
 +
 +==== Scopo del progetto ====
 +Questa libreria è un wrapper attorno a ''lib2to3'' che serve a rendere il codice Python 2 più moderno in ottica di portarlo su Python 3.
 +
 +Il comando python-modernize funziona come 2to3. Ecco come riscrivere un singolo file:
 +
 +<code bash>
 +python-modernize -w example.py
 +</code>
 +
 +Il sito Web del progetto è disponibile su [[https://github.com/python-modernize/python-modernize|GitHub]] e il nome del progetto PyPI è [[https://pypi.python.org/pypi/modernize|modernize]]
 +
 +==== Una nota sulla gestione delle stringhe ====
 +  * Di default modernize non cambia le stringhe Unicode. \\ Questa è l'opzione più semplice se vuoi supportare Python 3.x e versioni successive insieme a Python 2.
 +  * In alternativa, c'è il flag ''**--six-unicode**'' che avvolgerà le stringhe Unicode con la funzione helper **six.u()** usando il fixer contenuto in ''libmodernize.fixes.fix_unicode''.\\ Ciò è utile se si desidera supportare Python 3.1 e Python 3.2 senza grandi cambiamenti.
 +  * L'ultima alternativa è il flag ''**--future-unicode**'' che importa gli ''unicode_literals'' dal modulo **<nowiki>__future__</nowiki>** usando il fixer contenuto in ''libmodernize.fixes.fix_unicode_future''.\\ Ciò richiede Python 2.6 e versioni successive e richiederà di contrassegnare le bytestring con b '' e le stringhe native in str('') o qualcosa di simile che sopravviva alla trasformazione.
 +
 +==== Preparazione ====
 +Dal prompt dei comandi lanciare
 +<code bash>
 +pip --install modernize
 +</code>
 +Al termine dell'installazione avrete a disposizione l'eseguibile **python-modernize**
 +
 +==== Batch di conversione ====
 +<code batch>
 +set mydatetime=%date:~-4%%date:~-7,2%%date:~-10,2%_%time:~0,2%%time:~3,2%%time:~6,2%
 +set logpath=.\Logs
 +set logfile=%logpath%\Migrate_%Application%_2to3_%mydatetime%.log
 +set RepoPath=path\to\your \web2py\applications
 +set Application=Your_Folder_App_Name
 +
 +IF NOT EXIST %logpath% (
 +  MKDIR %logpath%
 +)
 +
 +(FOR /f "delims=" %%a IN ('DIR %RepoPath%\%Application%\*.pyc /b /s') do (del %%a))
 +(FOR /f "delims=" %%a IN ('DIR %RepoPath%\%Application%\*.py /b /s') do (python-modernize -w %%a >> %LogFile%))
 +(FOR /f "delims=" %%a IN ('DIR %RepoPath%\%Application%\*.bak /b /s') do (del %%a))
 +</code>
  
  
Linea 955: Linea 1313:
   - [[https://www.geeksforgeeks.org/important-differences-between-python-2-x-and-python-3-x-with-examples/]]   - [[https://www.geeksforgeeks.org/important-differences-between-python-2-x-and-python-3-x-with-examples/]]
   - [[https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html]]   - [[https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html]]
 +  - [[https://python-modernize.readthedocs.io/en/latest/]]
  
  
python/porting_python_2_to_pyton_3.1579711208.txt.gz · Ultima modifica: 2020/01/22 16:40 da apressato
Torna su
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0