I want to give a user the possibility of "restarting" an interactive
session, by removing all the objects defined by her since the
beginning. The way I make this possible is by having a "function"
that can be called during the interactive session using locals() as an
argument, as follows:
restart(locals())
It works. However, I would like to make this "friendlier", i.e.
requiring the user to simply type
restart()
and have the code extract out the value of locals() of the "parent"
namespace. I thought it might be possible using sys._getframe, but I
have not been able to figure out why.
Any help would be appreciated.
André
==================
The code below defines the class I used. The interpreter is started
via
#--
exec user_code in symbols
#--
where user_code is
#--
interp = SingleConsole()
interp.interact()
#--
# InteractiveConsole is very similar to the class of the same name
# in module code.py of the standard library
class SingleConsole(InteractiveConsole):
'''SingleConsole are isolated one from another'''
def __init__(self, locals={}, filename="Isolated console"):
self.locals = locals
self.locals['restart'] = self.restart
InteractiveConsole.__init__(self, self.locals,
filename=filename)
def restart(self, loc):
"""Call this function as follows: restart(locals())
Used to restart an interpreter session, removing all
variables
and functions introduced by the user, but leaving Crunchy
specific
ones in."""
to_delete = set()
# We can't iterate over a dict while changing its size; we do
it
# in two steps; first identify the objects to be deleted while
# iterating over the dict; then iterate over a set while
removing
# the objects in the dict.
for x in loc:
if x not in ['__builtins__', 'crunchy', 'restart']:
to_delete.add(x)
for x in to_delete:
del loc[x]
return 2 2892
On Fri, 2007-07-27 at 20:46 +0000, André wrote:
I want to give a user the possibility of "restarting" an interactive
session, by removing all the objects defined by her since the
beginning. The way I make this possible is by having a "function"
that can be called during the interactive session using locals() as an
argument, as follows:
restart(locals())
It works. However, I would like to make this "friendlier", i.e.
requiring the user to simply type
restart()
and have the code extract out the value of locals() of the "parent"
namespace. I thought it might be possible using sys._getframe, but I
have not been able to figure out why.
inspect.currentframe(1).f_locals
The same caveats for locals() apply here: Modifications to this
dictionary may or may not be visible to the caller.
HTH,
--
Carsten Haese http://informixdb.sourceforge.net
On Jul 27, 6:01 pm, Carsten Haese <cars...@uniqsys.comwrote:
On Fri, 2007-07-27 at 20:46 +0000, André wrote:
I want to give a user the possibility of "restarting" an interactive
session, by removing all the objects defined by her since the
beginning. The way I make this possible is by having a "function"
that can be called during the interactive session using locals() as an
argument, as follows:
restart(locals())
It works. However, I would like to make this "friendlier", i.e.
requiring the user to simply type
restart()
and have the code extract out the value of locals() of the "parent"
namespace. I thought it might be possible using sys._getframe, but I
have not been able to figure out why.
inspect.currentframe(1).f_locals
The same caveats for locals() apply here: Modifications to this
dictionary may or may not be visible to the caller.
HTH,
--
Carsten Haesehttp://informixdb.sourceforge.net
Thanks, it worked!
André This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: RR |
last post by:
I have two classes:
class Base
{
function Insert()
{
// does stuff
}
}
|
by: balg |
last post by:
An ActiveX control has a call to "UserControl.Parent" which
fails when it is interopped and added to a .Net Form.
Is there any way to access to .Net parent control/it's interfaces
from VB6...
|
by: nicver |
last post by:
I am fixing a client's Web site and I do not have the time to reprogram
things my way, and the last hurdle I have is with a piece of javascript
embedded in an ASP snippet.
The payment form is...
|
by: Bill C |
last post by:
I've tried to google an explanation, but can't find one:
I'm obviously don't know dhtml, but need to clean up a misbehaving line of
code in a CGI.
Does the "0" value of "parent.frames"...
|
by: Maansi Sanghi |
last post by:
Hello,
(1) I am building a com component with multiple interfaces in .NET Managed
VC++
(2) Then use the managed .NET dll in unmanaged code after registering
through regasm and getting the...
|
by: Ray Schumacher |
last post by:
What is the feeling on using "parent" in a class definition that
class methods can refer to, vs. some other organization ?
Should all relevant objects/vars just be passed into the method as needed?...
|
by: Mo |
last post by:
Hi,
I am not sure if this is the right group but I have a web control
within a page and I am trying to access a method from the control. My
master page is set as :
public partial class...
|
by: syvman |
last post by:
Hi... I am working on a simple program (in VB.net) that will grab the
values of elements out of an XML file that are tagged as
"databasePath"... My program works, but I am getting too many...
|
by: eBob.com |
last post by:
How does a "sub-form", i.e. one invoked by another form, determine anything
about the form which brought it into existence, i.e., I suppose,
instantiated it? I wanted to so something like this ......
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |