If you want to map an existing template, for example. control the elements of the stream, such as {% for x in y %} , from the expected output from it, you will have to analyze the template language - which seems like a lot of work.
On the other hand, if you are ready to write a second template for verification purposes - something like:
Dear {{customer}}, We received your last payment for {{currency}} on {{full-date}}\. We'd like you to be aware of the following charges: ( {{currency}} {{supplier}} on {{short-date}} ){,5}As always, you can view these transactions in our portal\.
..., which is just an extension of the syntax of regular expressions, it is quite simple to hack something together, which confirms this:
import re FIELDS = { "customer": r"[\w\s\.-]{,50}", "supplier": r"[\w\s\.,-]{,30}", "currency": r"[$β¬Β£]\d+\.\d{2}", "short-date": r"\d{,2}/\d{,2}", "full-date": r"\d{,2}/\d{,2}/\d{2}", } def validate(example, template_file): with open(template_file) as f: template = f.read() for tag, pattern in FIELDS.items(): template = template.replace("{{%s}}" % tag, pattern) valid = re.compile(template + "$") return (re.match(valid, example) is not None)
The above example is not the biggest Python code of all time by any means, but it is enough to get a general idea.
Zero piraeus
source share