How to get all the variables in a python shell

Alan J. Salmoni salmoni at gmail.com
Sat May 31 10:27:48 EDT 2008


I'm not certain if this is what you want but try this for the first
window:

import __main__
localvars = __main__.__dict__
dir(localvars) # lists names of all objects available to the
interpreter.

And then you can pass localvars anywhere in the program - so after a
command is entered in one window, you can import the all locals into
the other by putting this function in the second window:

def SetWindow2Locals(localvars):
    __main__.__dict__ = localvars

and do the reverse by sending the updated localvars from window 2 to
window 1. Setting up an object between the 2 windows should be enough:

# window 1, must be created first
import __main__
localvars = __main__.__dict__
# create second window (say, win2)
win2.SetLocals(localvars)

in win2 after creation, there is a method/function:

SetLocals(localvars):
    __main__.__dict__ = localvars

If you want to store the object in something like a DB, you can store
this localvars dictionary rather than the actual objects. This should
be efficient because it stores the object name and ID only but not the
objects themselves. However, if the objects are deleted, after saving
and before re-loading, you may get some odd results but I'm not sure.
I would be very careful of using this in the wild.

You can also add objects singly to the dictionary by hand with a
function:

def importobject(obj):
    __main__.__dict__[obj.__name__] = obj

Which should make them available in the other window easily. If a new
object is added to the __main__.__dict__, just grab it and send it to
this function. Make sure that you send the object and not just its
name because the name is only a string.

Sorry if this is not what you were after.

Alan

On May 29, 2:47 pm, lixinyi... at gmail.com wrote:
> Hi!
>
> I'm currently working on a scientific computation software built in
> python.
> What I want to implement is a Matlab style command window <->
> workspace interaction.
>
> For example, you type 'a=1' in the command window, and you see a list
> item named 'a' in the workspace.
> You double click the icon of the item, and you see its value. You can
> modify the value of the list item,
> 1 -> 100 etc,  after which if you go back to the command window and
> type 'a'  and press enter, you see that
> varable a's value has been changed to 100.
>
> So my question is : if you have two DOS command windows running under
> WINDOWS OS, how can you make them share the same internal variable
> buffer? Or is there any easier way to implement such kind of
> interaction?
>
> Maybe I could just build a small database to store all the values and
> access them from both programs, but chances are sometimes I have to
> deal with big arrays, and they will eat extra memory if I keep them in
> a database. Is there anyway to access a shell's local memory buffer?
> I tried to use shell.interp.locals() in wxPython, but there's too many
> variables in the list which I don't actually need.
>
> Come on guys, give me some ideas. Thanks in advance!




More information about the Python-list mailing list