def get_func_arg(handler, runobj: RunObject, context: MLClientCtx): params = runobj.spec.parameters or {} inputs = runobj.spec.inputs or {} args_list = [] i = 0 args = inspect.signature(handler).parameters if len(args) > 0 and list(args.keys())[0] == 'context': args_list.append(context) i += 1 if len(args) > i + 1 and list(args.keys())[i] == 'event': event = Event(runobj.to_dict()) args_list.append(event) i += 1 for key in list(args.keys())[i:]: if args[key].name in params: args_list.append(copy(params[key])) elif args[key].name in inputs: obj = context.get_input(key, inputs[key]) if type(args[key].default) is str or args[key].annotation == str: args_list.append(obj.local()) else: args_list.append(context.get_input(key, inputs[key])) elif args[key].default is not inspect.Parameter.empty: args_list.append(args[key].default) else: args_list.append(None) return args_list
def test_init_context_sets_logger_correctly(caplog, step_config): context = Context() init_context(context) # logger injected into init from init context assert context.user_data.instance.logger == context.logger, "logger instance is not the context logger" handler(context, Event(json.dumps({"hi": "Hello"}))) captured = caplog.records for records in captured: assert 'Logger got passed through' in records.message, "logger failed to log to standard out"
def get_func_arg(handler, runobj: RunObject, context: MLClientCtx): params = runobj.spec.parameters or {} inputs = runobj.spec.inputs or {} args_list = [] i = 0 args = inspect.signature(handler).parameters if len(args) > 0 and list(args.keys())[0] == 'context': args_list.append(context) i += 1 if len(args) > i + 1 and list(args.keys())[i] == 'event': event = Event(runobj.to_dict()) args_list.append(event) i += 1 for key in list(args.keys())[i:]: if args[key].name in params: args_list.append(params[key]) elif args[key].name in inputs: obj = context.get_input(key, inputs[key]) if type(args[key].default) is str: filepath = obj.url if obj.kind != 'file': dot = filepath.rfind('.') filepath = mktemp() if dot == -1 else \ mktemp(filepath[dot:]) logger.info('downloading {} to local tmp'.format(obj.url)) obj.download(filepath) args_list.append(filepath) else: args_list.append(context.get_input(key, inputs[key])) elif args[key].default is not inspect.Parameter.empty: args_list.append(args[key].default) else: args_list.append(None) return args_list
def test_handler(): context, event = Context(), Event(body='Dave') context.logger.info_with('some information', x=1, y=2) out = handler(context, event) assert out == 'Hi Dave. How are you?'