[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

How to refer to the current module?

Mike

1/7/2008 1:22:00 PM

I want to do something like the following (let's pretend that this is
in file 'driver.py'):

#!/bin/env python

import sys

def foo():
print 'foo'

def bar(arg):
print 'bar with %r' % arg

def main():
getattr(driver, sys.argv[1])(*sys.argv[2:])

if __name__=='__main__':
main()


Essentially what I'm trying to get at here is dynamic function
redirection, like a generic dispatch script. I could call this as

python driver.py foo

or

python driver.py bar 15

and at any time later I can add new functions to driver.py without
having to update a dispatch dict or what-have-you.

The problem is, 'driver' doesn't exist in main() line 1. If I 'import
driver' from the command line, then getattr(driver, ...) works, but
it's not bound here.

Is there any way around this? Can I somehow scope the 'current
module' and give getattr(...) an object that will (at run time) have
the appropriate bindings?

Thanks in advance for all advice!

Mike
4 Answers

Guilherme Polo

1/7/2008 1:31:00 PM

0

2008/1/7, Mike <ckimyt@gmail.com>:
> I want to do something like the following (let's pretend that this is
> in file 'driver.py'):
>
> #!/bin/env python
>
> import sys
>
> def foo():
> print 'foo'
>
> def bar(arg):
> print 'bar with %r' % arg
>
> def main():
> getattr(driver, sys.argv[1])(*sys.argv[2:])
>
> if __name__=='__main__':
> main()
>
>
> Essentially what I'm trying to get at here is dynamic function
> redirection, like a generic dispatch script. I could call this as
>
> python driver.py foo
>
> or
>
> python driver.py bar 15
>
> and at any time later I can add new functions to driver.py without
> having to update a dispatch dict or what-have-you.
>
> The problem is, 'driver' doesn't exist in main() line 1. If I 'import
> driver' from the command line, then getattr(driver, ...) works, but
> it's not bound here.
>
> Is there any way around this? Can I somehow scope the 'current
> module' and give getattr(...) an object that will (at run time) have
> the appropriate bindings?

globals() =)

>
> Thanks in advance for all advice!
>
> Mike
> --
> http://mail.python.org/mailman/listinfo/p...
>


--
-- Guilherme H. Polo Goncalves

Mike

1/7/2008 1:46:00 PM

0

Sweet! Thanks!

Mike

On Jan 7, 8:30 am, "Guilherme Polo" <ggp...@gmail.com> wrote:
>
> globals() =)
>

Christian Heimes

1/7/2008 2:48:00 PM

0

Mike wrote:
> Is there any way around this? Can I somehow scope the 'current
> module' and give getattr(...) an object that will (at run time) have
> the appropriate bindings?

globals() for the current name space

import sys
sys.modules[__name__] gets you the module object

Christian

Stargaming

1/7/2008 11:40:00 PM

0

On Mon, 07 Jan 2008 05:21:42 -0800, Mike wrote:

> I want to do something like the following (let's pretend that this is in
> file 'driver.py'):
>
> #!/bin/env python
>
> import sys
>
> def foo():
> print 'foo'
>
> def bar(arg):
> print 'bar with %r' % arg
>
> def main():
> getattr(driver, sys.argv[1])(*sys.argv[2:])
>
> if __name__=='__main__':
> main()
>
>
> Essentially what I'm trying to get at here is dynamic function
> redirection, like a generic dispatch script. I could call this as
>
> python driver.py foo
>
> or
>
> python driver.py bar 15
>
> and at any time later I can add new functions to driver.py without
> having to update a dispatch dict or what-have-you.
>
> The problem is, 'driver' doesn't exist in main() line 1. If I 'import
> driver' from the command line, then getattr(driver, ...) works, but it's
> not bound here.
>
> Is there any way around this? Can I somehow scope the 'current module'
> and give getattr(...) an object that will (at run time) have the
> appropriate bindings?
>
> Thanks in advance for all advice!
>
> Mike

`__main__` (after you did ``import __main__``) could be an option as
well. But I'd prefer a custom dictionary for your dispatch, rather than
some magic with the module's names.

See http://docs.python.org/ref/pro... for details on __main__.