Changes between Version 2 and Version 3 of Python3Support


Ignore:
Timestamp:
May 21, 2018, 6:53:37 PM (6 years ago)
Author:
wenzeslaus
Comment:

more reasoning for Unicode (or not)

Legend:

Unmodified
Added
Removed
Modified
  • Python3Support

    v2 v3  
    1212
    1313== Python Scripting Library ==
     14
     15What to consider:
     16
     17* The API is used not only by the GRASS Development Team (core devs) but in general, e.g. by writing addons or custom user scripts.
     18 * Maybe the core devs can be convinced to follow certain special practices for the core modules, but it doesn't seem realistic that addon contributors will follow them if there are too distant from what is standard for the language (less serious example is requiring PEP8 conventions versus some custom ones).
     19 * The purpose of the API is to make it simple for people to use and extend GRASS GIS.
     20* Trained (and even the non-trained) Python 3 programmers will expect API to behave in the same way as the standard library and language in general.
     21 * One writes `os.environ['PATH']`, not `os.environ[b'PATH']` nor `os.environ[u'PATH']`.
     22 * GUI needs Unicode at the end.
     23
    1424Possible approach:
    1525* functions need to accept unicode and return unicode
     
    3646    line = item.split(b'|')[3:]
    3747}}}
     48
     49Unicode as the default type in the API, e.g. for keys, but also for many values, is supported by Unicode being the default string literal type in Python 3. API users will expect that expressions such as hypothetical `computation_region['north']` will work. Unlike in Python 2, there is a difference in Python 3 between `computation_region[u'north']` and `computation_region[b'north']`. See comparison of dictionary behavior in 2 and 3:
     50
     51{{{
     52#!python
     53# Python 2
     54>>> d = {'a': 1, b'b': 2}
     55>>> d['b']
     562
     57>>> d[u'b']
     582
     59>>> # i.e. no difference between u'' and b'' keys
     60>>> and that applies for creating also:
     61>>> d = {u'a': 1, b'a': 2}
     62>>> d['a']
     632
     64>>> # because
     65>>> d
     66{u'a': 2}
     67# Python 3
     68>>> # unlike in 2, we get now two entries:
     69>>> d = {'a': 1, b'a': 2}
     70>>> d
     71{b'a': 2, 'a': 1}
     72>>> d['a']
     731
     74>>> d[b'a']
     752
     76>>> # it becomes little confusing when we combine unicode and byte keys
     77>>> d = {'a': 1, b'b': 2}
     78>>> d['a']
     791
     80>>> d['b']
     81Traceback (most recent call last):
     82  File "<stdin>", line 1, in <module>
     83KeyError: 'b'
     84>>> d[b'b']
     852
     86>>> # in other words, user needs to know and specify the key as bytes
     87}}}