473,396 Members | 1,792 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,396 software developers and data experts.

Uplevel functionality

Hello,

I would like to know if there is any possibility in Python to execute
arbitrary scripts in the context higher in the stack frame.

In essence, I would like to have the equivalent of the following Tcl code:

proc repeat {n script} {
for {set i 0} {$i < $n} {incr i} {
uplevel $script
}
}

This allows me to do:

repeat 5 {puts "hello"}

prints:
hello
hello
hello
hello
hello

Or this:

set i 10
repeat 5 {incr i}
puts $i

prints:
15

That second example shows that the script provided as a second parameter
to the "repeat" procedure (the script is "incr i") is executed in the
context where the procedure was called, not locally in the procedure itself.

The strongest analogy to the above repeat procedure in Tcl would be a
hypothetical Python function:

def repeat(n, script):
for i in xrange(n):
EVALUATE script HIGHER IN THE STACK #???
Thank you very much,

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

Jul 18 '05 #1
5 2068
In article <bs**********@nemesis.news.tpi.pl>, Maciej Sobczak
<no*****@no.spam.com> writes

How about this, apparently f_locals/globals are readonly so you won't be
allowed arbitrary scopes.

############################
C:\tmp>cat repeat.py
def repeat(n, script):
from sys import _getframe
f = _getframe(1)
L = f.f_locals
G = f.f_globals
for i in xrange(n):
exec script in G, L

def test():
i = 0
repeat(3,'print i;i+=5')

if __name__=='__main__':
test()

C:\tmp>repeat.py
0
5
10

C:\tmp>
############################
Hello,

I would like to know if there is any possibility in Python to execute
arbitrary scripts in the context higher in the stack frame.

In essence, I would like to have the equivalent of the following Tcl code:

proc repeat {n script} {
for {set i 0} {$i < $n} {incr i} {
uplevel $script
}
}

This allows me to do:

repeat 5 {puts "hello"}

prints:
hello
hello
hello
hello
hello

Or this:

set i 10
repeat 5 {incr i}
puts $i

prints:
15

That second example shows that the script provided as a second parameter
to the "repeat" procedure (the script is "incr i") is executed in the
context where the procedure was called, not locally in the procedure itself.

The strongest analogy to the above repeat procedure in Tcl would be a
hypothetical Python function:

def repeat(n, script):
for i in xrange(n):
EVALUATE script HIGHER IN THE STACK #???
Thank you very much,


--
Robin Becker
Jul 18 '05 #2
In article <X6**************@jessikat.fsnet.co.uk>, Robin Becker
<ro***@jessikat.fsnet.co.uk> writes

However, by using an exec '' the following can be made to work. The exec
is used to disable the unwanted optimisation of print k into a name
error.

##################################
def repeat(n, script):
from sys import _getframe
f = _getframe(1)
L = f.f_locals
G = f.f_globals
for i in xrange(n):
exec script in G, L

def test():
i = 0
repeat(3,'print i;i+=5')
exec ''
repeat(3,'k=2')
print k

if __name__=='__main__':
test()
##################################
In article <bs**********@nemesis.news.tpi.pl>, Maciej Sobczak
<no*****@no.spam.com> writes

How about this, apparently f_locals/globals are readonly so you won't be
allowed arbitrary scopes.

############################
C:\tmp>cat repeat.py
def repeat(n, script):
from sys import _getframe
f = _getframe(1)
L = f.f_locals
G = f.f_globals
for i in xrange(n):
exec script in G, L

def test():
i = 0
repeat(3,'print i;i+=5')

if __name__=='__main__':
test()

C:\tmp>repeat.py
0
5
10

C:\tmp>
############################
Hello,

I would like to know if there is any possibility in Python to execute
arbitrary scripts in the context higher in the stack frame.

In essence, I would like to have the equivalent of the following Tcl code:

proc repeat {n script} {
for {set i 0} {$i < $n} {incr i} {
uplevel $script
}
}

This allows me to do:

repeat 5 {puts "hello"}

prints:
hello
hello
hello
hello
hello

Or this:

set i 10
repeat 5 {incr i}
puts $i

prints:
15

That second example shows that the script provided as a second parameter
to the "repeat" procedure (the script is "incr i") is executed in the
context where the procedure was called, not locally in the procedure itself.

The strongest analogy to the above repeat procedure in Tcl would be a
hypothetical Python function:

def repeat(n, script):
for i in xrange(n):
EVALUATE script HIGHER IN THE STACK #???
Thank you very much,


--
Robin Becker
Jul 18 '05 #3
Maciej Sobczak <no*****@no.spam.com> wrote in message news:<bs**********@nemesis.news.tpi.pl>...
Hello,

I would like to know if there is any possibility in Python to execute
arbitrary scripts in the context higher in the stack frame.

In essence, I would like to have the equivalent of the following Tcl code:

proc repeat {n script} {
for {set i 0} {$i < $n} {incr i} {
uplevel $script
}
}

This allows me to do:

repeat 5 {puts "hello"}

prints:
hello
hello
hello
hello
hello

