def from_func(func: Callable, schema: Any, validation_rules: Dict[str, Any]) -> "_FuncAsProcessor": if schema is None: schema = parse_output_schema_from_comment(func) validation_rules.update(parse_validation_rules_from_comment(func)) tr = _FuncAsProcessor() tr._wrapper = FunctionWrapper(func, "^e?(c|[dlspq]+)x*z?$", "^[dlspq]$") # type: ignore tr._engine_param = (tr._wrapper._params.get_value_by_index(0) if tr._wrapper.input_code.startswith("e") else None) tr._use_dfs = "c" in tr._wrapper.input_code tr._need_output_schema = tr._wrapper.need_output_schema tr._validation_rules = validation_rules tr._output_schema = Schema(schema) if len(tr._output_schema) == 0: assert_or_throw( tr._need_output_schema is None or not tr._need_output_schema, FugueInterfacelessError( f"schema must be provided for return type {tr._wrapper._rt}" ), ) else: assert_or_throw( tr._need_output_schema is None or tr._need_output_schema, FugueInterfacelessError( f"schema must not be provided for return type {tr._wrapper._rt}" ), ) return tr
def from_func(func: Callable, schema: Any) -> "_FuncAsCreator": # pylint: disable=W0201 if schema is None: schema = parse_output_schema_from_comment(func) tr = _FuncAsCreator() tr._wrapper = FunctionWrapper(func, "^e?x*z?$", "^[dlspq]$") # type: ignore tr._need_engine = tr._wrapper.input_code.startswith("e") tr._need_output_schema = "s" == tr._wrapper.output_code tr._output_schema = Schema(schema) if len(tr._output_schema) == 0: assert_or_throw( not tr._need_output_schema, FugueInterfacelessError( f"schema must be provided for return type {tr._wrapper._rt}" ), ) else: assert_or_throw( tr._need_output_schema, FugueInterfacelessError( f"schema must not be provided for return type {tr._wrapper._rt}" ), ) return tr
def from_func(func: Callable, schema: Any, validation_rules: Dict[str, Any]) -> "_FuncAsCoTransformer": assert_or_throw( len(validation_rules) == 0, NotImplementedError( "CoTransformer does not support validation rules"), ) if schema is None: schema = parse_output_schema_from_comment(func) if isinstance(schema, Schema): # to be less strict on determinism schema = str(schema) if isinstance(schema, str): assert_or_throw( "*" not in schema, FugueInterfacelessError( "* can't be used on cotransformer output schema"), ) assert_arg_not_none(schema, "schema") tr = _FuncAsCoTransformer() tr._wrapper = FunctionWrapper( # type: ignore func, "^(c|[lspq]+)[fF]?x*z?$", "^[lspq]$") tr._dfs_input = tr._wrapper.input_code[0] == "c" # type: ignore tr._output_schema_arg = schema # type: ignore tr._validation_rules = {} # type: ignore tr._uses_callback = "f" in tr._wrapper.input_code.lower( ) # type: ignore tr._requires_callback = "F" in tr._wrapper.input_code # type: ignore return tr
def from_func(func: Callable, schema: Any) -> "_FuncAsTransformer": if schema is None: schema = parse_output_schema_from_comment(func) if isinstance(schema, Schema): # to be less strict on determinism schema = str(schema) assert_arg_not_none(schema, "schema") tr = _FuncAsTransformer() tr._wrapper = FunctionWrapper(func, "^[lsp]x*$", "^[lsp]$") # type: ignore tr._output_schema_arg = schema # type: ignore return tr
def test_parse_output_schema_from_comment(): def a(): pass # asdfasdf def b(): pass # asdfasdf # schema : s : int # more comment # # # schema : a : int,b:str # asdfasdf def c(): pass # schema: def d(): pass assert parse_output_schema_from_comment(a) is None assert parse_output_schema_from_comment(b) is None assert "s:int" == parse_output_schema_from_comment(c) raises(SyntaxError, lambda: parse_output_schema_from_comment(d))
def test_parse_output_schema_from_comment(): def a(): pass # asdfasdf def b(): pass # asdfasdf # schema : s:int # # # schema : a : int,b:str # asdfasdf def c(): pass # schema: def d(): pass assert parse_output_schema_from_comment(a) is None assert parse_output_schema_from_comment(b) is None assert "a:int,b:str" == parse_output_schema_from_comment(c) assert parse_output_schema_from_comment(d) is None
def from_func(func: Callable, schema: Any, validation_rules: Dict[str, Any]) -> "_FuncAsTransformer": if schema is None: schema = parse_output_schema_from_comment(func) if isinstance(schema, Schema): # to be less strict on determinism schema = str(schema) validation_rules.update(parse_validation_rules_from_comment(func)) assert_arg_not_none(schema, "schema") tr = _FuncAsTransformer() tr._wrapper = FunctionWrapper( # type: ignore func, "^[lspq][fF]?x*z?$", "^[lspq]$") tr._output_schema_arg = schema # type: ignore tr._validation_rules = validation_rules # type: ignore tr._uses_callback = "f" in tr._wrapper.input_code.lower( ) # type: ignore tr._requires_callback = "F" in tr._wrapper.input_code # type: ignore return tr