​​On Wed, Mar 30, 2016 at 10:39 AM, Edward K. Ream <[email protected]>
wrote:


> actually getting code to work on 2 and 3 can be fairly tricky.  My
> preferred way is to do:
>
> if g.isPython3:
>     # Python 3-specific code.
> else:
>     # Python 2-specific code.
>

*​Important*: leoGlobals​.py has a number of convenience functions that
make these tests automatically. You should be particularly aware of:

- g.isBytes, g.isInt, g.isString, g.isUnicode.
- g.toUnicode, g.toEncodedString, g.isValidEncoding.
- g.u, g.ue.

If you use the above methods there should never be a need for direct calls
to unicode(s), encode(s) or decode(s).  The above methods are clearer,
safer, and do better error checking and recovery.

I don't have time to go into details now, but I'll be doing a post on there
for devs.  It will also be part of Leo's code academy.

A few other functions that should be on your radar:

- g.readFileIntoEncodedString
- s, e = g.readFileIntoString
- g.stripBOM

There may be cases where direct tests against g.isPython3 seem the
clearest. That's fine, but usually you can avoid direct tests.

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 https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to