[Tutor] General Feedback, Script Structure

Kent Johnson kent37 at tds.net
Sun Feb 15 18:20:45 CET 2009


On Sun, Feb 15, 2009 at 11:00 AM, Damon Timm <damontimm at gmail.com> wrote:
> Hi -
>
> Am still new to python -- was writing a script that is used by mdadm
> (linux software raid utility) when there was a raid "event" ... the
> script then sends an email (using another python script caled
> "gmailme") to me with the information from the event and attaches the
> details of the raid device that triggered the problem.
>
> It works fine -- seems to do what I need -- but, to be very frank, I
> don't think it is very *pretty* !  But, I am not sure what the
> "python-way" would be to re-structure this so it continues to do what
> it needs to do but is more readable and future-friendly.
>
> Could you take a look ?
>
> http://python.pastebin.com/m4e1694d5
>
> Would be interested in any feedback (obviously, I should add some doc strings!).

Some ideas:
- put the main code in a main() function rather than splitting it
across the file.
- the use of tmpfile is awkward, can you make the gmailme script take
its input in strings?
- I would use plain positional parameters to getMessage(), rather than
supplying defaults.
- the status dict could be built once, containing just the format strings, e.g.
status = dict(
  TestMessage = "This was a crazy test message! Woo hoo!",
  RebuildStarted = "The rebuilding of %(mddevice)s has begun!",
  # etc
}

then you can build the message as
body = status.get(event, nomatch) % vars()
message = header + body + footer

Kent


More information about the Tutor mailing list