473,654 Members | 3,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ()
#--
# InteractiveCons ole is very similar to the class of the same name
# in module code.py of the standard library

class SingleConsole(I nteractiveConso le):
'''SingleConsol e are isolated one from another'''
def __init__(self, locals={}, filename="Isola ted console"):
self.locals = locals
self.locals['restart'] = self.restart
InteractiveCons ole.__init__(se lf, self.locals,
filename=filena me)

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 2957
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.current frame(1).f_loca ls

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...@uniqsy s.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.current frame(1).f_loca ls

The same caveats for locals() apply here: Modifications to this
dictionary may or may not be visible to the caller.

HTH,

--
Carsten Haesehttp://informixdb.sour ceforge.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
4796
by: RR | last post by:
I have two classes: class Base { function Insert() { // does stuff } }
2
2800
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 ActiveX Control. Thanks, BalG
4
3092
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 named "frmPayment". The page itself is in a frame named "main". The form contains 1 or many items. The amount of each item can be modified from a drop down (this is for a classified ads system, you can select a different ad type per ad) and the...
2
3588
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" mean the frame itself? Does the 0
0
1173
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 tlb file Problem: (A) Everyrthing works fine except that I get a compile time error C2039 while trying to
6
2543
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? It seems like including "parent" in the class def is just like a class variable, which most do not recommend. An example: class LXSerial: def __init__(self, parent, debug=False): ...
2
1993
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 _Default : System.Web.UI.Page { private string Cart = ""; public string GetCart
2
10631
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 results. I need to limit the scope of the program to only include the string value from the element that has a parent of "numberone", and not both "numberone" and "numbertwo". I don't know if "numberone" is a node, or what the correct term for it is...
3
1901
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 ... MsgBox("called by " & Owner.Name) .... but that throws a null reference exception. Parent.Name and ParentForm.Name also throw null reference exceptions. Form1.Name works - but that's not very flexible.
0
8376
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
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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...
1
8489
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
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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
5622
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
4149
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
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.