def test_parse_output_schema_from_comment(): # partitionby_has: a, b # partitionby_is: c , d # presort_has: a, b desc # presort_is: a asc, b desc # input_has: a,b: str,c # input_is: a: int , b : str # dummy: ssdsfd def t(): pass # input_is: def t2(): pass assert { "partitionby_has": ["a", "b"], "partitionby_is": ["c", "d"], "presort_has": [("a", True), ("b", False)], "presort_is": [("a", True), ("b", False)], "input_has": ["a", "b:str", "c"], "input_is": "a:int,b:str", } == parse_validation_rules_from_comment(t) raises(SyntaxError, lambda: parse_validation_rules_from_comment(t2))
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, validation_rules: Dict[str, Any] ) -> "_FuncAsOutputter": validation_rules.update(parse_validation_rules_from_comment(func)) tr = _FuncAsOutputter() tr._wrapper = FunctionWrapper( # type: ignore func, "^e?(c|[dlspq]+)x*z?$", "^n$" ) tr._need_engine = tr._wrapper.input_code.startswith("e") tr._use_dfs = "c" in tr._wrapper.input_code tr._validation_rules = validation_rules return tr
def from_func( func: Callable, schema: Any, validation_rules: Dict[str, Any]) -> "_FuncAsOutputTransformer": assert_or_throw(schema is None, "schema must be None for output transformers") validation_rules.update(parse_validation_rules_from_comment(func)) tr = _FuncAsOutputTransformer() tr._wrapper = FunctionWrapper( # type: ignore func, "^[lspq][fF]?x*z?$", "^[lspnq]$") tr._output_schema_arg = None # 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
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