Python equivalent of #line?

Russ Salsbury rcs at russellsalsbury.com
Mon May 26 14:03:39 EDT 2003


Steven Taschuk <staschuk at telusplanet.net> wrote in message news:<mailman.1053933749.3956.python-list at python.org>...
> 
> Code generation makes sense in some situations; Knuth's "literate
> programming" methodology comes to mind.  (And having line numbers
> refer to the original source makes good sense there.)

+1 for #line

Very few people have a need for it, but those of us who write
application generators appreciate it very much.

And a real world example.  About a decade ago, I was a member of a
team of 4 people who had to implement the central host software for a
statewide vehicle emissions program.  Due to drawn out negotiations we
had a bit more than 6 months to implement it and a hard deadline with
significant $ penalties.  The approach I chose was to use an
application generator that generated C code from templates controlled
by a template language very much like Velocity with added support for
objects with a very rich typing system.  It functiond very much like
JSP or Velocity.  The key piece of leverage was generating huge
amounts of SQL and C embedded SQL from object declarations. It took me
3 months to implement the application generator.  (I know, if I had
known about Python, I could have done it in a weekend. :-) Even so, we
made the deadline and were inspecting vehicles without a hitch on
opening day.

I compared the generated application to the input templates and found
a 5 to 1 difference in lines of code.  Another metric: our team put in
about 3 man-years and met our deadline. A competitor in another state
had a team 2 or 3 times our size.  They started a year before their
deadline, missed it by a year, and took another year before the state
would accept the system.  Their effort may have been 10 times ours.

Although my project was in C and we could have achieved similar
improvements had we developed the system in Python, the real point is
that you can get huge savings in implementation effort on a large
system by using a domain specific language and generating code in a
lower level target language.

One problem I had in generating C was getting the indentation correct.
 The natural thing to do was to use reasonable indentation in writing
the C and the template language in the templates, which produced some
pretty horrible indentation in the output and took a fair amount of
effort in both the generator and the templates to get it right. 
Python's use of indentation makes the formatting issue critical in a
template driven system.  I suppose using #begin and #end of #{ and #}
and a reformatter would do the trick.

Any effort to support Python-like domain specific languages gets my
support.

Russ




More information about the Python-list mailing list