Generate labels for a multi-level outline

castironpi at gmail.com castironpi at gmail.com
Tue May 6 23:46:28 EDT 2008


On May 6, 4:43 pm, pyt... at bdurham.com wrote:
> I need to generate multi-level incrementing labels for an
> outline/hierarchy where the string for each level of a label is based on
> an incrementing sequence like 1, 2, 3 or A, B, C, or even I, II, III.
> For simplicity, assume that each level's label segment is separated by a
> period (".").
>
> I will pass an integer level (1 ... N) to this function/object so that
> level specific counters get reset when the level changes.
>
> I will use this function/object to generate output like:
>
> Label     Level
> I.        1
> I.A.      2
> I.B.      2
> I.C.      2
> I.D.      2
> I.D.1.    3
> I.D.2.    3
> I.D.3.    3
> I.E.      2
> II.       1
>
> Is there a pre-built class for generating label sequences like this?
> (I'm not sure what I would use as search terms to google for such a
> class).
>
> While this sounds like a simple/fun class to write, I think it could
> quickly get complicated when one factors in parsing rules for each
> level's label type, separators, and error checking. If there's an
> existing, road tested class (with unit tests) that does this, I would
> rather avoid re-inventing/re-testing the wheel.
>
> Thanks,
> Malcolm

You've inquired about syntax here.

(simulated)

>>> a.next( )
I.
>>> a.throw( in )
I.A.
>>> a.next( )
I.B.
>>> a.next( )
I.C.
>>> a.throw( out )
II.

I will implement the object (the term is  "generator"), but you have
to count on two things: I can, and I'll check the newsgroup (i.e. bank
on the spare time of others).  You're invited to reply with any and
all interest.  Personally, the offer expires, but I'm not the only one
with the skill.  (Stay tuned.)

(The trick to generators is "dis"inventing wheels.)

Is the formulation proposed satisfactory to you?

Would you prefer to yield a sequence of marks, as a further option, as
follows?  (As opposed to the rejoined string.)

(simulated)

>>> a.next( )
( "I", )
>>> a.throw( in )
( "I", "A" )
>>> a.next( )
( "I", "B" )
>>> a.next( )
( "I", "C" )
>>> a.throw( out )
( "II", )

At this point in conception, my proposal has -not- met your criterion
of:

> I will pass an integer level (1 ... N) to this function/object so that
> level specific counters get reset when the level changes.

but does proceed stepwise through my sequence.

(Further I am interested in other means of display of the information
you're presenting in the outline; feel free to brainstorm on this
group.)

Library: Python allows you to join sequences of strings simply:

>>> ".".join( ( "I", "A", "1", "iii" ) )
'I.A.1.iii'

But customization could make the return a multi-liner.  Yes it's free,
what you've asked.

The implementation is subject to taste in one of two ways, whether
you're holding a mutable sequence of generators (per se), or just one,
but for beggars can't be choosers, the free code only comes with one.
If you will want to "go back and insert", generators are the -wrong-
solution (prop check).



More information about the Python-list mailing list