473,757 Members | 9,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get all the variables in a python shell

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.lo cals() 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!
Jun 27 '08
15 2172
Your project interests me. Actually I was thinking about doing the
same. I hadn't worked on it at all, but I though about it and had the
idea about reading the session namespace directly, which I though
would be stored in the __dict__ attribute of something.

After reading your post, I have been trying a little bit, and I have
found a way to do it with ipython. If you open an ipython console,
press _ then hit TAB, you'll see it stores some useful information,
including all input, all output, and after some searching, a
dictionary matching all variables to its values.

__IPYTHON__.use r_ns

There is a little extra stuff in there that you don't want, but that
can be easily filtered (the extra stuff is either 'In', 'Out', 'help'
or starts with '_'). I've tried it, and you can change the value in
that dict to alter the value of the real variable. Say you have a
variable 'test':

test=5
__IPYTHON__.use r_ns['test']=4
print test #prints 5

If I get it right, python is a dynamic language, and you won't break
things by messing around with its inner stuff like this, but you
better check it.

Is this what you had in mind?
Jun 27 '08 #11
I meant it prints 4, which means the value of test is modified by the
access to the dict
test=5
__IPYTHON__.use r_ns['test']=4
print test #prints 4
Jun 27 '08 #12
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 SetWindow2Local s(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(local vars):
__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(ob j):
__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...@gmai l.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.lo cals() 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!
Jun 27 '08 #13
Lie
lixinyi...@gmai l.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.lo cals() 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!
In all kinds of code, it's best to seperate the workers code and the
UI code, in your case, you should create a backend (worker), which is
a class that stands on its own, and two foreground class (UI) that is
completely independent of each other but have the same interface. The
backend code would have an event that is raised when it is changed to
notify the UI (esp. The gui one) that it has changed since last time,
possibly passing info on what have been changed. The front ends, would
watch for this event as necessary (I don't think it is necessary for
the command line to watch this event) and react to it as necessary
like refreshing the view. The front-end window may only call functions
on the backend to interact with the data being worked on (in short the
backend class is opaque).

This obliviate the need to share data between the two (or more)
windows (UI) because all the data are contained in the backend class
that the frontend can't access directly.
Jun 27 '08 #14
Lie
On Jun 2, 1:29*am, Lie <Lie.1...@gmail .comwrote:
lixinyi...@gmai l.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.lo cals() 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!

In all kinds of code, it's best to seperate the workers code and the
UI code, in your case, you should create a backend (worker), which is
a class that stands on its own, and two foreground class (UI) that is
completely independent of each other but have the same interface. The
backend code would have an event that is raised when it is changed to
notify the UI (esp. The gui one) that it has changed since last time,
possibly passing info on what have been changed. The front ends, would
watch for this event as necessary (I don't think it is necessary for
the command line to watch this event) and react to it as necessary
like refreshing the view. The front-end window may only call functions
on the backend to interact with the data being worked on (in short the
backend class is opaque).

This obliviate the need to share data between the two (or more)
windows (UI) because all the data are contained in the backend class
that the frontend can't access directly.
To clarify what I meant, the front ends should never contain any
working data except the ones needed for the UI to illustrate what it
wanted to show at the moment, and even then, it is accessed in read
only fashion.

And actually because of Python's Global Interpreter Lock, which means
that your program would all be contained in the same python
interpreter instance (unless you do some workarounds), passing objects/
lists around between python program is cheap because they're just a
"reference" passing (like pointer passing in C/C++)

This approach is a simple "server-client" method (not a true server-
client method though, since a true one cannot share unserialized
data), and is extremely scalable, it's easy to add a third window for
example, there is no need for every front end to be aware that there
are other front ends, since it just watches for the "Changed" event
from the backend.
Jun 27 '08 #15
Lie
On May 29, 1:47*pm, lixinyi...@gmai l.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.lo cals() 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!
As an addition: Don't try to share data between windows, it's messy,
fragile, and easy to make bugs.

PS: Do not confuse Lie (Me) and Lee (OP)
Jun 27 '08 #16

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

Similar topics

4
3848
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
20
5193
by: Matthew Thorley | last post by:
My friend sent me an email asking this: > I'm attemtping to decide which scripting language I should master and > was wondering if it's possible to do > these unixy awkish commands in python: > > How to find the amount of disk space a user is taking up: > > find / -user rprice -fstype nfs ! -name /dev/\* -ls | awk '{sum+=$7};\ > {print "User rprice total disk use = " sum}'
3
2228
by: Greg Lindstrom | last post by:
Hello- I am running python 2.3. on an HP-9000 box running Unix and have a POSIX script that sets up my production environment. I would like to run the script from inside a python routine and pick up the environment variables but am having trouble getting it done. Suppose the script is called setprod. How can I call this from inside a python script then pick up the env's later in my program? I've tried os.system ('setprod') and...
3
2967
by: David Durkee | last post by:
Hi, I'm trying to write a script I can run from tcsh in Terminal (on Mac OS X) that will set environment variables that can be accessed by subsequent commands I execute in that session. Not having any luck so far. Here's what I've tried: ------------ #!/usr/bin/python
2
4545
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the the function due to its
30
2914
by: bblais | last post by:
Hello, Let me start by saying that I am coming from a background using Matlab (or Octave), and C++. I am going to outline the basic nuts-and-bolts of how I work in these languages, and ask for some help to find out how the same thing is done in Python. I am not sure what the standard is. In C++, I open up an editor in one window, a Unix shell in another. I write the code in the editor, then switch to the shell window for compile...
5
2835
by: Maxim Veksler | last post by:
Hello list, I'm trying to write a python script that would allow me to manipulate shell variables of the calling shell. I'm trying to write some logic that would know to add LD_LIBRARY_PATH to the users environment. In bash this is done with "export", can I do this with python? (If at all possible because python is actually a sub process of bash). Thank you,
1
1134
by: Lee | last post by:
Hi, thank your for your reply. I will try iPython. I did try sage for a while, but I found it quite heavy, and I'm not sure whether it's easy to expand like python or not. New libraries can be easily imported in python, and those libraries could be build in almost any popular computer language. Can sage do that? The reason why I want to work on this is the same with you. I'm an automotive engineer. What I need is a powerful
1
3270
by: Fredrik Lundh | last post by:
John Lawrence wrote: doesn't exactly work for Python scripts, though: $ cat env.py #!/usr/bin/env python import os os.environ = "hello"
0
9489
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10072
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.