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 17:04] – 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 1266: Linea 1263:
  
  
-===== Modernize =====+===== 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 1283: 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.1579712696.txt.gz · Ultima modifica: 2020/01/22 17:04 da apressato
Torna su
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0