473,463 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Feedback on Until recipe

Occasionally someone posts to this group complaining about the lack of
"repeat ... until" in python. I too have occasionally wished for such
a construct, and after some thinking, I came up with the class below.
I'm hoping to get some feedback here, and if people besides me think
they might use it someday, I can put it on the python cookbook. I'm
pretty happy with it, the only ugly thing is you have to use a
lambda. Ideally i'd like to just see
while Until(i<3)
but that doesn't work.
Please tell me what you think, and thanks for your time.

Tom

class Until:
"""
>>i = 0
while Until(lambda: i<3):
... print "hello"
... i += 1
hello
hello
hello
>>while Until(lambda: i<2): #note i still equals 3 here
... print "hello"
hello
"""
yet = True
def __init__(self, mybool):
if self.__class__.yet or mybool():
self.__class__.yet = False
self.ans = True
else:
self.__class__.yet = True
self.ans = False

def __nonzero__(self):
return self.ans

Apr 24 '07 #1
7 1468
Thomas Nelson wrote:
Occasionally someone posts to this group complaining about the lack of
"repeat ... until" in python. I too have occasionally wished for such
a construct, and after some thinking, I came up with the class below.
I'm hoping to get some feedback here, and if people besides me think
they might use it someday, I can put it on the python cookbook. I'm
pretty happy with it, the only ugly thing is you have to use a
lambda. Ideally i'd like to just see
while Until(i<3)
but that doesn't work.
Please tell me what you think, and thanks for your time.

Tom

class Until:
"""
>>i = 0
>>while Until(lambda: i<3):
... print "hello"
... i += 1
hello
hello
hello
>>while Until(lambda: i<2): #note i still equals 3 here
... print "hello"
hello
"""
yet = True
def __init__(self, mybool):
if self.__class__.yet or mybool():
self.__class__.yet = False
self.ans = True
else:
self.__class__.yet = True
self.ans = False

def __nonzero__(self):
return self.ans
First of all, I have to say it reads horribly. "while Until(...)" is
supposed to remind us of a *repeat* loop?

Secondly it isn't really what you want because the condition is still
being evaluated before the loop body is executed, when the idea of a
repeat loop is to terminate the loop after at least one iteration once
the condition is true. This is not strictly equivalent to your
conditions. Temporal logic is tricky.

By the way, isn't it always true that self.ans == not self.__class__.yet
(and why use a class variable, by the way, doesn't this stop you from
using nested loops)?

That's about all I can think of offhand. Sorry if it seems a bit
negative. I'm hoping it will spur you on to a better solution.

regards
Steve
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 24 '07 #2
On Apr 24, 11:08 am, Thomas Nelson <t...@mail.utexas.eduwrote:
Occasionally someone posts to this group complaining about the lack of
"repeat ... until" in python. I too have occasionally wished for such
a construct, and after some thinking, I came up with the class below.
I'm hoping to get some feedback here, and if people besides me think
they might use it someday, I can put it on the python cookbook. I'm
pretty happy with it, the only ugly thing is you have to use a
lambda. Ideally i'd like to just see
while Until(i<3)
but that doesn't work.
Please tell me what you think, and thanks for your time.

Tom

class Until:
"""
>>i = 0
>>while Until(lambda: i<3):
... print "hello"
... i += 1
hello
hello
hello
>>while Until(lambda: i<2): #note i still equals 3 here
... print "hello"
hello
"""
yet = True
def __init__(self, mybool):
if self.__class__.yet or mybool():
self.__class__.yet = False
self.ans = True
else:
self.__class__.yet = True
self.ans = False

def __nonzero__(self):
return self.ans
Class-level "yet" variable not a good idea. Loops can be nested:

i = 0
# this should loop once, since test is logically at the end of the
loop
while Until(lambda : i < 0 ):
print "hello"
i += 1
j = 0
while Until(lambda : j < 0 ):
print "byebye"
j += 1

This loops forever.

After tinkering with this a bit, I can see why you need a class
variable. In your outer loop, "Until(lambda : i < 0)" is evaluated
every time around the loop, creating a new Until object, so you can't
preserve looping state in the object, you have to do it in the class.

