C++ code generation from xml

Max Ischenko max at malva.com.uaREMOVE.IT
Tue Apr 16 02:56:54 EDT 2002


 phil hunt wrote:

>>I have a Python program that uses data stored in XML file to generate some
>>C++ code. The problem (or, more precisely, code smell) I have is that the
>>actual generation algorithm is hardwired into python and therefore is
>>cryptic, difficult to understand and maintain.

> How about:

>   def genBody(self, c):
>      substitutions = { 
>         "name": c.cxxClassName,
>         "ctorArg": c.factoryCtorArg or ''}
>      if c.factoryNargs == 0:
>         return subst(template1, substitutions)
>      else 
>         return subst(template2, substitutions)
Hmm. Have to think about it.
It still embeds generation algorithm inside the python script, but at
least does this much more elegantly that my version.
 From the DTSTTCPW point I think I could give it a chance, thanks!

>   def subst(formatString, args):
>      f = ... in formatString convert all $xxx to %(xxx)s ...
>      return f % args

> The implementation of subst() is left as an exercise.



-- 
If something is ugly, it can't be the best solution.
             -- Kelly Johnson.



More information about the Python-list mailing list