def Where(self, filter): r''' Filter the object stream, allowing only items for which `filter` evaluates as try through. Args: filter: A filter lambda that returns True/False. Returns: A new ObjectStream that contains only elements that pass the filter function ''' return ObjectStream(query_ast.Where(self._ast, parse_as_ast(filter)))
def Select(self, f): r""" Apply a transformation function to each object in the stream, yielding a new type of object. Args: f: selection function (lambda) Returns: A new ObjectStream of the transformed elements. """ return ObjectStream(query_ast.Select(self._ast, parse_as_ast(f)))
def SelectMany(self, func): r""" Given the current stream's object type is an array or other iterable, return the items in this objects type, one-by-one. This has the effect of flattening a nested array. Args: func: The function that should be applied to this stream's objects to return an iterable. Each item of the iterable is now the stream of objects. Returns: A new ObjectStream. """ return ObjectStream(query_ast.SelectMany(self._ast, parse_as_ast(func)))
def test_parse_as_ast_lambda(): l = lambda_unwrap(ast.parse("lambda x: x + 1")) r = parse_as_ast(l) assert isinstance(r, ast.Lambda)
def test_parse_as_ast_wrapped_lambda(): l = ast.parse("lambda x: x + 1") r = parse_as_ast(l) assert isinstance(r, ast.Lambda)
def test_parse_as_ast_bad_text(): try: parse_as_ast("x+1") assert False except: pass
def test_parse_as_ast_good_text(): r = parse_as_ast("lambda x: x+1") assert isinstance(r, ast.Lambda) s = ast.dump(r) assert "op=Add" in s
def test_parse_as_ast_none(): try: parse_as_ast("") assert False except: pass