Accessing global namespace from module

R

reubendb

Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?

Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:

mainscript.py:
---------------------------
AddPlot("scatter", "coordinate")
# set other things here
DrawPlots()

it works fine. But I want to be able to do this:

myModule.py:
----------------------
def defaultScatterPlot():
AddPlot("scatter", "coordinate")
#do other things
DrawPlots()

and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaultScatterPlot()

This won't work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?

Thank you in advance for any help.
RDB
 
K

kyosohma

Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?

Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:

mainscript.py:
---------------------------
AddPlot("scatter", "coordinate")
# set other things here
DrawPlots()

it works fine. But I want to be able to do this:

myModule.py:
----------------------
def defaultScatterPlot():
AddPlot("scatter", "coordinate")
#do other things
DrawPlots()

and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaultScatterPlot()

This won't work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?

Thank you in advance for any help.
RDB

I think you're doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.

<code>

commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args, *kwargs):
# Do something
---------------------


mainProgram.py
------------------------
from commonMods import AddPlot
AddPlot("scatter", "coordinate")
# etc etc
-------------------------

myModule.py
------------------------
from commonMods import AddPlot
AddPlot("scatter", "coordinate")
# etc etc
-------------------------

</code>

Hope that helps.

Mike
 
R

reubendb

<snip>

I think you're doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.

<code>

commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args, *kwargs):
# Do something
---------------------

Hi Mike,
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.

Thanks.
RDB
 
G

Gabriel Genellina

The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.

Don't you have some import statements at the top of mainscript.py that are
responsible for bringing AddPlot and DrawPlots into the current namespace?
Import the same things in your second module.
 
G

Gabriel Genellina

The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.

Don't you have some import statements at the top of mainscript.py that are
responsible for bringing AddPlot and DrawPlots into the current namespace?
Import the same things in your second module.
 
R

reubendb

Don't you have some import statements at the top of mainscript.py that are
responsible for bringing AddPlot and DrawPlots into the current namespace?
Import the same things in your second module.

No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?

Thanks.
RDB
 
G

Gabriel Genellina

No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?

Yes: create your own module on-the-fly, using the recipe posted earlier by
John Krukoff.
If there are many functions, try enumerating them all:

import sys
from types import ModuleType as module

plotModule = module('plot')
for key,value in globals().items():
if key[:2] != '__':
setattr(plotModule, key, value)

sys.modules['plot'] = plotModule
 
G

Gabriel Genellina

No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?

Yes: create your own module on-the-fly, using the recipe posted earlier by
John Krukoff.
If there are many functions, try enumerating them all:

import sys
from types import ModuleType as module

plotModule = module('plot')
for key,value in globals().items():
if key[:2] != '__':
setattr(plotModule, key, value)

sys.modules['plot'] = plotModule
 
R

Reuben D. Budiardja

No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?

Yes: create your own module on-the-fly, using the recipe posted earlier by
John Krukoff.
If there are many functions, try enumerating them all:

import sys
from types import ModuleType as module

plotModule = module('plot')
for key,value in globals().items():
if key[:2] != '__':
setattr(plotModule, key, value)

sys.modules['plot'] = plotModule

Great ! That seems to work, thanks a lot.
One last question. Do I have to do this for ever script I write, or can I put
this into separate file and "include" it somehow ?
I am going to have several mainscripts.py, and all is going to import myModule
that will need access to this plots subroutine. It'll be great if I can put
this trick on a single file that is included by the main scripts, to avoid
violating DRY principle.

Thanks for all the help.
RDB
 
G

Gabriel Genellina

En Mon, 11 Jun 2007 22:19:15 -0300, Reuben D. Budiardja
One last question. Do I have to do this for ever script I write, or can
I put
this into separate file and "include" it somehow ?
I am going to have several mainscripts.py, and all is going to import
myModule
that will need access to this plots subroutine. It'll be great if I can
put
this trick on a single file that is included by the main scripts, to
avoid
violating DRY principle.

According to your description on how things work, you will need the
globals() from mainscript.py; try this:

--- mainscript.py ---
import plot_setup
plot_setup.setup(globals())
.... rest of script, perhaps importing myModule.py ...

--- plot_setup.py ---
import sys
from types import ModuleType as module

def setup(namespace):
plotModule = module('plot')
for key,value in namespace.items():
if key[:2] != '__':
setattr(plotModule, key, value)
sys.modules['plot'] = plotModule

--- myModule.py ---
import plot

def do_work():
plot.DrawPlot(...)
...

(Having to type two lines at the top of your main scripts doesn't look so
bad...)
 
R

Reuben D. Budiardja

En Mon, 11 Jun 2007 22:19:15 -0300, Reuben D. Budiardja


According to your description on how things work, you will need the
globals() from mainscript.py; try this:
Hi Gabriel,
This works great ! Thanks a lot (and to John Krukoff too).

RDB
 
R

Reuben D. Budiardja

En Mon, 11 Jun 2007 22:19:15 -0300, Reuben D. Budiardja
According to your description on how things work, you will need the
globals() from mainscript.py; try this:
--- plot_setup.py ---
import sys
from types import ModuleType as module

def setup(namespace):
plotModule = module('plot')
for key,value in namespace.items():
if key[:2] != '__':
setattr(plotModule, key, value)
sys.modules['plot'] = plotModule

Hm.. apparently this works for function but doesn't work for method :(. Do you
know of any reason ?

Thanks.
RDB
 
G

Gabriel Genellina

En Tue, 12 Jun 2007 18:37:45 -0300, Reuben D. Budiardja
import sys
from types import ModuleType as module

def setup(namespace):
plotModule = module('plot')
for key,value in namespace.items():
if key[:2] != '__':
setattr(plotModule, key, value)
sys.modules['plot'] = plotModule

Hm.. apparently this works for function but doesn't work for method :(.
Do you
know of any reason ?

Do you mean, it doesn't work for classes? What kind of error do you get?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top