Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

match case in micropython – SyntaxError: invalid syntax

I am using python 3.10.5 on my raspberry pi pico and I am trying to use match & case instead of if statements When I try to run the program it returns an error:

Traceback (most recent call last):
  File "<stdin>", line 22
SyntaxError: invalid syntax

Here is my function:

async def BlueTooth(delay):
    while True:
        if uart.any():
            command = uart.readline()
            #print(command)   # uncomment this line to see the received data
            match command:
                case b'1':
                    led.value(1)
                    break
                case b'0':
                    led.value(0)
            write_i2c("cmd: {}\n{}".format(command, Commands.get_command_action(str(command))))

        await asyncio.sleep(delay)

I have checked, and everything should be normal, what can cause the problem?
BTW, I am using Thonny as my editor.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

According to micropython’s README.md:

MicroPython implements the entire Python 3.4 syntax (including exceptions, with, yield from, etc., and additionally async/await keywords from Python 3.5 and some select features from later versions).

match/case are new as of Python 3.10, six releases after the last version of the Python language spec MicroPython claims full support for. And it’s a ridiculously complex addition to the language (relying on a complete replacement of the simpler LL(1) parser with a more flexible/powerful/complex PEG parser among other things). They don’t support it yet, but it’s on their todo list. When it’s supported, the "MicroPython differences from CPython » Python 3.10" docs should be updated to indicate that support is completed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading