Re: Python code written in 1998, how to improve/change it?

2006-01-28 Thread Petr Jakes
To provide some feedback I vould like to show the code which works fine for me as a FSM machine. As speed is not the crucial issue for my application, I have decided to use an approach showed in the Skip Montanaro's code. http://orca.mojam.com/~skip/python/fsm.py (it is using dictionary to store

Re: Python code written in 1998, how to improve/change it?

2006-01-26 Thread Bengt Richter
On Wed, 25 Jan 2006 15:50:27 -0600, [EMAIL PROTECTED] wrote: If they need to resume their calculations from where they left off after the last yield. Wolfgang Well, no, independently from that. Wolfgang Just to avoid to inital overhead of the function call. How do you pass

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread Bengt Richter
On Tue, 24 Jan 2006 14:58:27 -0600, [EMAIL PROTECTED] wrote: Wolfgang So basically if I want to write a long-running program in Wolfgang Python, it would make sense to code all functions that are Wolfgang likely to be called more than once as generators... If they need to resume

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread Wolfgang Keller
So basically if I want to write a long-running program in Python, it would make sense to code all functions that are likely to be called more than once as generators... This was meant as a question. If they need to resume their calculations from where they left off after the last yield.

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread Fredrik Lundh
Wolfgang Keller wrote: So basically if I want to write a long-running program in Python, it would make sense to code all functions that are likely to be called more than once as generators... This was meant as a question. If they need to resume their calculations from where they

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread Wolfgang Keller
what makes you think that resuming a generator won't involve function calls ? That was not what I wrote. I referred to what Peter Hansen [EMAIL PROTECTED] wrote in [EMAIL PROTECTED]: I believe the more modern approach to this is to use generators in some way, yield each other as the next

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread Magnus Lycka
Wolfgang Keller wrote: The way I understand this, resuming a generator causes less overhead than the inital overhead of a function call. I don't have Python 2.4 handy, but it doesn't seem to be true in 2.3. I'm not very proficient with generators though, so maybe I'm doing something stupid

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread Peter Hansen
Wolfgang Keller wrote: what makes you think that resuming a generator won't involve function calls ? That was not what I wrote. I referred to what Peter Hansen [EMAIL PROTECTED] wrote in [EMAIL PROTECTED]: I believe the more modern approach to this is to use generators in some way,

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread skip
Wolfgang So basically if I want to write a long-running program in Wolfgang Python, it would make sense to code all functions that are Wolfgang likely to be called more than once as generators... Skip If they need to resume their calculations from where they left off Skip

Re: Python code written in 1998, how to improve/change it?

2006-01-25 Thread skip
If they need to resume their calculations from where they left off after the last yield. Wolfgang Well, no, independently from that. Wolfgang Just to avoid to inital overhead of the function call. How do you pass in parameters? Consider: def square(x): return

Re: Python code written in 1998, how to improve/change it?

