473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 2944
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...

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.