converting to python 3, venting

Posted on
Tue Feb 04, 2020 1:06 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

converting to python 3, venting

just have to vent a bit.. some of my pibeacon raspberry code i need to convert to python 3
the new python 3 code should call the existing functions..

challenges eg: .find .strip, .split

use : theText.split("\n") does not work anymore
use split(b"\n")
that also seems to work with python2

eg:
Code: Select all
cmd= "ps -ef | grep  '.py' | grep -v grep"
ret = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
lines = ret.split(b"\n")

works
w/o the b infront of the "\n" it complains


and
import Queue
is now
import queue

and
soc.send(theText)
is now
soc.send(bytes(theText,"utf8"))


and unicode("some text") not good
etc etc

Do we want to go to python 3 in indigo?

That would be a major undertaking I guess.


BUT I like to write code from here on that works under 2 and 3 .. any pointers to some GOOD lectures?


Karl

Posted on
Tue Feb 04, 2020 4:11 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: converting to python 3, venting

I haven't looked closely at it, but the six library is supposed to help with some of those kinds of compatibility issues. I believe Python 2.7 contained some explicit compatibility things to help with migrating to Python 3 so you might want to do some research there as well.

Moving to Python 3 will be a non-trivial exercise because of how we bridge our Objective-C++ code to Python, though we know we'll have to do it at some point. So it's a question of when (not soon) not if. That's why I've started posting about the differences here and there on the forums.

I'm in the process of moving our backend systems to Python 3 and Django 2 (as part of our OAuth 2.0 implementation) and while the Python change is somewhat tedious, it's really not that bad (the Django move is far more troublesome but I think that's finally worked out as well).

When it comes to imports, you can always use this pattern:

Code: Select all
try:
    import Queue  # try the Python 2 import
except:
    import queue as Queue # try the Python 3 import


Of course, that will only work if the methods in the library are named the same and have the same params.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Feb 04, 2020 4:48 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: converting to python 3, venting

Have you tried running 2to3 on your code? https://docs.python.org/2/library/2to3.html

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Feb 04, 2020 7:46 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: converting to python 3, venting

My problem is not so much the syntax and function defs ( Queue and queue is ok) but the Unicode- bytes - strings.
Some of the functions return bytes some .. strings
Then split() .. does not work

Ie what does subprocess.popen Return

...
Karl




Sent from my iPhone using Tapatalk

Posted on
Fri Feb 07, 2020 10:56 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: converting to python 3, venting

this solved 90% of my problems:
Code: Select all
ret = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]

subprocess seems to return a byte format under python3 adding .decode('utf-8') converts it into text, then the rest of the code works fine.
Code: Select all
ret = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0].decode('utf-8')

before eg \n became some byte thingy shown as " \\n" , so
ret.split("\n")
Did not work anymore adding .decode('utf-8') solves that

Reading regular ascii files w f= open().. f.read() seems to return simple ascii.

Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests