My first Python program

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Oct 13 13:48:36 EDT 2010


Seebs wrote:
> 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
>   
If you plan to code in python, then you may consider using a linter. 
These a formidable tools to help you set your coding rules, and most of 
these tools can detect most of the basics errors, or coding wrong habits.

If you wonder about some defects reported by such linters, you can then 
ask in this list why something is not that good, because it may not be 
always obvious.

'pylint' is one them, pretty effective.

JM





More information about the Python-list mailing list