Accessing global namespace from module

2007-06-11 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing global namespace from module

2007-06-11 Thread reubendb
On Jun 11, 1:37 pm, [EMAIL PROTECTED] wrote:
> On Jun 11, 11:02 am, reubendb <[EMAIL PROTECTED]> wrote:
>
>
>
> > 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 ?
> 
>
> 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.
>
> 
>
> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing global namespace from module

2007-06-11 Thread reubendb
On Jun 11, 3:30 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <[EMAIL PROTECTED]> escribió:
>
> > 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.

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

-- 
http://mail.python.org/mailman/listinfo/python-list

Redirecting stderr to null and revert

2007-08-06 Thread reubendb
Hello,
I have some function that output too much stuff to stderr when called,
and I have no control over the function itself. So I thought as a
workaround, I redirect stderr to /dev/null before calling that
function, and then revert the stderr back after calling that
function.
I have something like this:

def nullStderr():
  sys.stderr.flush()
  err = open('/dev/null', 'a+', 0)
  os.dup2(err.fileno(), sys.stderr.fileno())

def revertStderr():
  sys.stderr = sys.__stderr__

Then when calling the function, I want to do:
nullStderr()
noisyFunction(foo, bar)
revertStderr()

The problem is the "revertStderr()" doesn't work, ie. I still get no
stderr back after calling it (stderr still redirects to /dev/null). I
want stderr back so that when the script fails in different places
later I can get error messages. What am I missing here ? Thanks a lot
for any help.

RDB

-- 
http://mail.python.org/mailman/listinfo/python-list