types = {'WORD': '\\w+', 'NUMBER': '\\d+'}

def compile(pat):
    return re.sub('%{(\\w+):(\\w+)}', lambda m: '(?P<' + m.group(2) + '>' +
        types[m.group(1)] + ')', pat)
rr = compile('%{WORD:method} %{NUMBER:bytes} %{NUMBER:duration}')
print(re.search(rr, 'hello 123 456').groupdict())
Is there any Python implementation of logstash's grok functionality?