Re: define loop statement?

2006-02-22 Thread Magnus Lycka
David Isaac wrote:

 PS Here's the motivation.  Python closely resembles pseudocode.  With
 a very little LaTeX hacking, it is often possible to write algorithms is
 Python that typeset as reasonable pseudocode.  A simple repetitive
 loop is a bit of a sticking point.

With slightly more LaTeX hacking or possibly Python hacking on your
script before passing it to LaTeX, you might just be able to change 
occurences of

for x in range(a, b):

to

for x in [a, b):

or whatever, and get away with plain vanilla Python
in your code...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-19 Thread David Isaac

Benji York [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Here's a flagrant hack:

Admiration wins out over revulsion.  ;-)
Thanks,
Alan Isaac

PS Here's the motivation.  Python closely resembles pseudocode.  With
a very little LaTeX hacking, it is often possible to write algorithms is
Python that typeset as reasonable pseudocode.  A simple repetitive
loop is a bit of a sticking point.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-19 Thread Cameron Laird
In article [EMAIL PROTECTED],
David Isaac [EMAIL PROTECTED] wrote:
.
.
.
Admiration wins out over revulsion.  ;-)
Thanks,
Alan Isaac

PS Here's the motivation.  Python closely resembles pseudocode.  With
a very little LaTeX hacking, it is often possible to write algorithms is
Python that typeset as reasonable pseudocode.  A simple repetitive
loop is a bit of a sticking point.



Wow.  Innovative.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-18 Thread Jeffrey Schwab
David Isaac wrote:
 I would like to be able to define a loop statement
 (nevermind why) so that I can write something like
 
 loop 10:
 do_something
 
 instead of
 
 for i in range(10):
 do_something
 
 Possible?  If so, how?

Ruby and Smalltalk are both good at this kind of thing, since they have 
syntactic support for associating a block with each method call.  In 
Python, I think you just have to do a little more setup.  How about 
something like this?

class Loop:
def __init__(self, n):
self.n = n
def __call__(self):
self.n = self.n - 1
return self.n != 0


if __name__ == '__main__':
loop = Loop(10)
while loop:
print OK
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-18 Thread Georg Brandl
Jeffrey Schwab wrote:

 class Loop:
   def __init__(self, n):
   self.n = n
   def __call__(self):
   self.n = self.n - 1
   return self.n != 0
 
 
 if __name__ == '__main__':
   loop = Loop(10)
   while loop:
   print OK

Seems you forgot () after while loop above.

Georg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-18 Thread Jeffrey Schwab
Jeffrey Schwab wrote:

 class Loop:
 def __init__(self, n):
 self.n = n
 def __call__(self):
 self.n = self.n - 1
 return self.n != 0
 
 
 if __name__ == '__main__':
 loop = Loop(10)
 while loop:

Whoops.  Should be while loop().

 print OK
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-18 Thread David Isaac

 Alan Isaac wrote:
  I would like to be able to define a loop statement
  (nevermind why) so that I can write something like
 
  loop 10:
  do_something
 
  instead of
 
  for i in range(10):
  do_something
 
  Possible?  If so, how?


Jeffrey Schwab [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 class Loop:
  def __init__(self, n):
   self.n = n
  def __call__(self):
   self.n = self.n - 1
   return self.n != 0


 if __name__ == '__main__':
 loop = Loop(10)
 while loop():
  print OK

OK, that's pretty good.
Thanks!
Alan Isaac


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-18 Thread Felipe Almeida Lessa
Em Sáb, 2006-02-18 às 20:04 +, Jeffrey Schwab escreveu:
 if __name__ == '__main__':
   loop = Loop(10)
   while loop:
   print OK

Maybe:

while Loop(10)():
print OK

Looks rather ugly but requires one less line ;-).

-- 
Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas.

  -- Sun Tzu, em A arte da guerra

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: define loop statement?

2006-02-18 Thread Nigel Rowe
Felipe Almeida Lessa wrote:

 Em Sáb, 2006-02-18 às 20:04 +, Jeffrey Schwab escreveu:
 if __name__ == '__main__':
 loop = Loop(10)
 while loop:
 print OK
 
 Maybe:
 
 while Loop(10)():
 print OK
 
 Looks rather ugly but requires one less line ;-).
 
Doesn't work.  You get a NEW Loop(10) instance on each pass through the
'while'.  This is just an expensive way to make an endless loop.

