Changes between Version 6 and Version 7 of Python3Support


Ignore:
Timestamp:
Aug 11, 2018, 7:44:51 PM (6 years ago)
Author:
sanjeet
Comment:

Added compatible code table

Legend:

Unmodified
Added
Removed
Modified
  • Python3Support

    v6 v7  
    370370}}}
    371371
     372{{{#!th style="background: #ddd"
     373'''Label'''
     374}}}
     375{{{#!th style="background: #ddd"
     376'''Python 2'''
     377}}}
     378{{{#!th style="background: #ddd"
     379'''Python 3'''
     380}}}
     381{{{#!th style="background: #ddd"
     382'''Python 2 and 3 compatible solution'''
     383}}}
     384|-----------------------
     385{{{#!td
     386Strings -
     387bytes/str/unicode
     388}}}
     389{{{#!td
     390}}}
     391{{{#!td
     392}}}
     393{{{#!td
     394{{{
     395Use decode and encode functions from grass.script.utils
     396}}}
     397}}}
     398|-----------------------
     399{{{#!td
     400String functions
     401}}}
     402{{{#!td
     403{{{
     404String.split
     405String.join
     406}}}
     407}}}
     408{{{#!td
     409{{{
     410"".split(), "".join(" ")
     411}}}
     412}}}
     413{{{#!td
     414Use functions as in Python 3
     415}}}
     416|-----------------------
     417{{{#!td
     418String types
     419}}}
     420{{{#!td
     421{{{
     422Types.StringTypes
     423Types.Stringtype
     424}}}
     425}}}
     426{{{#!td
     427{{{
     428str or unicode
     429str
     430}}}
     431}}}
     432{{{#!td
     433Use direct types like str, unicode, bytes as:
     434{{{
     435if sys.version_info.major == 3:
     436    unicode = str
     437else:
     438    bytes = str
     439}}}
     440}}}
     441|-----------------------
     442{{{#!td
     443ps.communicate():
     444stdout, stderror
     445}}}
     446{{{#!td
     447}}}
     448{{{#!td
     449}}}
     450{{{#!td
     451Use decode:
     452{{{
     453from grass.script.utils import decode
     454
     455stdout = decode(stdout)
     456stderr = decode(stdout)
     457}}}
     458}}}
     459|-----------------------
     460{{{#!td
     461Opening files
     462}}}
     463{{{#!td
     464{{{
     465fileName = file(“example.txt”, w)
     466}}}
     467}}}
     468{{{#!td
     469{{{
     470fileName = open(“example.txt”, w)
     471}}}
     472}}}
     473{{{#!td
     474Replace file() with open() to open files
     475}}}
     476|-----------------------
     477{{{#!td
     478Filter function
     479}}}
     480{{{#!td
     481{{{
     482filter(a_function, a_sequence)
     483}}}
     484Filter returns a list
     485}}}
     486{{{#!td
     487{{{
     488filter(a_function, a_sequence)
     489}}}
     490Filter returns an iterator, not a list
     491}}}
     492{{{#!td
     493Explicit conversion to list using:
     494{{{
     495list( filter(a_function, a_sequence) )
     496}}}
     497}}}
     498|-----------------------
     499{{{#!td
     500Urlopen proxies
     501}}}
     502{{{#!td
     503}}}
     504{{{#!td
     505}}}
     506{{{#!td
     507Create proxy handler:
     508https://docs.python.org/3.0/library/urllib.request.html#urllib.request.ProxyHandler
     509
     510Read more:
     511http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#urllib
     512}}}
     513|-----------------------
     514{{{#!td
     515Import errors
     516}}}
     517{{{#!td
     518}}}
     519{{{#!td
     520}}}
     521{{{#!td
     522Use try/except to catch Import errors and deal accordingly
     523}}}
     524|-----------------------
     525{{{#!td
     526Relative Imports
     527}}}
     528{{{#!td
     529{{{
     530import example
     531from x import y
     532}}}
     533}}}
     534{{{#!td
     535{{{
     536from . import example
     537from .x import y
     538}}}
     539}}}
     540{{{#!td
     541Use period (.) in import calls
     542}}}
     543|-----------------------
     544{{{#!td
     545Dictionary methods
     546}}}
     547{{{#!td
     548{{{
     549a_dictionary.keys()
     550a_dictionary.items()
     551a_dictionary.iterkeys()
     552}}}
     553}}}
     554{{{#!td
     555{{{
     556list(a_dictionary.keys())
     557list(a_dictionary.items())
     558iter(a_dictionary.keys())
     559}}}
     560}}}
     561{{{#!td
     562Read more:
     563http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#dict
     564}}}
     565|-----------------------
     566{{{#!td
     567Other dictionary iterators
     568}}}
     569{{{#!td
     570{{{
     571a_dictionary.iterkeys()
     572a_dictionary.iteritems()
     573a_dictionary.itervalues()
     574}}}
     575}}}
     576{{{#!td
     577{{{
     578six.iterkeys(a_dictionary)
     579six.iteritems(a_dictionary)
     580six.itervalues(a_dictionary)
     581}}}
     582}}}
     583{{{#!td
     584Use function from six library
     585}}}
     586|-----------------------
     587{{{#!td
     588Map list
     589}}}
     590{{{#!td
     591{{{
     592map(x, y)
     593}}}
     594Returns list
     595}}}
     596{{{#!td
     597{{{
     598map(x,y)
     599}}}
     600Returns iterator
     601}}}
     602{{{#!td
     603{{{
     604list(map(x,y))
     605}}}
     606Use list to wrap around map
     607}}}
     608
    372609=== Other recommendations:
    373610Use .format specifier for the strings and parameters.