My first Python program

Seebs usenet-nospam at seebs.net
Tue Oct 12 15:14:00 EDT 2010


So, I'm new to Python, though I've got a bit of experience in a few other
languages.  My overall impressions are pretty mixed, but overall positive;
it's a reasonably expressive language which has a good mix between staying
out of my way and taking care of stuff I don't want to waste attention on.

My first project was to replace a shell script with a Python script.  The
context is a project ("pseudo") which does some really hairy stuff in C.
Part of what it does involves creating hundreds of stub functions.  The
existing shell script did this successfully, but wasn't particularly
fast, and was basically unmaintainable.  So I've redone it in Python.

The input is a list of C function declarations, such as:
	int foo(char *s);
and the output is several files, which include lists of these functions,
declarations for wrappers for them, and so on.  So that would produce
something to the effect of:
	int foo(char *s) {
	   /* various magic */
	   int rc = -1;
	   /* stuff happens */
	   rc = wrap_foo(s);
	   /* more magic */
	   return rc;
	}

Where it gets complicated is that there are, of course, special cases;
for instance, the wrapper for 'int open(char *path, int mode, int flags)' has
to know that the flags argument is conditional, and not always provided, so
it declares open as "int open(char *path, int mode, ...)", then extracts
flags using a va_list.  Weird stuff ensues.  It's a pretty weird hunk of
code.

The source in its current form:

http://github.com/wrpseudo/pseudo/blob/master/makewrappers

The underlying task is fairly ugly, and it's my first Python project,
so the result isn't particularly pretty, but I'd be interested in
feedback on it.  However, I'm not at all sure whether it's appropriate for
this group to post 467 lines of code with no question beyond "how am
I screwing this up?"

At this point, it does everything I want it to do, so the question isn't
"how can I do this?", but "how should I have done this more idiomatically?"

There's a few quirks; one is that I have to run on whatever Python happens
to be installed on a variety of systems, from RHEL4 to Fedora 13 or so.
(It is, at least for now, completely unimportant whether I run on non-Linux
systems.)  I can't rely on packages or extensions which aren't going to
be in the base Python install.

Apart from that... I'm interested in feedback.  I'm not expecting that
this is good or idiomatic Python, but I'd like to learn to write Python
correctly and expressively, and there's nothing like criticism to improve
code.  And if people would prefer that I post the code here, I could,
I just figured that it was a bit large.

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam at seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.



More information about the Python-list mailing list