Ask HN: Code Generators
I am interested in knowing which (if any?) code generation tools Hacker News readers use?
As a professional C# developer (don't laugh :), I'm familiar with CodeSmith, MyGeneration, and most recently Microsoft T4. But I'm getting more and more into Python, PyDev, and Eclipse and would like to generate Python source based on entity definition files. I have rolled my own ORM toolkit in C# would now like to "port" it to Python.
I came across Cheetah and it looks pretty solid, but an IDE-integrated solution would be ideal. Is there a tool which is the code generator to use? Or maybe because there is no obvious solution, code generation with dynamically-typed languages is dead and I'm trying to apply my statically-typed background to a dynamically-typed foreground?
7 comments
[ 5.0 ms ] story [ 31.2 ms ] threadTo keep track of all of this I ended up specifying a lot of the rules in an XML file and used 'Cog' written by Ned Batchelder. http://nedbatchelder.com/code/cog/ to generate the boilerplate code. Cog is different than any other code generation tool I had used before because you write your generation code in commented lines in your normal source file. When you run Cog, it finds these special lines and replaces the code between the markers with whatever code is supposed to be generated. No other code is touched so it survived a round trip through an IDE like VS. It worked great for what I was doing and I used it heavily but the project was thankfully cancelled before I could finish.
THIS
Don't ever generate boilerplate Python code (especially 'fill in the blanks' crap). You're using a language where you can put the data in an appropriate structure used by high-order code.
You don't even have to make a new data structure if the information is already present. For instance, I wrote a python library that when called as an executable, looks up 'tool verb' style commands by looking at the methods on the available classes, gets the help from __doc__, and the extra optargs from attributes on the methods (added via a decorator).
So, help me out here - "You're using a language where you can put the data in an appropriate structure used by high-order code." I don't understand that statement? I don't know what "the data" or "high-order code" is?
It's true that in less powerful languages, you need code generation to do what more powerful languages can do with higher-order functions and reflection. But it's also true that code generation (along with other techniques from compiler and language development) remain quite useful for writing shorter, clearer programs in environments that support this.