Can I do this?

Alex Martelli aleaxit at yahoo.com
Wed May 23 06:29:48 EDT 2001


"David Haynes" <david.haynes2 at sympatico.ca> wrote in message
news:hlvmgtoat93gb3957vrvgsqki6jd46lqmr at 4ax.com...
> I want to implement a light-weight finite state automata as a set of
> rules. Each rule consists of a rule number, regular expression,
> function if true, function if false.
>
> Can I somehow call the function named in the rule or do I have to use
> the value in the function area to do a lookup for the function?
>
> That is, should my rules look like:
>
> 10,a < b, do_a(a, b), do_b(b, a)

Assuming a 'rule' is a string, this would be harder to
parse as commas are found in two different roles -- not
impossible of course, but make your life simpler by
using different character separators:-).

If this is meant to be a tuple display, then as written
it would call the functions when evaluated, just as it
would evaluate then the "a<b" sub-expression (I don't
see a regular expression anywhere here, by the way).

You may want a slightly different format, e.g.:

10, "a<b", (do_a, 'a', 'b'), (do_b, 'b', 'a')


> or should they look like:
>
> 10,a<b,11,12
>
> and have a function that converts the '11' to a call to do_a()?

You do need "a function that converts" one way or another,
but you may usefully place a reference to the function
object directly in your rule and save the converting
function some work.  Or if your rule is a string, it can
contain the function-name, and the converting function
will look it up (e.g. in globals()) to get the function
object -- this may still keep your rules more readable
than using arbitrary numbers there, I guess.


Alex






More information about the Python-list mailing list