0

Please let me know if I should provide any more information.

I'm trying to connect a pihut controller to a raspberry pi zero using code I found from approxeng. I am running python 3.5. https://approxeng.github.io/approxeng.input/simpleusage.html#connecting-to-a-controller

from approxeng.input.selectbinder import ControllerResource

    while True:
        try:
            with ControllerResource() as joystick:
                print('Found a joystick and connected')
                while joystick.connected:
                    # Do stuff with your joystick here!
                    # ....
                    # ....
            # Joystick disconnected...
            print('Connection to joystick lost')
        except IOError:
            # No joystick found, wait for a bit before trying again
            print('Unable to find any joysticks')
            sleep(1.0)

However, every time I run this code, or some similar code, I get the same error

    File "/usr/local/lib/python3.5/dist-packages/approxeng/input/__init__.py", line 375
    xname = f'{rootname}x'
    ^
    SyntaxError: invalid syntax

Upon inspecting this file (found here https://github.com/ApproxEng/approxeng.input/blob/master/src/python/approxeng/input/init.py), at line 375 I find

# Look to see whether we've got pairs of lx,ly and / or rx,ry and create corresponding circular axes
        def add_circular_axis(rootname):
            xname = f'{rootname}x'
            yname = f'{rootname}y'
            if xname in self.axes_by_sname and yname in self.axes_by_sname:
                self.axes_by_sname[rootname] = CircularCentredAxis(x=self.axes_by_sname[xname],
                                                                   y=self.axes_by_sname[yname])

I can't edit this file as it's write-protected, and even if I could edit it, I'm not sure what to do. How can I stop getting this error?

  • Hi @Yacine Hannane, your python statement " xname = f ' {rootname} x ' " looks weird. It reminds me my arduino days when I wrote tons of unreadable C++ preprocessor macros. Can you google python docs and let me know the link of what in general do the following 2 expressions mean? - (1) f ' {x} ', (2) f ' { x } y '. Ref: ApproxEnggr Python Game Controller approxeng.github.io/approxeng.input/index.html# – tlfong01 Aug 9 '19 at 2:12
  • Your python expression also reminds me of the unreadable Python watchdog timer time bomb function " : ( ) { : | : & } ; : " . It took me a very long while to google to understand what is going on there! :) raspberrypi.stackexchange.com/questions/99584/… – tlfong01 Aug 9 '19 at 2:27
  • On second thought, the statement with syntax error might be a shell script statement, not of Bash, but perhaps Korn or other shell, not compatible to Rpi Stretch. So you may like to check out which shell ApproxEnggr is using. – tlfong01 Aug 9 '19 at 6:29
1

I'm the author of this library - f'{something}' is a syntax introduced in Python3.6, it allows for direct variable insertion into strings (including with formatting characters, although I'm not using those here). The library requires Python3.7, so I'm not particularly surprised it doesn't like running under something earlier.

https://realpython.com/python-f-strings/ for some more detail about this feature, it's really nice!

  • Is this relevant to the question or just a post pushing your library? – Brick Aug 9 '19 at 13:15
  • I'd say it's fairly relevant yes, the code the OP is trying to run is the library I wrote, and the error they report is due to running under an older version of Python. As there's clearly some confusion (see all the comments) about what the f<string> notation means I figured I might as well explain that too. – user2776089 Aug 9 '19 at 23:39
  • OK, I see. I read your first sentence differently than it was meant. Cool. – Brick Aug 10 '19 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.