is there a nmore pythonic way ?

Brandon Beck bbeck13 at excite.com
Tue Mar 11 20:00:08 CET 2003


"shagshag13" <shagshag13 at yahoo.fr> wrote in message news:<3e6dbc5e$0$2688$626a54ce at news.free.fr>...
> hello,
> 
> i need to use module.function passed from command line, by now i use :
> 
> if __name__ == '__main__':
>  args = sys.argv[1:]
>  if len(args) == 0:
>   ## print help information and exit:
>   usage()
>   sys.exit(2)
> 
>  im = '.'.join(sys.argv[1].split('.')[:-1])
>  func = sys.argv[1].split('.')[-1:][0]
> 
>  exec("from %s import %s" % (im, func))


Perhaps this will help:

>>> x = "a.b.c.d"
>>> x[:x.rfind(".")]
'a.b.c'
>>> x[x.rfind(".")+1:]
'd'

Be careful though, rfind could return -1 if what you're searching for isn't found.


Brandon




More information about the Python-list mailing list