Migrating to 3.0
If you have a working program using NXT-Python 2, you will have to make changes to port it to NXT-Python 3.
Porting to Python 3
First of all you need to port your code to Python 3, there is an guide in Python documentation. You do not need to worry about keeping the code compatible with Python 2 as it is no longer supported.
There is also an automatic script to ease the porting, called 2to3.
One major change in Python is the distinction between text string
(str
) and binary string (bytes
). This means that you need to
make sure to use the right one when passing arguments to NXT-Python functions.
Porting to NXT-Python 3
Next step is to adapt the code for NXT-Python.
Major Changes
The nxt.locator.find_one_brick()
function has been removed and replaced
with the simpler nxt.locator.find()
function. Actually, the whole
nxt.locator
has been replaced.
Many debug arguments have been removed, now NXT-Python uses the
logging
module to log messages. If you want to enable debug messages,
use this code before calling any NXT-Python function:
import logging
logging.basicConfig(level=logging.DEBUG)
The nxt
and nxt.sensor
modules no longer exports name from
sub-modules. In general, NXT-Python now avoids to have two names for the same
object.
Output port constants are replaced by enumerations, using the enum
module:
NXT-Python 2 |
NXT-Python 3 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
You can now create nxt.motor.Motor
objects using
nxt.brick.Brick.get_motor()
, however direct creation still works.
Input port constants are replaced by enumerations, using the enum
module. The nxt.sensor.common
module has been removed, its content is
directly available in nxt.sensor
:
NXT-Python 2 |
NXT-Python 3 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Removed |
|
Removed |
You can now create sensor
objects using
nxt.brick.Brick.get_sensor()
, however direct creation still works. For
digital sensors with identification information, this can automatically detect
the sensor type as with previous version. The new cls argument allows
creating a sensor object using another class.
Text String or Binary String
The NXT brick only understands ASCII, so this is the default encoding used in NXT-Python.
From nxt.brick.Brick
:
get_device_info()
now returns astr
for the brick name.file_write()
,write_io_map()
andmessage_write()
now takebytes
instead of astr
.file_read()
,read_io_map()
andpoll_command()
no longer return the read size, but the returnedbytes
object is cut to the right size.get_current_program_name()
returns astr
.file_delete()
is fixed and returns astr
.find_files()
andfind_modules()
usestr
for file and module names.
File Access
File reading and writing are now implemented using classes implementing
io.RawIOBase
. When using open_file()
,
depending of the parameters, the raw file-like object is returned directly, or
wrapped in a io.BufferedIOBase
or io.TextIOBase
object.
Default access mode is now text with ASCII encoding, you need to ask explicitly for binary if needed.
This means that file access should be similar to regular Python file access.
Renamed
From nxt.brick.Brick
:
delete()
has been renamed tofile_delete()
.Many low level file and module access methods now have a
file_
ormodule_
prefix. They are however not supposed to be used directly.
From nxt.error
:
DirProtError
andSysProtError
have been renamed toDirectProtocolError
andSystemProtocolError
.FileNotFound
has been renamed toFileNotFoundError
.ModuleNotFound
has been renamed toModuleNotFoundError
.New
EmptyMailboxError
andNoActiveProgramError
have been added as subclasses ofDirectProtocolError
.
Sensors:
nxt.sensor.generic.Color20
has been renamed tonxt.sensor.generic.Color
.
Removed
Some attributes are now private (prefixed with _
).
Support for the lightblue module has been removed. It has been integrated into PyBluez.
From nxt.brick
:
Brick.open_read_linear()
has been removed, it has never been accessible from outside the NXT brick.File
,FileReader
andFileWriter
have been removed, useBrick.open_file()
.FileFinder
has been removed, useBrick.find_files()
.ModuleFinder
has been removed, useBrick.find_modules()
.Brick.mc
has been removed, make an instance using:mc = nxt.motcont.MotCont(the_brick)
From other modules:
nxt.motcont.MotCont.move_to()
has been removed as it is not part of MotorControl interface and its role was not clear.nxt.motcont.MotorConError
has been removed and replaced withnxt.error.ProtocolError
.nxt.telegram.InvalidReplyError
andnxt.telegram.InvalidOpcodeError
have been removed and replaced withnxt.error.ProtocolError
.
Module nxt.utils
has been removed, use argparse
.
Other Changes
From nxt.brick.Brick
:
get_device_info()
returns a tuple for the Bluetooth signal strength values instead of a single 32 bit value.find_files()
andfind_modules()
return an empty iterator instead of raising an exception when no file or module is found.close()
now closes the connection to the NXT brick. AlsoBrick
now implements the context manager interface so that it can be used with thewith
syntax.boot()
now takes a argument to avoid accidental firmware erasure.
Other:
nxt.motcont.MotCont
methods accept tuple as argument to control several ports.Scripts command line interface has changed.