Added inspect() command
This commit is contained in:
@@ -296,8 +296,8 @@ class SheerkaFilter(BaseService):
|
||||
# s.send(data)
|
||||
|
||||
@staticmethod
|
||||
def pipe_traverse(args):
|
||||
for arg in args:
|
||||
def pipe_traverse(iterable):
|
||||
for arg in iterable:
|
||||
try:
|
||||
if isinstance(arg, str):
|
||||
yield arg
|
||||
@@ -342,7 +342,7 @@ class SheerkaFilter(BaseService):
|
||||
yield item
|
||||
|
||||
@staticmethod
|
||||
def pipe_write(iterable, fname, glue="\n"):
|
||||
def pipe_to_file(iterable, fname, glue="\n"):
|
||||
with open(fname, "w") as f:
|
||||
for item in iterable:
|
||||
f.write(str(item) + glue)
|
||||
@@ -432,3 +432,13 @@ class SheerkaFilter(BaseService):
|
||||
yield item
|
||||
except NameError:
|
||||
pass
|
||||
|
||||
def pipe_inspect(self, iterable, path, when=None):
|
||||
compiled = self.get_compiled("inspect", path)
|
||||
for item in iterable:
|
||||
try:
|
||||
context = {} if is_primitive(item) else as_bag(item)
|
||||
context["self"] = item
|
||||
yield eval(compiled, context)
|
||||
except Exception as ex:
|
||||
yield ex
|
||||
|
||||
@@ -211,3 +211,19 @@ class TestSheerkaFilter(TestUsingMemoryBasedSheerka):
|
||||
assert isinstance(format_instructions, FormatInstructions)
|
||||
assert format_instructions.recursive_props["children"] == 10
|
||||
assert format_instructions.recursive_props["other_prop"] == 15
|
||||
|
||||
def test_i_can_inspect_obj(self):
|
||||
filter_service = SheerkaFilter(None)
|
||||
|
||||
lst = [Obj("a", "b"), Obj("c", "d")]
|
||||
res = lst | Pipe(filter_service.pipe_inspect)("prop2")
|
||||
|
||||
assert list(res) == ["b", "d"]
|
||||
|
||||
def test_i_can_inspect_obj_with_bag(self):
|
||||
filter_service = SheerkaFilter(None)
|
||||
|
||||
lst = [ObjWithAsBag("a", "b"), ObjWithAsBag("c", "d")]
|
||||
res = lst | Pipe(filter_service.pipe_inspect)("second_prop")
|
||||
|
||||
assert list(res) == ["b", "d"]
|
||||
|
||||
Reference in New Issue
Block a user