最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How perform sql like query (may be matcher?) on custom properties in python object? - Stack Overflow

programmeradmin0浏览0评论

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:

  1. First one contains data. Some class or dict. There is no knoledge what object is - dictionary or class
  2. 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:

  1. First one contains data. Some class or dict. There is no knoledge what object is - dictionary or class
  2. 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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

发布评论

评论列表(0)

  1. 暂无评论