Python机器人控制器代码中的SyntaxError

Python机器人控制器代码中的SyntaxError,python,python-2.7,Python,Python 2.7,我用的是机器人控制器和 def\uuuu init\uuuu(self,*需求,打印事件=False,**kwargs): 抛出一个SyntaxError,特别是print\u events语句。我不明白为什么。这是给出错误的完整代码: from functools import reduce from select import select from threading import Thread import approxeng.input.sys as sys from approxe

我用的是机器人控制器和
def\uuuu init\uuuu(self,*需求,打印事件=False,**kwargs):
抛出一个
SyntaxError
,特别是
print\u events
语句。我不明白为什么。这是给出错误的完整代码:

from functools import reduce
from select import select
from threading import Thread

import approxeng.input.sys as sys
from approxeng.input.controllers import *

EV_KEY = 1
EV_REL = 2
EV_ABS = 3


class ControllerResource:
    """
    General resource which binds one or more controllers on entry and unbinds the event listening thread on exit.
    """

    def __init__(self, *requirements, print_events=True, **kwargs):
        """
        Create a new resource to bind and access one or more controllers. If no additional arguments are supplied this
        will find the first controller of any kind enabled by the library. Otherwise the requirements must be provided
        as a list of ControllerRequirement

        :param requirements:
            ControllerRequirement instances used, in order, to find and bind controllers. If empty this will
            be equivalent to supplying a single unfiltered requirement and will match the first specified controller.
        :param print_events:
            Defaults to False, if set to True then all events picked up by the binder will be printed to stdout. Use
            this when you're trying to figure out what events correspond to what axes and buttons!
        :param kwargs:
            Any addition keyword arguments are passed to the constructors for the controller classes. This is useful
            particularly to specify e.g. dead and hot zone ranges on discovery.
        :raises ControllerNotFoundError:
            If the requirement can't be satisfied, or no requirements are specified but there aren't any controllers.
        """

        self.discoveries = find_matching_controllers(*requirements, **kwargs)
        self.unbind = None
        self.print_events = print_events

    def __enter__(self):
        """
        Called on entering the resource block, returns the controller passed into the constructor.
        """
        self.unbind = bind_controllers(*self.discoveries, print_events=self.print_events)
        if len(self.discoveries) == 1:
            return self.discoveries[0].controller
        else:
            return tuple(discovery.controller for discovery in self.discoveries)

    def __exit__(self, exc_type, exc_value, traceback):
        """
        Called on resource exit, unbinds the controller, removing the listening thread.
        """
        self.unbind()


def bind_controllers(*discoveries, print_events=False):
    """
    Bind a controller or controllers to a set of evdev InputDevice instances, starting a thread to keep those
    controllers in sync with the state of the hardware.

    :param discoveries:
        ControllerDiscovery instances specifying the controllers and their associated input devices
    :param print_events:
        Defaults to False, if set to True then all events picked up by this binder will be printed to stdout
    :return: 
        A function which can be used to stop the event reading thread and unbind from the device
    """

    discoveries = list(discoveries)

    class SelectThread(Thread):
        def __init__(self):
            Thread.__init__(self, name='evdev select thread')
            self.daemon = True
            self.running = True

            self.device_to_controller_discovery = {}
            for discovery in discoveries:
                for d in discovery.devices:
                    self.device_to_controller_discovery[d.fn] = discovery
            self.all_devices = reduce(lambda x, y: x + y, [discovery.devices for discovery in discoveries])

        def run(self):

            for discovery in discoveries:
                discovery.controller.device_unique_name = discovery.name

            while self.running:
                try:
                    r, w, x = select(self.all_devices, [], [], 0.5)
                    for fd in r:
                        active_device = fd
                        controller_discovery = self.device_to_controller_discovery[active_device.fn]
                        controller = controller_discovery.controller
                        controller_devices = controller_discovery.devices
                        prefix = None
                        if controller.node_mappings is not None and len(controller_devices) > 1:
                            try:
                                prefix = controller.node_mappings[active_device.name]
                            except KeyError:
                                pass
                        for event in active_device.read():
                            if print_events:
                                print(event)
                            if event.type == EV_ABS or event.type == EV_REL:
                                controller.axes.axis_updated(event, prefix=prefix)
                            elif event.type == EV_KEY:
                                # Button event
                                if event.value == 1:
                                    # Button down
                                    controller.buttons.button_pressed(event.code, prefix=prefix)
                                elif event.value == 0:
                                    # Button up
                                    controller.buttons.button_released(event.code, prefix=prefix)
                except Exception as e:
                    self.stop(e)

        def stop(self, exception=None):

            for discovery in discoveries:
                discovery.controller.device_unique_name = None
                discovery.controller.exception = exception

            self.running = False

    polling_thread = SelectThread()

    # Force an update of the LED and battery system cache
    sys.scan_cache(force_update=True)

    for device in polling_thread.all_devices:
        device.grab()

    def unbind():
        polling_thread.stop()
        for dev in polling_thread.all_devices:
            try:
                dev.ungrab()
            except IOError:
                pass

    polling_thread.start()

    return unbind

最小可复制示例

以下是问题的主要内容:

类控制器资源:
定义初始化(self,*需求,打印事件=True,**kwargs):
通过
对于Python3.x,这可以很好地编译(也就是说,您可以无错误地导入它),但是对于Python2.x,这会引发语法错误:

文件“test.py”,第3行
定义初始化(self,*需求,打印事件=True,**kwargs):
^
SyntaxError:无效语法
解释

原因是参数列表在varargs参数(即
*requirements
)之后包含一个常规(关键字)参数(即
print\u events=True

在Python3.x中,这是有效的,它将使参数
print\u events
成为一个只包含关键字的参数(有关更多信息和示例,请参阅)。Python2.x不支持纯关键字参数,因此上述代码在Python2.x中无效

解决方案

首先,我强烈建议在可能的情况下使用Python3.x,因为Python2.x非常古老,而且以后不再进行维护

但是,如果出于任何原因确实必须使用Python2.x,则可以按如下方式重新排列
\uuuu init\uuu
方法的参数:

类控制器资源:
定义初始化(自我,打印事件=True,*要求,**kwargs):
通过

最小可复制示例

以下是问题的主要内容:

类控制器资源:
定义初始化(self,*需求,打印事件=True,**kwargs):
通过
对于Python3.x,这可以很好地编译(也就是说,您可以无错误地导入它),但是对于Python2.x,这会引发语法错误:

文件“test.py”,第3行
定义初始化(self,*需求,打印事件=True,**kwargs):
^
SyntaxError:无效语法
解释

原因是参数列表在varargs参数(即
*requirements
)之后包含一个常规(关键字)参数(即
print\u events=True

在Python3.x中,这是有效的,它将使参数
print\u events
成为一个只包含关键字的参数(有关更多信息和示例,请参阅)。Python2.x不支持纯关键字参数,因此上述代码在Python2.x中无效

解决方案

首先,我强烈建议在可能的情况下使用Python3.x,因为Python2.x非常古老,而且以后不再进行维护

但是,如果出于任何原因确实必须使用Python2.x,则可以按如下方式重新排列
\uuuu init\uuu
方法的参数:

类控制器资源:
定义初始化(自我,打印事件=True,*要求,**kwargs):
通过

您似乎正在使用一个或多个库,这些库不是标准Python的一部分,但尚未确定它们是什么。请提供您所询问的代码的上下文。(并且
print_events
不是一个语句。)您似乎正在使用一个或多个库,这些库不是标准Python的一部分,但尚未确定它们是什么。请提供您所询问的代码的上下文。(而
print\u events
不是一个语句。)