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 4402
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
Sweet! Thanks!
Mike
On Jan 7, 8:30 am, "Guilherme Polo" <ggp...@gmail.comwrote:
>
globals() =)
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
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__. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
by: Fernando Rodriguez |
last post by:
Hi,
How can I get the list of all the functions defined in the current module?
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |