Python 3.10 模式匹配 (PEP 634) - 字符串中的通配符

Python 3.10 pattern matching (PEP 634) - wildcard in string

我得到了一个很大的 JSON 对象列表,我想根据其中一个键的开头来解析这些对象,其余的只是通配符。很多键都是相似的,比如 "matchme-foo""matchme-bar"。有一个内置的通配符,但它只用于整数值,有点像 else.

我可能忽略了一些东西,但我在提案的任何地方都找不到解决方案:

https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching

在 PEP-636 中还有更多关于它的内容:

https://www.python.org/dev/peps/pep-0636/#going-to-the-cloud-mappings

我的数据是这样的:

data = [{
          "id"     : "matchme-foo",
          "message": "hallo this is a message",
      },{
          "id"     : "matchme-bar",
          "message": "goodbye",
      },{
          "id"     : "anotherid",
          "message": "completely diffrent event"
      }, ...]

我想做一些可以匹配 ID 的事情,而不必列出 | 的长列表。

像这样:

for event in data:
    match event:
        case {'id':'matchme-*'}: # Match all 'matchme-' no matter what comes next
            log.INFO(event['message'])
        case {'id':'anotherid'}:
            log.ERROR(event['message'])

这是对 Python 的一个相对较新的补充,因此关于如何使用它的指南还不多。

你可以使用 guard:

for event in data:
    match event:
        case {'id': x} if x.startswith("matchme"): # guard
            print(event["message"])
        case {'id':'anotherid'}:
            print(event["message"])

引用自official documentation,

Guard

We can add an if clause to a pattern, known as a “guard”. If the guard is false, match goes on to try the next case block. Note that value capture happens before the guard is evaluated:

match point:
     case Point(x, y) if x == y:
         print(f"The point is located on the diagonal Y=X at {x}.")
     case Point(x, y):
         print(f"Point is not on the diagonal.")

另请参阅: