parse for format, for Jinja2 ...?

parse() is the opposite of format() https://pypi.python.org/pypi/parse

I learned that there is a way to make a python dictionary etc. from a template string by doing the reverse of format

I want to match multiple lines

I have template.txt and result.txt, and I want to combine the {} parts in template.txt into a dictionary.

However, result.txt also captures all other standard output etc., it is large to some extent, and it is difficult to make the whole template.txt. Even if it doesn't, there are lines that change from run to run (but aren't important enough to get them all), such as date and run time logs.

Until now, I wanted to make it a little easier than I used to make it half-manually every time with a shell script using awk or sed.

What i did

An image that parses each line of template.txt while convolving it into each line of result.txt, and combines and extracts all matching lines. Both map and reduce do not stop even if None appears in the middle, so it is very wasteful, but if you do not seek performance to that extent ... (I didn't get it right with a double loop ...: persevere :)

from parse import *
from parse import compile

def coerce_res(a, b):
   try:
      a.named.update(b.named)
      a.fixed = a.fixed + b.fixed
      return a
   except:
      return None

def match():
   with open('template.txt', 'r') as ftemp, open('result.txt', 'r') as flog:
      templines = ftemp.readlines()
      # todo: remove leading & trailing blank lines from templines
      loglines = flog.readlines()

      for i in range(len(loglines) - len(templines) + 1):
         a = map(lambda (x,y) : parse(x,y), zip(templines, loglines[i:]))
         b = reduce(coerce_res, a)

         if(b):
            return b

print match()
# print fixed, named

The summary is usually at the end, so it seems faster to do it from the end

what I want

parse is the reverse conversion of format, but if there is something like the reverse conversion of Jinja2, I think that there is a user, but is there such a thing? In the runtime log etc., describe the part that you do not know exactly how many lines will appear with something like {% for%} and output it as a list. Enclose the log that appears only when a failure occurs in a {% if%} block to detect the failure. (By the way, I haven't used Jinja before, so I'm completely talking about it.)

Recommended Posts

parse for format, for Jinja2 ...?
[Python 2/3] Parse the format string
Use Jinja2 for PasteScript template engine
Bbox optimization for custom datasets [COCO format]