472,783 Members | 950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

Is access to locals() of "parent" namespace possible?

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

Jul 27 '07 #1
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
Jul 27 '07 #2
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é

Jul 27 '07 #3

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

Similar topics

3
by: RR | last post by:
I have two classes: class Base { function Insert() { // does stuff } }
2
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...
4
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...
2
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"...
0
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...
6
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?...
2
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...
2
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...
3
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 ......
0
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...
0
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...
0
linyimin
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...
0
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...
14
DJRhino1175
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...
0
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...
5
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...
0
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=()=>{
2
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...

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.