Or this:

set i 10
repeat 5 {incr i}
puts $i

prints:
15

That second example shows that the script provided as a second parameter
to the "repeat" procedure (the script is "incr i") is executed in the
context where the procedure was called, not locally in the procedure itself.

The strongest analogy to the above repeat procedure in Tcl would be a
hypothetical Python function:

def repeat(n, script):
for i in xrange(n):
EVALUATE script HIGHER IN THE STACK #???

Yes, it would be

def repeat(n, script):
for i in xrange(n):
exec(script, locals(), globals())

repeat(5, 'print "hello"')

i = 10
repeat(5, 'i += 1')
print i


Thank you very much,


With pleasure,
----
levi
Jul 18 '05 #4

"Maciej Sobczak" <no*****@no.spam.com> wrote in message
news:bs**********@nemesis.news.tpi.pl...
Hello,

I would like to know if there is any possibility in Python to execute
arbitrary scripts in the context higher in the stack frame.
An alternate answer from those already posted is to not create the new
context that you want to uplevel from. Seriously.
In essence, I would like to have the equivalent of the following Tcl code:
proc repeat {n script} {
for {set i 0} {$i < $n} {incr i} {
uplevel $script
}
}
As I read this, this defines the repeat function as an abbreviation of a
'for' loop. The uplevel is only needed because the abbreviating process
creates a nested frame. In Lisp, repeat would be written a text macro
which does the substitution in the same frame, and hence the frame problem
and need for uplevel would not arise. In Python, we currently live without
the pluses and minuses of macros and usually write out the for loops ;-)
This allows me to do:

repeat 5 {puts "hello"}

prints:
hello
hello
hello
hello
hello
for i in range(5): print 'hello' # does same thing.
repeat(5, "print 'hello'") # even if possible, saves all of 5 key
strokes
repeat(5, lambda: print 'hello') # possible, without uplevel, takes 2 more
Or this:

set i 10
repeat 5 {incr i}
puts $i

prints:
15


i=10
for n in range(5): i+=1
print i

In batch mode, which requires 'print ', 36 instead of 33 keystrokes.

You *can* do this in current Python:

def repeat(num, func):
for i in range(num): func()

def inci(): i[0]+=1 # need def since lambda only abbreviates 'return
expression'

i=[10]
repeat(5, inci)
print i[0]

15

Python goes for clarity rather than minimal keystrokes. Example:
It has been proposed that 'for i in n:' be equivalent to 'for i in
range(n):'.
Given the set theory definition n == {0, ..., n-1} (==set(range(n))),
this is theoretically sensible, but was judged too esoteric for most
people.

Terry J. Reedy
Jul 18 '05 #5
On Wed, 31 Dec 2003 12:41:02 -0500, rumours say that "Terry Reedy"
<tj*****@udel.edu> might have written:
This allows me to do:

repeat 5 {puts "hello"}

prints:
hello
hello
hello
hello
hello


for i in range(5): print 'hello' # does same thing.
repeat(5, "print 'hello'") # even if possible, saves all of 5 key
strokes
repeat(5, lambda: print 'hello') # possible, without uplevel, takes 2 more


print is invalid in a lambda (like other statements) as you most surely
know. I would guess the champagne is to blame? :-) Happy new year!
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
Jul 18 '05 #6

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

Similar topics

2
by: Vikas | last post by:
Hi y'all, I need logging functionality to provide me following: - logging levels that can be set at run-time. - able to output to different files for different levels - thread safe - line,...
2
by: Tom | last post by:
Hello, I'm looking for a tool that would allow me to create a web page with the spreadsheet like functionality. Basically, I want to be able to type in a number in the cell and have all totals...
25
by: KK | last post by:
Hi, I am using history.go(-1) for implementing the back button functionality. Its working fine but with this exception. 1. The page which is having back button has some hyperlinks on it. ...
0
by: blongmire | last post by:
OS: WinXP Home Access 2000 Word 2000 I loaded 1998 vintage 16-bit business mapping software onto a PC and I think it overlayed some OS files dealing with DDE functionality. After that software...
0
by: rellik | last post by:
Hi All, I've run into a problem with the MenuStrip control and any help would be greatly appreciated! The problem I've got is that when use a control derived from the MenuStrip class all MDI...
1
by: Srini | last post by:
How do I do a functionality for a ASP.NET web application? Is there an automated way to do this? Can ACT do functionality test apart from the stress tests? When I make some change to the...
11
by: MadMonk | last post by:
Hi, I need to write a small Windows application (console or forms) (for Windows XP SP2 & Windows Server 2003), which will check a folder for the presence of some files, and if they exist, it...
23
by: Dautkhanov | last post by:
Hello ! Does anybody have cutted version of prototype.js with the AJAX functionality only? I am a new in prototype.js topic, so I think this task should be done by other developers. Maybe...
1
by: Monty | last post by:
I know this isn't necessarily an ASP.Net question, but it is web programming and I intend to try it using ASP.Net, I just need to get pointed in the right direction. I've seen some sites that allow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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,...

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.