I try my best to exmplain but if it is not clear add a comment.
I need to implement some fucntion that will take 2 arguments:
- First one contains data. Some class or dict. There is no knoledge what object is - dictionary or class
- Second is a "matcher" like PyHamcrest
The problem that I need to write matcher for some nested properties. So the function will extract nested property and apply a mather to it. But how to do that? PyHamcrest works when object is "here". Of course I can pass nested property as variable but this is not a solution. I need more generic one - define mathcers for nested properties, union all of them with and
/or
/anyOf` and check.
So for end user: it define nested properties, matcher to apply for them. Then function takes objetc and return true/false wether object mathes or not.
Something shuld work like:
matcher = or(greater_than(exist("porperty.sub_property"), 49), not(exist("porperty.sub_property"))
result = myfunction({"some_obejct": 1}, matcher)
# here result is True
I try my best to exmplain but if it is not clear add a comment.
I need to implement some fucntion that will take 2 arguments:
- First one contains data. Some class or dict. There is no knoledge what object is - dictionary or class
- Second is a "matcher" like PyHamcrest
The problem that I need to write matcher for some nested properties. So the function will extract nested property and apply a mather to it. But how to do that? PyHamcrest works when object is "here". Of course I can pass nested property as variable but this is not a solution. I need more generic one - define mathcers for nested properties, union all of them with and
/or
/anyOf` and check.
So for end user: it define nested properties, matcher to apply for them. Then function takes objetc and return true/false wether object mathes or not.
Something shuld work like:
matcher = or(greater_than(exist("porperty.sub_property"), 49), not(exist("porperty.sub_property"))
result = myfunction({"some_obejct": 1}, matcher)
# here result is True
Share
Improve this question
asked Mar 27 at 17:42
CherryCherry
33.7k74 gold badges243 silver badges395 bronze badges
1 Answer
Reset to default 0You need a function that extracts nested properties and applies PyHamcrest matchers dynamically. You can use a recursive approach or operator.attrgetter
/dict.get
for flexible access.