Leo's fundamental unicode-related functions, g.toUnicode and 
g.toEncodedString have recently collapsed in complexity.  This all got 
started by a recent bug report that the unicode function does not exist in 
Python 3.

The Aha was triggered by this Q & A answer from stackoverflow:
http://stackoverflow.com/questions/14472650/python-3-encode-decode-vs-bytes-str

Q:  There are 2 ways to [encode & decode]:
u'something'.encode('utf-8') will generate b'bytes', but so does 
bytes(u'something', 'utf-8').
And b'bytes'.decode('utf-8') seems to do same thing as str(b'', 'utf-8'). 

Why are there 2 methods that seem to do the same thing, and is either 
better than the other?

A: Neither is better than the other, they do exactly the same thing. 
However, using .encode() and .decode() is the more common way to do it. It 
is also compatible with Python 2.

The answer created an Aha.  Using decode instead of unicode/str will 
simplify the code because no tests for g.isPython3 is necessary.  Doh!

Furthermore, I *finally* found a way to remember the difference between 
encode & decode.  Obviously, encode produces and encoded string, that is, a 
list of bytes (in a particular encoding), so decode must do the reverse, 
which means decode produces unicode.  So simple, but I've been confused for 
years.

The result is remarkable: compare the old and new versions of g.toUnicode 
and g.toEncodedString.  The old, horrible, g.reportBadChars is gone, 
replaced by::

    s = s.encode(encoding,"replace") # in g.toEncodedString
    s = s.decode(encoding,'replace') # in g.toUnicode

So the old, clueless, risible code is finally gone and at last. Unicode 
wonks will conclude that Leo actually handles unicode properly.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to