I used your idea to create an instance-level Until that survives
looping, but the requirements for construction are still onerous. In
essence, the instance-level util defers the break-on-condition by one
loop iteration.

class Until:
"""
>>i = 0
while Until(lambda: i<3):
... print "hello"
... i += 1
hello
hello
hello
>>while Until(lambda: i<2):
... print "hello"
hello
"""
def __init__(self, mybool):
self.lastTest = True
self.mybool = mybool

def __nonzero__(self):
ret,self.lastTest = self.lastTest,self.mybool()
return ret

i = 0
uCond1 = Until(lambda : i < 0 )
uCond2 = Until(lambda : j < 0 )
while uCond1:
print "hello"
i += 1
j = 0
while uCond2:
print "byebye"
j += 1

-- Paul

Apr 26 '07 #3
I moved the state into the Until instance vs. the Until class, so that
it now supports nesting. But the calling syntax is even uglier - but
necessary to avoid creating a new Until instance each time around.
That is "while Until(blah):" evaluates and constructs a new Until
instance each time around the loop, so that is why the state had to be
kept in a class-level variable in your original design (which is why
nesting doesn't work).

class Until:
def __init__(self, mybool):
self.lastTest = True
self.mybool = mybool

def __nonzero__(self):
ret,self.lastTest = self.lastTest,self.mybool()
return ret

i = 0
u1 = Until(lambda : i < 0 )
while u1:
print "hello"
i += 1
j = 0
u2 = Until(lambda : j < 0 )
while u2:
print "byebye"
j += 1

Apr 26 '07 #4
On Wed, 2007-04-25 at 19:11 -0700, Paul McGuire wrote:
I moved the state into the Until instance vs. the Until class, so that
it now supports nesting. But the calling syntax is even uglier - but
necessary to avoid creating a new Until instance each time around.
That is "while Until(blah):" evaluates and constructs a new Until
instance each time around the loop, so that is why the state had to be
kept in a class-level variable in your original design (which is why
nesting doesn't work).

class Until:
def __init__(self, mybool):
self.lastTest = True
self.mybool = mybool

def __nonzero__(self):
ret,self.lastTest = self.lastTest,self.mybool()
return ret

i = 0
u1 = Until(lambda : i < 0 )
while u1:
print "hello"
i += 1
j = 0
u2 = Until(lambda : j < 0 )
while u2:
print "byebye"
j += 1
Using iterators is a way around the problem of making a new instance
every time around the loop, but the calling syntax is not much nicer
since it is shoehorned into a for-loop. Note that I've renamed the beast
in order to reflect what it actually does: The loop body is repeated as
long as the given condition is true.

class RepeatAsLongAs(object):
def __init__(self, cond):
self.cond = cond
self.previousTest = True
def __iter__(self):
return self
def next(self):
ret, self.previousTest = self.previousTest, self.cond()
if not ret: raise StopIteration
return ret

i = 0
for _ in RepeatAsLongAs(lambda: i<0):
print "hello"
i += 1
j = 0
for _ in RepeatAsLongAs(lambda: j<0):
print "byebye"
j += 1

Having said that, I don't see myself ever using such a beast. The
equivalent code

while True:
# do something
if until_condition: break

is much more concise, much easier to understand, and it shows explicitly
that the condition is checked after the loop body is executed.

-Carsten
Apr 26 '07 #5
On 2007-04-24, Thomas Nelson <th*@mail.utexas.eduwrote:
Occasionally someone posts to this group complaining about the lack of
"repeat ... until" in python. I too have occasionally wished for such
a construct, and after some thinking, I came up with the class below.
I'm hoping to get some feedback here, and if people besides me think
they might use it someday, I can put it on the python cookbook. I'm
pretty happy with it, the only ugly thing is you have to use a
lambda. Ideally i'd like to just see
while Until(i<3)
but that doesn't work.
Please tell me what you think, and thanks for your time.
Maybe we should try to get the developers revive PEP 315.

This PEP whould introduce a do-while statement that looks
like the folowing:

do
statements
while condition
statements
this would be equivallent to:

while 1:
statements
if not condition:
break
statements
Those wanting something like:

repeat
statements
until condition
Could then write something like:

do
statements
while not condition
pass
Still not the most elegant but a vast improvement IMO.

--
Antoon Pardon
Apr 26 '07 #6
On Apr 26, 11:58 am, Antoon Pardon <apar...@forel.vub.ac.bewrote:
On 2007-04-24, Thomas Nelson <t...@mail.utexas.eduwrote:
Occasionally someone posts to this group complaining about the lack of
"repeat ... until" in python. I too have occasionally wished for such
a construct, and after some thinking, I came up with the class below.
I'm hoping to get some feedback here, and if people besides me think
they might use it someday, I can put it on the python cookbook. I'm
pretty happy with it, the only ugly thing is you have to use a
lambda. Ideally i'd like to just see
while Until(i<3)
but that doesn't work.
Please tell me what you think, and thanks for your time.

Maybe we should try to get the developers revive PEP 315.

This PEP whould introduce a do-while statement that looks
like the folowing:

do
statements
while condition
statements

this would be equivallent to:

while 1:
statements
if not condition:
break
statements

Those wanting something like:

repeat
statements
until condition

Could then write something like:

do
statements
while not condition
pass

Still not the most elegant but a vast improvement IMO.
At http://mail.python.org/pipermail/pyt...ry/060718.html
Raymond Hettinger suggested removing the final colon after the 'while'
if there's no statement after it, which I agree with, although I would
prefer 'repeat' instead of 'do' (IMHO that doesn't suggest repetition
clearly enough):

repeat:
statements
while condition:
statements

and:

repeat:
statements
while condition

Apr 26 '07 #7
On 2007-04-26, MRAB <go****@mrabarnett.plus.comwrote:
At http://mail.python.org/pipermail/pyt...ry/060718.html
Raymond Hettinger suggested removing the final colon after the 'while'
if there's no statement after it, which I agree with, although I would
prefer 'repeat' instead of 'do' (IMHO that doesn't suggest repetition
clearly enough):

repeat:
statements
while condition:
statements

and:

repeat:
statements
while condition
I wouldn't object to this. But more important than how it will look like
IMO is that it gets implemented. Acoording to your URL there is little
chance that this will be implemented before Py3k. A pity.

--
Antoon Pardon
Apr 27 '07 #8

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

Similar topics

4
by: Dan Perl | last post by:
pyfmf is a project I started a few months ago and that is registered with sourceforge (http://pyfmf.sourceforge.net, http://sourceforge.net/projects/pyfmf). It consists of a framework for file...
8
by: Michele Simionato | last post by:
I was playing with string.Template in Python 2.4 and I came out with the following recipe: import sys from string import Template def merge(*dictionaries): """Merge from right (i.e. the...
36
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at...
1
by: Nicholas Zhou | last post by:
We'd like your help. The ChineseFoodDIY web site has just been redesigned. We have just updated it to include some great new tips and it now has an entirely new look. I think that I'm "too...
1
by: Dgates | last post by:
I'm learning ASP.NET, C# and VB.NET, and hoping to get some feedback from more experienced programmers on a few issues regarding efficient, readable, well-organized code. I'm trying to program...
8
by: Richard Hollenbeck | last post by:
I have a recipe database that I've been building but I haven't yet put any of the ingredients in because of a little problem of normalization. If I build a table of ingredients, all the recipes...
6
by: placid | last post by:
Hi all, Im using the cmd module and i have command that loops and keeps on printing text, what i want to be able to do is loop until the user presses a particular key, say Q/q ? I tried the...
17
by: Ron Adam | last post by:
I put together the following module today and would like some feedback on any obvious problems. Or even opinions of weather or not it is a good approach. While collating is not a difficult thing...
0
by: Thomas Nelson | last post by:
Occasionally people post complaining about the lack of a "repeat...until" structure in python. I thought about it and came up with this recipe that I like. The only ugly thing is having to use...
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
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
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,...
1
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
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.