473,245 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

How to refer to the current module?

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
Jan 7 '08 #1
4 4361
2008/1/7, Mike <ck****@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/python-list

--
-- Guilherme H. Polo Goncalves
Jan 7 '08 #2
Sweet! Thanks!

Mike

On Jan 7, 8:30 am, "Guilherme Polo" <ggp...@gmail.comwrote:
>
globals() =)
Jan 7 '08 #3
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

Jan 7 '08 #4
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/programs.html for details on __main__.
Jan 7 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MK | last post by:
What would be the best platform-independent way to refer to the current (execute) path for a Python script? self.dirname = ??? This variable should contain directory name for the current path....
3
by: Fernando Rodriguez | last post by:
Hi, How can I get the list of all the functions defined in the current module?
0
by: Christian Hudon | last post by:
Hi, I'd like to be able to trap lookups of attributes of the current module. Exactly like what __getattr__ does, but for modules. As an exmaple, I'd like to be able to do: File foo.py ...
1
by: 5tein | last post by:
I mean, with XSL functions such as SELECT can I only refer to XML element values or can I somehow also refer to XML attribute values? Is there any way to refer to XML content based on attribute...
4
by: solar | last post by:
How to refer to a subform ? On my main form called FrmEmployees I have a button to open a report. I want to forbid opening the report in case the liters in the subform are 0. To this end I...
4
by: Dean Slindee | last post by:
Is there a way to refer to the name of the function the code is currently within? I would like to identifiy on a MessageBox the function or sub containing the message box without hardcoding it in...
0
by: Larry Lard | last post by:
This came out of a thread explaining to "BK" about error BC42025 ("Access of shared member through an instance; qualifying expression will not be evaluated"); Frans Clasener then came up with...
6
by: Savante | last post by:
I have been writing a datalogging application. It reads in double's into a database. I want to be able to click on a row in a database (holds name of variable and also current value of variable)...
1
by: Alex | last post by:
Can I get reference to module object of current module (from which the code is currently executed)? I know __import__('filename') should probably do that, but the call contains redundant...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.