NotImplementedError. Loop for bytearray.

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
prem111
Posts: 127
Joined: Sun Feb 23, 2020 3:18 pm

NotImplementedError. Loop for bytearray.

Post by prem111 » Sat Jul 18, 2020 11:39 am

Code: Select all

def escape(msg):
        escaped = bytearray()
        reserved = bytearray(b"\x7E\x7D\x11\x13")

        escaped.append(msg[0])
        for m in msg[1:]:
            if m in reserved:
                escaped.append(0x7D)
                escaped.append(m ^ 0x20)
            else:
                escaped.append(m)
        return escaped
File "<stdin>", line 8, in escape
NotImplementedError:

How can you do?

Thanks for help!

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: NotImplementedError. Loop for bytearray.

Post by dhylands » Sat Jul 18, 2020 10:24 pm

It looks like using 'in' with a bytearray isn't supported.

If you replace it with:

Code: Select all

reserved = b"\x7E\x7D\x11\x13"
then it seems to work properly.

prem111
Posts: 127
Joined: Sun Feb 23, 2020 3:18 pm

Re: NotImplementedError. Loop for bytearray.

Post by prem111 » Sun Jul 19, 2020 5:32 am

Thanks for help.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: NotImplementedError. Loop for bytearray.

Post by jimmo » Mon Jul 20, 2020 4:22 am

prem111 wrote:
Sun Jul 19, 2020 5:32 am
Thanks for help.
One extra point on this -- although it would have been nice if what you wanted was implemented, it's actually a lot more efficient to use dhylands' suggestion instead, as it does not involve allocating or copying a bytearray every time this function is called.

Post Reply