Changes between Version 5 and Version 6 of Python3Support


Ignore:
Timestamp:
08/10/18 16:45:09 (6 years ago)
Author:
sanjeet
Comment:

Added dictionary difference table

Legend:

Unmodified
Added
Removed
Modified
  • Python3Support

    v5 v6  
    242242We already have encode and decode functions available in (from grass.script.utils import encode, decode) lib/python/script/utils.py that makes it easy for us to convert back and forth. To make it work with Python3, made changes in those functions to avoid syntax errors and exceptions.
    243243
     244**Dictionary Differences**
     245{{{#!th style="background: #ddd"
     246'''Python 2'''
     247}}}
     248{{{#!th style="background: #ddd"
     249'''Python 3'''
     250}}}
     251|-----------------------
     252{{{#!td
     253{{{
     254>>> d = {'a': 1, b'b': 2, u'c':3}
     255
     256>>>d['a']
     2571
     258>>>d[b'a']
     2591
     260>>>d[u'a']
     2611
     262
     263>>>d['b']
     2642
     265>>>d[b'b']
     2662
     267>>>d[u'b']
     2682
     269
     270>>>d['c']
     2713
     272>>>d[b'c']
     2733
     274>>>d[u'c']
     2753
     276}}}
     277}}}
     278{{{#!td
     279{{{
     280>>> d = {'a': 1, b'b': 2, u'c':3}
     281
     282>>>d['a']
     2831
     284>>>d[b'a']
     285KeyError: b'a'
     286>>>d[u'a']
     2871
     288
     289>>>d['b']
     290KeyError: 'b'
     291>>>d[b'b']
     2922
     293>>>d[u'b']
     294KeyError: 'b'
     295
     296>>>d['c']
     2973
     298>>>d[b'c']
     299KeyError: b'c'
     300>>>d[u'c']
     3013
     302}}}
     303}}}
     304|-----------------------
     305{{{#!th colspan=2
     306When special characters are involved:
     307}}}
     308|-----------------------
     309{{{#!td
     310{{{
     311>>> d = {'ř': 1, b'š': 2, u'ý':3}
     312
     313>>>d['ř']
     3141
     315>>>d[b'ř']
     3161
     317>>>d[u'ř']
     318KeyError: u '\u0159'
     319
     320>>>d['š']
     3211
     322>>>d[b'š']
     3231
     324>>>d[u'š']
     325KeyError: u '\u0161'
     326
     327>>>d['ý']
     328KeyError: '\xc3\xbd'
     329>>>d[b'ý']
     330KeyError: '\xc3\xbd'
     331>>>d[u'ý']
     3323
     333}}}
     334}}}
     335{{{#!td
     336{{{
     337>>> d = {'ř': 1, b's': 2, u'ý':3}
     338
     339>>>d['ř']
     3401
     341>>>d[b'ř']
     342SyntaxError
     343>>>d[u'ř']
     3441
     345
     346>>>d['s']
     347KeyError: 's'
     348>>>d[b's']
     3492
     350>>>d[u's']
     351KeyError: 's'
     352
     353>>>d['ý']
     3543
     355>>>d[b'ý']
     356SyntaxError
     357>>>d[u'ý']
     3583
     359}}}
     360}}}
     361
    244362== How to write Python 3 compatible code
    245363To check which Python version is being used, use sys.verson_info like: