Python considered harmful?
October 17th, 2008
Yesterday morning I started writing a simple tool which should compile all C++ files in a given directory tree. Ok, simple enough, it’s just a matter of a simple os.walk and some subprocess.call.
After that, I thought that it would be nice if the tool did something more. For example, I could write down a function that was able to “make clean” the executables I built. This is simple as well. You just have to be smart since on *nix executables do not have extensions. And I could make the system Windows friendly. It was a matter of a couple of functions more.
However, at that point I needed some kind of interface. A simple command line tool was nice. But… well, I remembered using a Prolog tool which did some heuristic to get wrong commands right (zsh does this as well)… so, why not? A levenshtein distance + some more checks could do. And it did. Nice.
But… what if I just want to compile one single file? Well, I had already most of the infrastructure. And why not letting me specify a single file to clean? As easy as breathing. Done. By the way, the system at that point supported building and cleaning object files as well.
And I was already wondering that I left out C files. After all I needed to process C++ files, but the tool would be surely more useful if I could use it with C files too. And why not Pascal? Ok, I have the right design which could support kind of configuration to map file types to compilers… and I could parametrize compiler options as well. Somewhere in my mind an automated compiler detection system was already lurking.
I refactored the tool. It became a package, with a lot of useful functions… But well… why rebuilding all the sources? I need only to rebuild executables which are older than their source file. This is easy, a couple of stat…
At that point I realized that if only I added some kind of file dependency discovery I would have basically reinvented make. That is to say I was reinventing a square wheel. One of these days I’ll get the very first script and modify so that instead of compiling files generates a CMakeLists.txt, calls cmake and then Make.
The end of the story is developing in Python is too fast.
BTW: the author is not suggesting Python should not be used, just the opposite!
9 Responses to “Python considered harmful?”
1Jafraldo
October 17th, 2008 @ 18:14
Thanks for that bit of clarification at the end there. A lot of us internetters can’t understand humor and would have missed the point of this blog post without that final sentence.
2asdf
October 17th, 2008 @ 23:03
I think python sucks because of significant white-space. That’s why no one uses it.
3Enrico Franchi
October 18th, 2008 @ 03:31
@Jafraldo: Well, the point was more that I should think more about reusing pieces of software rather than rewriting them, even though in this case language productivity was somewhat responsible of late understanding of my mistake. And I stress that it was my mistake.
Moreover, the point of the post was not entirely clear. It was easy to read only a couple of paragraphs and jump to conclusions. More over I’ve got my reputation to defend, I only flame pro-python.
4Enrico Franchi
October 18th, 2008 @ 03:38
@asdf: This is an example of hard to understand humor.
5ilya
October 18th, 2008 @ 13:02
As a matter of fact, creating a square wheel would be an improvement in this case ;-).
Simply b/c make is more of a triangular wheel and square wheels roll way better.
And, yes, in my experience a custom build scripts often beats handwritten makefiles both in terms of maintainability and code size.
Btw, if you want a python based alternative to make: take a look at scons.
6Enrico Franchi
October 18th, 2008 @ 13:43
Well, I see your point! I had a look at scons, but I chose to use cmake.
Having Python to script the builds is great, though I found cmake syntax more readable and clean. This is, of course, just a matter of taste.
About my square wheel, the point is that I could just generate CMakeLists.txt and let cmake && make do the job. Cmake is quite good at it!
Of course, I could have used scons as well. What they missed (or maybe I just did not find it in the documentation) is build every file of a certain kind in a given subdirectory without specifying it anywhere. I have to teach C++ and I need to write a lot of short examples and I find it boring to add them to the build system. A prefer a build system which discovers the files and builds them automatically.
7Stu
October 19th, 2008 @ 11:19
Python syntax would be so much nicer than make files (or bowing to the pointy bracketed overlords of ant)… bring it on!
8Jeethu Rao
October 26th, 2008 @ 23:58
And I thought I was the only one who’d tried to do this with Python. Automatic compiler detection, smart rebuilding with stat() calls, your whole post felt like a deja vu, except for the part where you used levenshtein distance to get wrong commands right (I just used optparse).
9Enrico Franchi
October 30th, 2008 @ 07:03
I think this could be listed among the things “any python programmer does sooner or later” among with the creation of a home brewed web framework.
Leave a Reply