Michael Sparks wrote:
> Since this has come up on IRC briefly, I'll just post one possible piece
> of syntactic sugar I'm considering. Specifically I'm thinking of a
> decorator for converting generators into generator components.
>
> For example, something like:
> @GeneratorComponent(Inboxes = ["inbox", "control"],
> Outboxes = ["outbox", "signal"])
> def Tracer(self):
> while 1:
> for msg in self.Inbox("inbox"):
> print self.tag, str(msg)
> self.send(msg, "outbox")
> self.pause()
> yield 1
>
> To be similar to:
>
> class TaggedTracer(Axon.Component.component):
> tag = "> "
> def main(self):
> while not self.dataReady("control"):
> for msg in self.Inbox("inbox"):
> print self.tag, str(msg)
> self.send(msg, "outbox")
> self.pause()
> yield 1
Here are some ideas that try to look more like idiomatic python,
incrementally moving closer to simple python code. It is end-result python
pseudo-code which may more may not be realistic given Kamaelia's internals.
# This one makes the inboxes/outboxes explicit object representations that
# behave like standard python objects.
# Pause in the wrapper? (not sure of semantics of pause)
# Could possibly eliminate inboxes/outboxes arguments to the decorator and get
# them through introspection on function arguments.
@GeneratorComponent(
inboxes=('inbox', 'control'),
outboxes('outbox', 'signal'))
def Tracer((inbox, control), (outbox, signal)):
while not control:
for msg in inbox:
print "> ", str(msg)
outbox.append(msg)
yield True
# Moves the signal handling into the wrapper.
# Skipping explicit inboxes/outboxes.
# These would seem to make it specific to pipelines.
@GeneratorPipelineComponent()
def Tracer(inbox, outbox):
while True:
for msg in inbox:
print "> ", str(msg)
outbox.append(msg)
yield True
# Becomes a subsumed generator by one in the wrapper that handles control and
# moving yielded values to the outbox. Looks like a standard simple generator.
@GeneratorPipelineComponent()
def Tracer(inbox):
for msg in inbox:
print "> ", str(msg)
yield msg
--
John Eikenberry
[[email protected] - http://zhar.net]
[PGP public key @ http://zhar.net/jae_at_zhar_net.gpg]
______________________________________________________________
"Perfection is attained, not when no more can be added, but when no more
can be removed." -- Antoine de Saint-Exupery
signature.asc
Description: Digital signature
