is there a nmore pythonic way ?
A. Lloyd Flanagan
alloydflanagan at attbi.com
Tue Mar 11 12:00:28 EST 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:
A zero-length string evaluates to false, so you can just:
if not sys.argv[1:]:
> ## print help information and exit:
> usage()
> sys.exit(2)
>
> im = '.'.join(sys.argv[1].split('.')[:-1])
> func = sys.argv[1].split('.')[-1:][0]
How about
pos = sys.argv[1].rfind('.')
im, func = sys.argv[1][:pos], sys.argv[1][pos+1:]
I don't know if that's more pythonic, but I found it a lot more
readable than using split and join in this case.
>
> exec("from %s import %s" % (im, func))
>
> -> and then func()
>
> is there a more correct way to do it ?
>
> thanks,
You're welcome. I'm hoping somebody will post a different solution I
haven't thought of :).
More information about the Python-list
mailing list