sendFile.py ported to Python 3

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
Post Reply
User avatar
Marco
Posts: 24
Joined: 08 Dec 2019, 15:33

sendFile.py ported to Python 3

Post by Marco » 20 Dec 2019, 07:23

HI all,

I have ported sendFile.py to Python 3. Support for Python 2 will end soon, anyway Python 3 is the default on my Linux machines and it's annoying (to say the least) having to switch between Python versions.

It's renamed to sendFile3.py (no surprise), that is on Github in Contrib/Crouze/

  • I have tested it with Python 3.8.0, on Linux (Arch Linux x86_64).
  • Both sending .gt1 and .gtb works well.
  • Not yet tested with other Python 3.x releases, and on other platforms.
  • It is not backwards compatible with Python 2! It may seem at first, but expect failures.
  • Additionally, I took the liberty to add the truncation result to the stdout output, when a BASIC line exceeds 25 characters.

Give it a try if you want. Of course the necessary libraries suitable for Python 3 must be installed. On my Arch Linux they already were and it worked out of the box. I'm not really a Python expert, so expect limited support ;)

Cheers,
Marco

User avatar
marcelk
Posts: 490
Joined: 13 May 2018, 08:26

Re: sendFile.py ported to Python 3

Post by marcelk » 22 Dec 2019, 10:39

Thanks. As Python's 2.7 supports indeed ends soon, we can safely switch from 2 to 3 without even considering backwards compatibility. Somebody started on it already, but got stuck. So this will be something for under the x-mas tree.

I note you didn't have to change the hash-bang line:

Code: Select all

#!/usr/bin/env python
But my systems here (macOS 10.15 Catalina and Raspbian) have both 'python' and 'python3'. Does your system also have 'python3' in its path, or just 'python'? Because I plan to update all such lines to this:

Code: Select all

#!/usr/bin/env python3

User avatar
Marco
Posts: 24
Joined: 08 Dec 2019, 15:33

Re: sendFile.py ported to Python 3

Post by Marco » 22 Dec 2019, 11:03

Hi Marcel,

As far as I am aware (but again, not a Python expert) the shebang "/usr/bin/env python" line is only applicable when more than one Python version is installed (which I do have) for the interpreter to use the first Python in the PATH environment variable. I do not have Python in my PATH, so for me the shebang line does nothing, it's only there for compatibility purposes (and because you are "supposed" to start a Python script with this declaration).

In my case both Python 2.7 and 3.8 binaries are in /usr/bin, and symlinks take care of versioning. That's the way Arch Linux works. If I want to use Python 2.7 I can either change the Python symlink to point to 2.7, replace the shebang line in the script to /usr/bin/python2, or other workarounds. None are very neat, but nothing is when it comes to Python versions running side-by-side. It will be a great relief when (if) there is only one Python release left.

Cheers,
Marco

User avatar
marcelk
Posts: 490
Joined: 13 May 2018, 08:26

Re: sendFile.py ported to Python 3

Post by marcelk » 22 Dec 2019, 11:29

The hash-bang line is there so you don't have to type 'python script.py blah...', but instead you can just do

Code: Select all

Utils/sendFile.py Apps/Overworld/Overworld.gt1
You also need the executable bit set. Your version doesn't have that, so maybe you don't use this at all. It's a Unix thing, not a Python thing.

It would be annoying if the 'python' command remains ambiguous across systems, while at the same time the 'python3' command isn't universally available. But then, this transition pain has been going on for more than a decade now. No doubt it will hurt for a couple more years to come.

User avatar
Marco
Posts: 24
Joined: 08 Dec 2019, 15:33

Re: sendFile.py ported to Python 3

Post by Marco » 22 Dec 2019, 15:47

marcelk wrote:
22 Dec 2019, 11:29
The hash-bang line is there so you don't have to type 'python script.py blah...', but instead you can just do

Code: Select all

Utils/sendFile.py Apps/Overworld/Overworld.gt1
That is true of course, and that's exactly the way I run the script. And yes I need to chmod the file too to set the executable bit, for it to work.

User avatar
marcelk
Posts: 490
Joined: 13 May 2018, 08:26

Re: sendFile.py ported to Python 3

Post by marcelk » 22 Dec 2019, 16:20

I just adopted the changes to Utils/sendFile.py. It now requires Python 3. As far as I'm concerned, at this point the Python migration is one-way, without looking back.

I refactored a bit as well, so I hope I didn't break the world. Most importantly, the port detection now uses pySerial's built-in comports() function. This should benefit Windows users the most.

User avatar
Marco
Posts: 24
Joined: 08 Dec 2019, 15:33

Re: sendFile.py ported to Python 3

Post by Marco » 22 Dec 2019, 17:58

Port detection didn't work for me (on Linux), I connect to ttyACM0 and that one didn't pass the autodetect test. Appending line #131 did the trick:

Code: Select all

autoDetect = ('Arduino', 'FT232R', 'SparkFun', 'ttyACM')
Although this is maybe what you not wanted to do in the first place, and I guess tty.usbmodem should then be added as well?

comports() for me only lists ttyS0 and ttyACM0...

User avatar
marcelk
Posts: 490
Joined: 13 May 2018, 08:26

Re: sendFile.py ported to Python 3

Post by marcelk » 22 Dec 2019, 18:23

Indeed it may be better to check for something like VID/PID pairs. Or talk to the connected devices and check if they run BabelFish. But the latter will certainly give unwelcome surprises.

User avatar
Marco
Posts: 24
Joined: 08 Dec 2019, 15:33

Re: sendFile.py ported to Python 3

Post by Marco » 22 Dec 2019, 19:57

The VID/PID check works for me!

User avatar
marcelk
Posts: 490
Joined: 13 May 2018, 08:26

Re: sendFile.py ported to Python 3

Post by marcelk » 22 Dec 2019, 20:04

That's good to know, because I was still trying to coerce my raspi setup into installing pip3 and pySerial. This isn't totally ideal yet, but neither was the earlier method. At least the VID/PID list is cross-platform and easy to expand correctly.

Post Reply