From 459f1832b606b524d76706a129930774fbfb92cd Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Mon, 19 Nov 2018 11:25:45 +0100 Subject: [PATCH] [utils] Implement helper method to check method argument This allows for python2 and python3 support and is required since we are currently providing a python2.7 environment. --- beat/backend/python/algorithm.py | 10 +++------- beat/backend/python/utils.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/beat/backend/python/algorithm.py b/beat/backend/python/algorithm.py index af0fc27..d5584db 100644 --- a/beat/backend/python/algorithm.py +++ b/beat/backend/python/algorithm.py @@ -42,8 +42,6 @@ import six import numpy import simplejson -from inspect import signature - from . import dataformat from . import library from . import loader @@ -257,15 +255,13 @@ class Runner(object): elif self.algorithm.type == Algorithm.AUTONOMOUS: run_args = [self.obj, 'process', self.exc, data_loaders, outputs_to_use] - sig = signature(self.obj.process) - params = sig.parameters - + has_loop_arg = utils.has_argument(self.obj.process, 'loop_channel') if loop_channel is not None: - if 'loop_channel' in params: + if has_loop_arg: run_args.append(loop_channel) else: raise exc("Algorithm '%s' is not a valid loop enabled algorithm" % self.name) - elif 'loop_channel' in params: + elif has_loop_arg: raise exc("Algorithm '%s' is a loop enabled algorithm but no loop_channel given" % self.name) return loader.run(*run_args) diff --git a/beat/backend/python/utils.py b/beat/backend/python/utils.py index eacb837..c18963b 100644 --- a/beat/backend/python/utils.py +++ b/beat/backend/python/utils.py @@ -360,3 +360,18 @@ class NumpyJSONEncoder(simplejson.JSONEncoder): if obj.name == 'str': return 'string' return obj.name return simplejson.JSONEncoder.default(self, obj) + + +# ---------------------------------------------------------- + + +def has_argument(method, argument): + try: + from inspect import signature + sig = signature(method) + params = sig.parameters + except ImportError: + from inspect import getargspec + params = getargspec(method).args + + return argument in params -- GitLab