Best Practices for Python Script Development?

M

metaperl

Hello, I am responsible for converting 30 loosey-goosey Perl scripts
into 30 well-documented easy to understand and use Python scripts.

No one has said anything in particular about how this should be done,
but the end product should be "professional" looking.

So, I'm looking for some books and suggestions which will help me write
high-quality scripts. I know about object-oriented programming and
application configuration and have spent 6 years doing professional
Perl but have decided that Python is the new choice of serious agile
developers. Only thing is, I only figured that out 1 month ago and
don't really know how to write good code yet :)


Documentation
=============
I would like browseable HTML documentation for each script. I have
started
to get some idea of using docstrings within a file and that they can
be extracted but would like a pointer to some docs on this practice.

`Pydoc <http://docs.python.org/lib/module-pydoc.html>`_ seems to be
built around modules and I want to document scripts.

Configuration
==========
Based on `this thread
<http://groups.google.com/group/comp.../thread/2ce17e8060ac708c?tvc=1&q=ConfigParser>`__
I plan to use ConfigParser to squirrel away generic information like
database connection info. The chapter "table-driven code" in Bruce
Eckels' "Thinking in Python" seems like it would've been of help but
it is not written. Any weblinks for similar articles?

The directory hierarchy is going to be /data/vendor/X where X is a
particular data vendor. In /data/vendor/config.ini will be the generic
config info along with an index.html giving an overview of the whole
shebang.

Usage
=====
Most scripts will be run from cron and so should be driveable via
command-line options.

optparse looks very good to me. And I had never thought about
required versus optional arguments in the way that it does. What an
eye-opener.

Searching cheeseshop.python.org/pypi for getopt modules does not work
very well by the way.

Version Control
===============
I've been using bazaar but mercurial seems to be quicker to create
forward-facing browseable web repos. And the propagation and pull
facilities seem more integrated and useable. But both are highly
desireable products.

OS X
====
I'm doing all this on OS X. I like Unix but am too dumb for Linux.
Windows is a great GUI but I hate the registry and hate jumping back
and forth between Windows and Cygwin and never getting the full power
of either.

I think I'll get 2 Mac Minis - one for the
actual data munging and one to back everything up on.

Which actual Python distro I will use is going to be based on the data
I get from `this thread
<http://groups.google.com/group/comp...e4206d1fbb7/f844fa418e8171dc#f844fa418e8171dc>`_

Books
=====
I think these 4 will carry me a long way, but any other suggestions ar
welcome:
* `Dive into Python <http://diveintopython.org/toc/index.html>`_
* `Text Processing in Python <http://gnosis.cx/TPiP/>`_
* Python Cookbook
* Programming Python
 
R

Robert Kern

metaperl said:
Usage
=====
Most scripts will be run from cron and so should be driveable via
command-line options.

optparse looks very good to me. And I had never thought about
required versus optional arguments in the way that it does. What an
eye-opener.

Searching cheeseshop.python.org/pypi for getopt modules does not work
very well by the way.

"Does not work very well" how? Are you just not finding anything?

If so, it's probably because there really aren't any, and they wouldn't be
referred to as "getopt modules." There's the (old) getopt module and the
(preferred) optparse module in the standard library. optparse really does fill
the niche quite thoroughly.

However, if you like, there's the newcomer argparse that tries to improve upon
optparse in several respects:

http://argparse.python-hosting.com/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
N

Nick Craig-Wood

metaperl said:
high-quality scripts. I know about object-oriented programming and
application configuration and have spent 6 years doing professional
Perl but have decided that Python is the new choice of serious agile
developers.

I was where you are a couple of years ago!
Books
=====
I think these 4 will carry me a long way, but any other suggestions ar
welcome:
* `Dive into Python <http://diveintopython.org/toc/index.html>`_

This is an excellent book to bootstrap your way into Python. It is
very good for experienced programmers.
* `Text Processing in Python <http://gnosis.cx/TPiP/>`_

I liked this book. However by the time I read it I'd already read
quite a few Python books so I perhaps didn't get as much out of it as
I should.
* Python Cookbook

Cookbooks don't really lend themselves to general reading, but saying
that I did read this one from cover to cover (the previous edition).
There is lots of interesting stuff in there and you'll learn plenty of
tricks, though Python doesn't have nearly as many tricks as Perl to
learn. If you are looking for something in particular there are many
more recipes in the online cookbook.
* Programming Python

The second edition is a thorough introduction to python 2.1. (Amazon
says there is a 3rd edition due out very soon though covering python
2.5.) The 2nd edition is missing newer features of the language, but
otherwise it is a solid book with lots of good stuff in. The section
on programming with TK is very good too - I keep coming back to that
section.

....

I'd recommend the first and the last from your list to start with,
"Dive into Python" and "Programming Python".
 
A

Ant

`Pydoc said:
built around modules and I want to document scripts.

Any python script *is* a python module. So pydoc is what you are after
here.
Version Control
===============

Subversion and Trac are a very good combination - Trac is a web-based
view of the subversion repository as well as being a wiki and issue
tracker. (Incidentally Trac is written in Python.)
Books
=====
* Python Cookbook
* Programming Python

I've found the Python Pocket reference very useful - I rarely use
anything other than that and the online docs (though I found Python in
a Nutshell and the cookbook very useful early on).
 
S

skip

Ant> Any python script *is* a python module. So pydoc is what you are
Ant> after here.

Assuming you name your scripts so that they are importable (e.g. "foobar.py"
instead of "foo-bar.sh").

Skip
 
M

metaperl

Ant said:
Any python script *is* a python module. So pydoc is what you are after
here.

Yes, but Lundh's PythonDoc looks good too. I'm inclined to go with
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top