Tidy module?

2008-04-17 Thread Mark Reed
Is there an easy_installable egg with an interface to libtidy?  I
found µTidy, but it looks like an inactive project, with no updates
since 2004, so I'm skeptical of its reliability.  I found mxTidy, but
it's only available as part of some larger distribution, and I don't
want to replace my Python installation.  Is there one out there that I
missed?

Preferably, it'd be a single module I can drop into my current Python
2.5 installation, ideally via easy_install, to get tidy functionality
without having to resort to os.popen("tidy").

Thanks in advance for any pointers.
-- 
http://mail.python.org/mailman/listinfo/python-list


Pythonically extract data from a list of tuples (getopt)

2008-04-23 Thread Mark Reed
So the return value from getopt.getopt() is a list of tuples, e.g.

>>> import getopt
>>> opts = getopt.getopt('-a 1 -b 2 -a 3'.split(), 'a:b:')[0]; opts
[('-a', '1'), ('-b', '2'), ('-a', '3')]

what's the idiomatic way of using this result?  I can think of several
possibilities.

For options not allowed to occur more than once, you can turn it into
a dictionary:

>>> b = dict(opts)['-b']; b
'2'

Otherwise, you can use a list comprehension:

>>> a = [arg for opt, arg in opts if opt == '-a']; a
['1', '3']

Maybe the usual idiom is to iterate through the list rather than using
it in situ...

>>> a = []
>>> b = None
>>> for opt, arg in opts:
...   if opt == '-a':
... a.append(arg)
...   elif opt == '-b':
... b = arg
...   else:
... raise ValueError("Unrecognized option %s" % opt)

Any of the foregoing constitute The One Way To Do It?  Any simpler,
legible shortcuts?

Thanks.


--
http://mail.python.org/mailman/listinfo/python-list