optparse with numpy.array?

Johan Ekh ekh.johan at gmail.com
Tue Jan 27 01:01:38 EST 2009


Thank you James,
but I just can't optparse to accept an array, only integers, floats ans
strings.

My code looks like this

from optparse import OptionParser
parser = OptionParser()
parser.add_option('-t', '--dt', action='store', type='float', dest='dt_i',
default=0.1, help='time increment where lsoda saves results')
parser.add_option('-T', '--tstop', action='store', type='float',
dest='tstop_i', default=1.0, help='duration of the solution')
parser.add_option('-m', '--mass_vector', action='store', type='float',
dest='m_i', default=[1.0, 1.0], help='vector with lumped masses')
op, args = parser.parse_args(sys.argv[1:])

I want this to work for m_i = array([1.0, 2.0, 3.0]) but the optparse
complains that m_i is not a float.

Best regards,
Johan



On Tue, Jan 27, 2009 at 6:53 AM, James Mills
<prologic at shortcircuit.net.au>wrote:

> On Tue, Jan 27, 2009 at 3:45 PM, Johan Ekh <ekh.johan at gmail.com> wrote:
> > Thank you Robert,
> > but what if I just want to create an array interactively, e.g. like m  =
> > array([1.0, 2.0, 3.0]), and pass it
> > to my program? I tried extending optparse with a new type as explained in
> > the link you gave me
> > but I was not able to get it to work. Is it really neccessary follow that
> > route just to pass an array?
> > Lot's of people must have done this before!
>
> Normally command line applications accept
> a number of arguments which are available
> in sys.argv
>
> cheers
> James
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090127/4f22d72a/attachment-0001.html>


More information about the Python-list mailing list