Added inspect() command
This commit is contained in:
@@ -296,8 +296,8 @@ class SheerkaFilter(BaseService):
|
|||||||
# s.send(data)
|
# s.send(data)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pipe_traverse(args):
|
def pipe_traverse(iterable):
|
||||||
for arg in args:
|
for arg in iterable:
|
||||||
try:
|
try:
|
||||||
if isinstance(arg, str):
|
if isinstance(arg, str):
|
||||||
yield arg
|
yield arg
|
||||||
@@ -342,7 +342,7 @@ class SheerkaFilter(BaseService):
|
|||||||
yield item
|
yield item
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pipe_write(iterable, fname, glue="\n"):
|
def pipe_to_file(iterable, fname, glue="\n"):
|
||||||
with open(fname, "w") as f:
|
with open(fname, "w") as f:
|
||||||
for item in iterable:
|
for item in iterable:
|
||||||
f.write(str(item) + glue)
|
f.write(str(item) + glue)
|
||||||
@@ -432,3 +432,13 @@ class SheerkaFilter(BaseService):
|
|||||||
yield item
|
yield item
|
||||||
except NameError:
|
except NameError:
|
||||||
pass
|
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 isinstance(format_instructions, FormatInstructions)
|
||||||
assert format_instructions.recursive_props["children"] == 10
|
assert format_instructions.recursive_props["children"] == 10
|
||||||
assert format_instructions.recursive_props["other_prop"] == 15
|
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