bbcmicrobit / micropython

Port of MicroPython for the BBC micro:bit
https://microbit-micropython.readthedocs.io
Other
595 stars 287 forks source link

AttributeError: 'str' object has no attribute 'partition' #641

Open microbit-mark opened 5 years ago

microbit-mark commented 5 years ago

microbit-support:20460

User reports code that previously worked using partition, no longer functions.


from microbit import *
import radio

radio.on()
started = False

def drive(onTime, offTime):
    global started
    stop(0)
    while True:
        incoming = radio.receive()
        if incoming is not None:
            # display.show(incoming)
            uart.write('Received: "' + incoming + '"\n')
            head, mid, tail = incoming.partition(' ')
            if head == 'buggy':
                cmd, mid, args = tail.partition(' ')
                # uart.write('cmd=' + cmd + ' mid=' + mid + 'args=' + args + '\n')
                if cmd == 'start':
                    started = True
                elif cmd == 'stop':
                    started = False
                    stop(0)
                elif cmd == 'direction' and started:
                    x, mid, y = args.partition(' ')
                    # uart.write('x=' + x + ' y=' + y + '\n')
                    xf = float(x)
                    yf = float(y)
                    if yf < 1:
                        if xf < 1:
                            leftforward(int(abs(yf-2)*onTime), offTime)
                        elif xf > 3:
                            rightforward(int(abs(yf-2)*onTime), offTime)
                        else:
                            forward(int(abs(yf-2)*onTime), offTime)
                    elif yf > 3:
                        if xf < 1:
                            leftbackward(int(abs(yf-2)*onTime), offTime)
                        elif xf > 3:
                            rightbackward(int(abs(yf-2)*onTime), offTime)
                        else:
                            backward(int(abs(yf-2)*onTime), offTime)
                    else:
                        stop(onTime)

It appears that this was deactivated in the port to v1.0.0

https://github.com/bbcmicrobit/micropython/blob/e10a5ffdbaf1cc40a82a665d79343c7b6b78d13b/inc/py/mpconfig.h#L709

User has found a workaround that uses a split instead.

microbit-carlos commented 5 years ago

It was present in micro:bit MicroPython v0.9 (upstream MicroPython v1.7) and not included in micro:bit MicroPython v1.0.0 (upstream MicroPython v1.9), as the define flag was probably introduced after upstream v1.7.

MicroPython v1.7-9-gbe020eb on 2016-04-18; micro:bit with nRF51822
Type "help()" for more information.
>>> a = 'hello'
>>> a.
find            rfind           index           rindex
join            split           rsplit          startswith
endswith        strip           lstrip          rstrip
format          replace         count           partition
rpartition      lower           upper           isspace
isalpha         isdigit         isupper         islower
>>> a.
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.0 with nRF51822
Type "help()" for more information.
>>> a = 'hello'
>>> a.
find            rfind           index           rindex
join            split           rsplit          startswith
endswith        strip           lstrip          rstrip
format          replace         count           lower
upper           isspace         isalpha         isdigit
isupper         islower
>>> a.