From: dirknbr on 28 Jun 2010 09:30 I get an int object is not callable TypeError when I execute this. But I don't understand why. parser = optparse.OptionParser("usage: %lines [options] arg1") parser.add_option("-l", "--lines", dest="lines", default=10, type="int", help="number of lines") parser.add_option("-t", "--topbottom", dest="topbottom", default="T", type="str", help="T(op) or B(ottom)") (options, args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") lines=options.lines tb=options.topbottom Dirk lines(args[0],topbottom=tb,maxi=lines)
From: Simon Brunning on 28 Jun 2010 09:51 On 28 June 2010 14:30, dirknbr <dirknbr(a)gmail.com> wrote: > I get an int object is not callable TypeError when I execute this. But > I don't understand why. > (snip) > Â Â lines=options.lines Here you are assigning the -l option to the name 'lines'. > Â Â lines(args[0],topbottom=tb,maxi=lines) Here you are attempting to call a function with the name 'lines'. But 'lines' has been assigned an integer above. I'm assuming that elsewhere you've defined a function called 'lines'. You'll need to call either the function or the option by another name. -- Cheers, Simon B.
From: Michele Simionato on 28 Jun 2010 11:44 On Jun 28, 3:30 pm, dirknbr <dirk...(a)gmail.com> wrote: > I get an int object is not callable TypeError when I execute this. But > I don't understand why. > > parser = optparse.OptionParser("usage: %lines [options] arg1") > parser.add_option("-l", "--lines", dest="lines", > default=10, type="int", > help="number of lines") > parser.add_option("-t", "--topbottom", dest="topbottom", > default="T", type="str", > help="T(op) or B(ottom)") > > (options, args) = parser.parse_args() > if len(args) != 1: > parser.error("incorrect number of arguments") > lines=options.lines > tb=options.topbottom > > Dirk > lines(args[0],topbottom=tb,maxi=lines) optparse is so old-fashioned. Use plac! $ cat x.py import plac @plac.annotations( lines=('number of lines', 'option', 'l', int), topbottom=('T(op) or B(ottom)', 'option', 't', str, 'TB')) def main(arg, lines=10, topbottom='T'): print arg, lines, topbottom if __name__ == '__main__': plac.call(main) $ python x.py -h usage: x.py [-h] [-l 10] [-t T] arg positional arguments: arg optional arguments: -h, --help show this help message and exit -l 10, --lines 10 number of lines -t T, --topbottom T T(op) or B(ottom) (see http://pypi.python.org/pypi/plac)
From: Ben Finney on 28 Jun 2010 15:56 Michele Simionato <michele.simionato(a)gmail.com> writes: > optparse is so old-fashioned. Use plac! The OP should be made aware that: * plac is a third-party library with (TTBOMK) no prospect of inclusion in the standard library * optparse is in the standard library and has been for many versions * argparse is a third-party library that is now accepted for inclusion in the standard library, intended as a full optparse replacement <URL:http://www.python.org/dev/peps/pep-0389/> Choose accordingly. -- \ “If consumers even know there's a DRM, what it is, and how it | `\ works, we've already failed.” —Peter Lee, Disney corporation, | _o__) 2005 | Ben Finney
From: rantingrick on 28 Jun 2010 17:47
On Jun 28, 10:44 am, Michele Simionato <michele.simion...(a)gmail.com> wrote: > optparse is so old-fashioned. Use plac! Hog wash! Both are archaic and asinine. Both clog your scripts with wasted lines and your memory with complex interfaces far worse than colon clogging junk food can hold a candle to. Give your *script* an enema, and do *yourself* a favor by harnessing the simplistic elegance of the "optphart" module instead! #-- Start Script --# def optphart(args): d = {'args':[]} for arg in args: if arg.startswith('-'): key, value = arg.split('=', 1) options = value.split(',') if len(options) > 1: d[key[1:]] = options else: d[key[1:]] = [value] else: d['args'].append(arg) return d if __name__ == '__main__': args = [ 'nakedArgumentOne', 'nakedArgumentTwo', '-file=aSingleFile.txt', '-files=aFileOne.txt,aFileTwo.txt', '-options=Op1,Op2,Op3,Op4', '-help=this_is_some_unhelpful_help', ] opts = optphart(args) #print opts.keys() for k,v in opts.items(): print '%s -> %s' %(k, v) #-- End Script --# |