Showcasing some new stuff in Peach

Listening to Iggy Pop and thinking I should write about some of the new stuff in the 0.6 release….

First up is GroupForeachDo(groupA, groupB). As the name would suggest we will perform groupB on each iteration of groupA. Lets see that in action:

groupA = Group()
groupB = Group()
group = GroupForeachDo(groupA, groupB)
generator = Block([
	List(groupA, ['My dog does ', 'My cat does ']),
	List(groupB, ['jumping', 'running', 'barking'])
	])

So can we guess yet what the output will look like?

My dog does jumping
My dog does running
My dog does barking
My cat does jumping
My cat does running
My cat does barking

Thank you drive through

Now lets talk about GeneratorList and GeneratorList2, these aren’t actually new (they showed up in the 0.5 back in April) but they now some debugging features added and were never really talked about.

A GeneratorList is about what it sounds like, a list of generators that will be used in sequence one at a time. In GeneratorList the iteration is controlled by the GeneratorList calling “.next()” on each sub-generator until getting a GeneratorException and moving to the next. While this is nice for a simple list of generators, you may want to have more control over the iteration that occurs to do something complex. That’s were GeneratorList2 comes in. GeneratorList2 takes a list of groups and a list of generators. As each generator is used in sequence, the corresponding group will be used as it’s iterator.

Lets look at some examples to see how that works.

Example #1, basic GeneratorList usage:

gen = GeneratorList(None, [ Static('A'), Static('B'), Static('C') ])

Output:

A
B
C

Example #2, simple GeneratorList usage:

gen = GeneratorList(None, [
	List(None, ['1', '2', '3']),
	Repeater(None, Static('A'), 1, 1, 3),
	Block2([ Static(":"), Repeater(None, Static('B'), 1, 1, 3)
	])

Output:

1
2
3
A
AA
AAA
:B
:BB
:BBB

Easy enough yah? Okay, lets skip on to GeneratorList2…

Example #1, basic usage:

groupA = Group()
groupBA = Group()
groupBB = Group()
groupB = GroupForeachDo(groupAA, groupBB)

gen = GeneratorList2(None, [groupA, groupB], [
	Repeater(groupA, Static('A'), 1, 1, 3),
	Block([
		List(groupBA, [':', '', '/']),
		Repeater(groupBB, Static('B'), 1, 1, 3)
		])
	])

Output:

A
AA
AAA
:B
:BB
:BBB
B
BB
BBB
/B
/BB
/BBB

See how easy that is? Now, hopefully you are starting to see how confusing it can get to debug a complex generation setup if you had, say, several GeneratorLists inside of another GeneratorList and an exception got thrown. To this end we can name our GeneratorLists by adding another parameter onto the end. This name will get displayed at times to help with debugging.

Looks like we are on a roll, lets take a peek at Block vs. Block2. Easy stuff here, Block ignores iteration while Block2 will pass it along to all the generators it contains.

Hmmm… whats next? Did you know GroupSequence acts like an array exposing it’s sub groups? No? Well you do now:

groupSeq = GroupSequence([ Group(), Group(), Group() ])
 ....
Generator(groupSeq[0], ...)
Generator(groupSeq[1], ...)

I think you get the idea :)

Okay, and that’s all we have time for today. As time allows you should see more blog entries on crazy stuff you can do with Peach.

~ by meddington on January 13, 2006.

Leave a comment