2006-01-24 Thread Wolfgang Keller
On Fri, 20 Jan 2006 05:16:57 +0100, Peter Hansen wrote (in article [EMAIL PROTECTED]): I believe the more modern approach to this is to use generators in some way, yield each other as the next state. This way you avoid all almost all the function call overhead (the part that takes

Weird generator id() behaviour (was Re: Python code written in 1998, how to improve/change it?)

2006-01-24 Thread Carl Cerecke
Wolfgang Keller wrote: On Fri, 20 Jan 2006 05:16:57 +0100, Peter Hansen wrote (in article [EMAIL PROTECTED]): I believe the more modern approach to this is to use generators in some way, yield each other as the next state. This way you avoid all almost all the function call overhead (the

Re: Python code written in 1998, how to improve/change it?

2006-01-24 Thread skip
Wolfgang So basically if I want to write a long-running program in Wolfgang Python, it would make sense to code all functions that are Wolfgang likely to be called more than once as generators... If they need to resume their calculations from where they left off after the last yield.

Re: Weird generator id() behaviour (was Re: Python code written in 1998, how to improve/change it?)

2006-01-24 Thread Wolfgang Keller
On Tue, 24 Jan 2006 21:19:26 +0100, Carl Cerecke wrote (in article [EMAIL PROTECTED]): def g_on(): print on action = next_action() if action == 'lift': yield g_on() elif action == 'push': yield g_off() else: yield None def g_off():

Re: Weird generator id() behaviour (was Re: Python code written in 1998, how to improve/change it?)

2006-01-24 Thread Fredrik Lundh
Carl Cerecke wrote: It turns out that generators are more efficient than the eval function excuting bits of compiled code. About 20-25% faster. why are you using generators to return things from a function, when you can just return the things ? def f_on(): print on action

Re: Python code written in 1998, how to improve/change it?

2006-01-23 Thread Bengt Richter
On Mon, 23 Jan 2006 08:53:59 +1300, Carl Cerecke [EMAIL PROTECTED] wrote: Bengt Richter wrote: On Thu, 19 Jan 2006 23:16:57 -0500, Peter Hansen [EMAIL PROTECTED] wrote: How about something like actions = dict( ...a=compile('print A; state=b','','exec'), ...b=compile('print B;

Re: Python code written in 1998, how to improve/change it?

2006-01-22 Thread Carl Cerecke
Bengt Richter wrote: On Thu, 19 Jan 2006 23:16:57 -0500, Peter Hansen [EMAIL PROTECTED] wrote: How about something like actions = dict( ...a=compile('print A; state=b','','exec'), ...b=compile('print B; state=c','','exec'), ...c=compile('print C; state=None','','exec')

Re: Python code written in 1998, how to improve/change it?

2006-01-22 Thread Petr Jakes
Sorry, I can't get in. Can you please show me, how to use your approach on the simple push/push ON/OFF button for example please? PS: seriously it is not a homework :) and I feel it like a shame I am asking such a simple questions :( States: ON, OFF Transition event: push, lift transition

Re: Python code written in 1998, how to improve/change it?

2006-01-22 Thread Petr Jakes
Sorry for the typo in my previous posting. Of course it has to be: simple liftt/push ON/OFF button -- http://mail.python.org/mailman/listinfo/python-list

Re: Python code written in 1998, how to improve/change it?

2006-01-22 Thread Carl Cerecke
Petr Jakes wrote: Sorry, I can't get in. Can you please show me, how to use your approach on the simple push/push ON/OFF button for example please? PS: seriously it is not a homework :) and I feel it like a shame I am asking such a simple questions :( States: ON, OFF Transition event:

Re: Python code written in 1998, how to improve/change it?

2006-01-20 Thread Bengt Richter
On Thu, 19 Jan 2006 23:16:57 -0500, Peter Hansen [EMAIL PROTECTED] wrote: Carl Cerecke wrote: Carl Cerecke wrote: Ah. Well, my post suggested, as one option, the callables call each other directly. Doh! No I didn't. And they shouldn't. Otherwise the call stack gets out of hand. But I did

Re: Python code written in 1998, how to improve/change it?

2006-01-20 Thread Steven D'Aprano
On Fri, 20 Jan 2006 10:27:58 +1300, Carl Cerecke wrote: We want a goto. Unfortunately, this is about the only problem I can think of where gotos are useful. And for reasons well explained elsewhere, gotos are Not Good. I've solved this in a very efficient, but rather unpythonic way.

Python code written in 1998, how to improve/change it?

2006-01-19 Thread Petr Jakes
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for my purposes. As this code was posted long time ago (November 1998) I would like to ask if the

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Steve Holden
Petr Jakes wrote: Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for my purposes. As this code was posted long time ago (November 1998) I would

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Carl Cerecke
Petr Jakes wrote: Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for my purposes. As this code was posted long time ago (November 1998) I would

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Dan Sommers
On Fri, 20 Jan 2006 10:27:58 +1300, Carl Cerecke [EMAIL PROTECTED] wrote: Petr Jakes wrote: [ a query regarding some 1998 python code that implements a finite state machine ] Python has no goto. Thank goodness! func = f_state_1 # start_state while func != None: func() We've

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Dave Hansen
On Fri, 20 Jan 2006 10:27:58 +1300 in comp.lang.python, Carl Cerecke [EMAIL PROTECTED] wrote: [...] Python has no goto. +1 [...] We want a goto. -1 Regards, -=Dave -- Change is inevitable, progress is not. --

Goto in python - NO! (was Re: Python code written in 1998, how to improve/change it?)

2006-01-19 Thread Carl Cerecke
Dave Hansen wrote: On Fri, 20 Jan 2006 10:27:58 +1300 in comp.lang.python, Carl Cerecke [EMAIL PROTECTED] wrote: [...] Python has no goto. +1 [...] We want a goto. -1 I agree entirely. My (rather unclearly made) point was that, for the particular application (FSM), an

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Carl Cerecke
Dan Sommers wrote: On Fri, 20 Jan 2006 10:27:58 +1300, Carl Cerecke [EMAIL PROTECTED] wrote: ... now you have a function-call overhead on each state transition ... Have you profiled your code and demonstrated that this particular function call consumes too much time? Yes. For a parser

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Petr Jakes
Thanks for your comments, The mentioned 8 years old code actually works somehow. I am trying to solve very similar problem about FSM as the code in the example does and I do not want to be overburden by the if/elif stuff or by declaring state functions (which IMHO is very similar approach as

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Chris Mellon
On 19 Jan 2006 15:53:54 -0800, Petr Jakes [EMAIL PROTECTED] wrote: Thanks for your comments, The mentioned 8 years old code actually works somehow. I am trying to solve very similar problem about FSM as the code in the example does and I do not want to be overburden by the if/elif stuff or

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Carl Cerecke
Chris Mellon wrote: I'm not sure why nobody else in this thread said it, but the most common way of implementing state machines I've seen in Python (unless theres only a couple states you can manage with if/elif) is to use a dict to map states to callables. Ah. Well, my post suggested, as

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Carl Cerecke
Carl Cerecke wrote: Chris Mellon wrote: I'm not sure why nobody else in this thread said it, but the most common way of implementing state machines I've seen in Python (unless theres only a couple states you can manage with if/elif) is to use a dict to map states to callables. Ah.

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread Peter Hansen
Carl Cerecke wrote: Carl Cerecke wrote: Ah. Well, my post suggested, as one option, the callables call each other directly. Doh! No I didn't. And they shouldn't. Otherwise the call stack gets out of hand. But I did suggest that each callable representing a state set a global variable, just

Re: Python code written in 1998, how to improve/change it?

2006-01-19 Thread skip
Petr Hello, I am trying to study/understand OOP principles using Petr Python. I have found following code http://tinyurl.com/a4zkn about Petr FSM (finite state machine) on this list, which looks quite useful Petr for my purposes. As this code was posted long time ago (November