Kamaelia docs : Kamaelia.Util.Chooser

Iterating Over A Predefined List

The Chooser component iterates (steps) forwards and backwards through a list of items. Request the next or previous item and Chooser will return it.

The ForwardIteratingChooser component only steps forwards, but can therefore handle more than just lists - for example: infinite sequences.

Example Usage

A simple slideshow:

items=[ "image1.png", "image2.png", "image3.png", ... ]

Graphline( CHOOSER  = Chooser(items=imagefiles),
           FORWARD  = Button(position=(300,16), msg="NEXT", caption="Next"),
           BACKWARD = Button(position=(16,16),  msg="PREV", caption="Previous"),
           DISPLAY  = Image(position=(16,64), size=(640,480)),
           linkages = { ("FORWARD" ,"outbox") : ("CHOOSER","inbox"),
                        ("BACKWARD","outbox") : ("CHOOSER","inbox"),
                        ("CHOOSER" ,"outbox") : ("DISPLAY","inbox"),
                      }
         ).run()

The chooser is driven by the 'next' and 'previous' Button components. Chooser then sends filenames to an Image component to display them.

Another example: a forever looping carousel of files, read at 1MBit/s:

def filenames():
    while 1:
        yield "file 1"
        yield "file 2"
        yield "file 3"

JoinChooserToCarousel( chooser = InfiniteChooser(items=filenames),
                       carousel = FixedRateControlledReusableFilereader("byte",rate=131072,chunksize=1024),
                     )

How does it work?

When creating it, pass the component a set of items for it to iterate over.

Chooser will only accept finite length datasets. InfiniteChooser will accept any interable sequence, even one that never ends from a generator.

Once activated, the component will emit the first item from the list from its "outbox" outbox.

If the list/sequence is empty, then nothing is emitted, even in response to messages sent to the "inbox" inbox described now.

Send commands to the "inbox" inbox to move onto another item of data and cause it to be emitted. This behaviour is very much like a database cursor or file pointer - you are issuing commands to step through a dataset.

Send "SAME" and the component will emit the same item of data that was last emitted last time. Both Chooser and InfiniteChooser respond to this request.

Send "NEXT" and the component will emit the next item from the list or sequence. If there is no 'next' item (becuase we are already at the end of the list/sequence) then nothing is emitted. Both Chooser and InfiniteChooser respond to this request.

With InfiniteChooser, if there is not 'next' item then, additionally, a producerFinished message will be sent out of its "signal" outbox to signal that the end of the sequence has been reached. The component will then terminate.

All requests described from now are only supported by the Chooser component. InfiniteChooser will ignore them.

Send "PREV" and the previous item from the list or sequence will be emitted. If there is no previous item (because we are already at the front of the list/sequence) then nothing is emitted.

Send "FIRST" or "LAST" and the first or last item from the list or sequence will be emitted, respectively. The item will be emitted even if we are already at the first/last item.

If Chooser or InfiniteChooser receive a shutdownMicroprocess message on the "control" inbox, they will pass it on out of the "signal" outbox. The component will then terminate.


Kamaelia.Util.Chooser.Chooser

class Chooser(Axon.Component.component)

Chooser([items]) -> new Chooser component.

Iterates through a finite list of items. Step by sending "NEXT", "PREV", "FIRST" or "LAST" messages to its "inbox" inbox.

Keyword arguments:

  • items -- list of items to be chosen from, must be type 'list' (default=[])

Inboxes

  • control : shutdown messages
  • inbox : receive commands

Outboxes

  • outbox : emits chosen items
  • signal : shutdown messages

Methods defined here

Warning!

You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers.

__init__(self[, items])

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

getCurrentChoice(self)

Return the current choice to the outbox

gotoFirst(self)

Goto the first item in the set. Returns True.

gotoLast(self)

Goto the last item in the set. Returns True.

gotoNext(self)

Advance the choice forwards one.

Returns True if successful or False if unable to (eg. already at end).

gotoPrev(self)

Backstep the choice backwards one.

Returns True if successful or False if unable to (eg. already at start).

main(self)

Main loop.

shutdown(self)

Returns True if a shutdownMicroprocess message was received.

Kamaelia.Util.Chooser.ForwardIteratingChooser

class ForwardIteratingChooser(Axon.Component.component)

Chooser([items]) -> new Chooser component.

Iterates through an iterable set of items. Step by sending "NEXT" messages to its "inbox" inbox.

Keyword arguments: - items -- iterable source of items to be chosen from (default=[])

Inboxes

  • control : shutdown messages
  • inbox : receive commands

Outboxes

  • outbox : emits chosen items
  • signal : shutdown messages

Methods defined here

Warning!

You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers.

__init__(self[, items])

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

getCurrentChoice(self)

Return the current choice

gotoNext(self)

Advance the choice forwards one.

Returns True if successful or False if unable to (eg. already at end).

main(self)

Main loop.

shutdown(self)

Returns True if a shutdownMicroprocess message was received.

Feedback

Got a problem with the documentation? Something unclear that could be clearer? Want to help improve it? Constructive criticism is very welcome - especially if you can suggest a better rewording!

Please leave you feedback here in reply to the documentation thread in the Kamaelia blog.

-- Automatic documentation generator, 05 Jun 2009 at 03:01:38 UTC/GMT


Kamaelia is an open source project originated from and guided by BBC Research. For more information browse the site or get in contact.

This is an ongoing community based development site. As a result the contents of this page is the opinions of the contributors of the pages involved not the organisations involved. Specificially, this page may contain personal views which are not the views of the BBC. (the site is powered by a wiki engine)

(C) Copyright 2008 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved.

This web site is powered by the same code created for the bicker manor project. For more details, contact Michael Sparks at BBC Research directly (cf contact)