with open(contextfilenameas contextfileself context json load(contextfiledef process(self)print("processing "if __name__ ='__main__'infilenameoutfilenamecontextfilename sys argv[ :engine templateengine(infilenameoutfilenamecontextfilenameengine process(this is all pretty basicwe create class and initialize it with some variables passed in on the command line notice how we try to make the regular expression little bit more readable by breaking it across two lineswe use raw strings (the prefix)so we don' have to double escape all our backslashes this is common in regular expressionsbut it' still mess (regular expressions always arebut they're often worth it the pos indicates the current character in the content that we are processingwe'll see lot more of it in moment now "all that' leftis to implement that process method there are few ways to do this let' do it in fairly explicit way the process method has to find each directive that matches the regular expression and do the appropriate work with it howeverit also has to take care of outputting the normal text beforeafterand between each directive to the output fileunmodified one good feature of the compiled version of regular expressions is that we can tell the search method to start searching at specific position by passing the pos keyword argument if we temporarily define doing the appropriate work with directive as "ignore the directive and delete it from the output file"our process loop looks quite simpledef process(self)match directive_re search(self templatepos=self poswhile matchself outfile write(self template[self pos:match start()]self pos match end(match directive_re search(self templatepos=self posself outfile write(self template[self pos:]