-- 
Nigel Rowe
A pox upon the spammers that make me write my address like..
rho (snail) swiftdsl (stop) com (stop) au
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-18 Thread Felipe Almeida Lessa
Em Dom, 2006-02-19 às 11:08 +1100, Nigel Rowe escreveu:
 Felipe Almeida Lessa wrote:
 
  Em Sáb, 2006-02-18 às 20:04 +, Jeffrey Schwab escreveu:
  if __name__ == '__main__':
  loop = Loop(10)
  while loop:
  print OK
  
  Maybe:
  
  while Loop(10)():
  print OK
  
  Looks rather ugly but requires one less line ;-).
  
 Doesn't work.  You get a NEW Loop(10) instance on each pass through the
 'while'.  This is just an expensive way to make an endless loop.

Oh, sorry, ignore me on that one. Now I think I should sleep =S...

-- 
Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas.

  -- Sun Tzu, em A arte da guerra

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: define loop statement?

2006-02-18 Thread Benji York
David Isaac wrote:
 I would like to be able to define a loop statement
 (nevermind why) so that I can write something like
 
 loop 10:
 do_something

Here's a flagrant hack:

import sys

VAR_NAME = '__repeat_counter'

def set_repeat_counter(value):
 frame = sys._getframe(2)
 frame.f_locals[VAR_NAME] = value

def get_repeat_counter(value):
 frame = sys._getframe(2)
 if VAR_NAME not in frame.f_locals:
 frame.f_locals[VAR_NAME] = value

 return frame.f_locals[VAR_NAME]

def repeat(limit):
 set_repeat_counter(get_repeat_counter(limit)-1)
 return get_repeat_counter(limit)

while repeat(10):
 print 'OK'

Without more work it doesn't allow nested loops though.

And for the record, if you're worrying about Python's counted loop 
construct you need better things to worry about. 
insert-smilies-as-appropriate
--
Benji York
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-17 Thread Xavier Morel
Rene Pijlman wrote:
 David Isaac:
 I would like to be able to define a loop statement
 (nevermind why) so that I can write something like

 loop 10:
do_something

 instead of

 for i in range(10):
do_something

 Possible?  If so, how?
 
 Yes. By implementing a compiler or an interpreter for your programming
 language. Or a preprocessor that converts your language to Python, or some
 other suitable intermediate language. Or a programmer, that converts your
 pseudocode and some coffee to the desired algorithm :-)
 
Or by hacking through the Python source and creating his own somehow 
pythonish but absolutely not python language
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-17 Thread Georg Brandl
David Isaac wrote:
 I would like to be able to define a loop statement
 (nevermind why) so that I can write something like
 
 loop 10:
 do_something
 
 instead of
 
 for i in range(10):
 do_something
 
 Possible?  If so, how?

It's not possible to create a new statement, with suite
and indentation rules without hacking the interpreter or
resorting to alternative bytecode compilers such as pyc.

Creating a _function_ named loop is easy as Jonathan's
answer shows.

Georg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-17 Thread Rene Pijlman
David Isaac:
I would like to be able to define a loop statement
(nevermind why) so that I can write something like

loop 10:
do_something

instead of

for i in range(10):
do_something

Possible?  If so, how?

Yes. By implementing a compiler or an interpreter for your programming
language. Or a preprocessor that converts your language to Python, or some
other suitable intermediate language. Or a programmer, that converts your
pseudocode and some coffee to the desired algorithm :-)

-- 
René Pijlman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-17 Thread bearophileHUGS
David Isaac:
 I would like to be able to define a loop statement
 (nevermind why) so that I can write something like

 loop 10:
 do_something

 instead of

 for i in range(10):
 do_something

 Possible?  If so, how?

It seems that you are looking for macros; maybe Logix language
(www.livelogix.net/logix) or Boo (boo.codehaus.org) can solve your
problem.
Otherwise you can do it with very different languages like Lisp or
Scheme (with a different syntax) and maybe Dylan too.

If you tell us your purpose, maybe we can suggest you a
better/different solution.

Bye,
bearophile

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define loop statement?

2006-02-17 Thread Xavier Morel
Rene Pijlman wrote:
 David Isaac:
 I would like to be able to define a loop statement
 (nevermind why) so that I can write something like

 loop 10:
do_something

 instead of

 for i in range(10):
do_something

 Possible?  If so, how?
 
 Yes. By implementing a compiler or an interpreter for your programming
 language. Or a preprocessor that converts your language to Python, or some
 other suitable intermediate language. Or a programmer, that converts your
 pseudocode and some coffee to the desired algorithm :-)
 
Or by hacking through the Python source and creating his own somehow 
pythonish but absolutely not python language
-- 
http://mail.python.org/mailman/listinfo/python-list