Generate labels for a multi-level outline

Discussion in 'Python' started by python@bdurham.com, May 6, 2008.

  1. Guest

    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
     
    , May 6, 2008
    #1
    1. Advertising

  2. Guest

    On May 6, 4:43 pm, 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).
     
    , May 7, 2008
    #2
    1. Advertising

  3. Guest

    '''
    On May 6, 4:43 pm, honoring:

    >>> a.next( )

    ( "I", )
    >>> a.throw( up )

    ( "I", "A" )
    >>> a.next( )

    ( "I", "B" )
    >>> a.next( )

    ( "I", "C" )
    >>> a.throw( down )

    ( "II", )
    '''

    #funny declaration
    class up( Exception ): pass
    class down( Exception ): pass

    def outline( ):
    stack= [ 1 ]
    while 1:
    try:
    yield stack
    stack[ -1 ]+= 1
    except up:
    stack.append( 1 )
    except down:
    stack.pop( -1 )
    stack[ -1 ]+= 1

    a= outline( )
    print a.next( )
    print a.throw( up )
    print a.next( )
    print a.next( )
    print a.throw( down )
    print a.throw( up )
    print a.throw( up )
    print a.next( )
    print a.next( )
    print a.throw( down )

    ##output:

    [1]
    [1, 1]
    [1, 2]
    [1, 3]
    [2]
    [2, 1]
    [2, 1, 1]
    [2, 1, 2]
    [2, 1, 3]
    [2, 2]

    ##

    cf.

    formatter.NullFormatter.format_counter
    formatter.NullFormatter.format_letter
    formatter.NullFormatter.format_roman
     
    , May 7, 2008
    #3
  4. Guest

    On May 7, 9:07 am, wrote:
    > '''
    > On May 6, 4:43 pm, honoring:
    >
    > >>> a.next( )

    > ( "I", )
    > >>> a.throw( up )

    > ( "I", "A" )
    > >>> a.next( )

    > ( "I", "B" )
    > >>> a.next( )

    > ( "I", "C" )
    > >>> a.throw( down )

    >
    > ( "II", )
    > '''
    >
    > #funny declaration
    > class up( Exception ): pass
    > class down( Exception ): pass
    >
    > def outline( ):
    >     stack= [ 1 ]
    >     while 1:
    >         try:
    >             yield stack
    >             stack[ -1 ]+= 1
    >         except up:
    >             stack.append( 1 )
    >         except down:
    >             stack.pop( -1 )
    >             stack[ -1 ]+= 1
    >
    > a= outline( )
    > print a.next( )
    > print a.throw( up )
    > print a.next( )
    > print a.next( )
    > print a.throw( down )
    > print a.throw( up )
    > print a.throw( up )
    > print a.next( )
    > print a.next( )
    > print a.throw( down )
    >
    > ##output:
    >
    > [1]
    > [1, 1]
    > [1, 2]
    > [1, 3]
    > [2]
    > [2, 1]
    > [2, 1, 1]
    > [2, 1, 2]
    > [2, 1, 3]
    > [2, 2]
    >
    > ##
    >
    > cf.
    >
    > formatter.NullFormatter.format_counter
    > formatter.NullFormatter.format_letter
    > formatter.NullFormatter.format_roman


    One execution of Python 3a4 included in built-ins.

    Python 3.0
    win32
    Type "help
    >>> next

    <built-in
    >>>


    Do you want send and throw in it too?
     
    , May 7, 2008
    #4
  5. Guest

    Re: Generate labels for a multi-level outline (THANK-YOU!)

    Castironpi and Dennis,

    WOW! Thank you very much for your examples!!!

    I wasn't trolling for free development, just whether such a library
    existed and/or design ideas (basic object or generator).

    I had started to develop my own solution which was much more complicated
    (re: ugly) than your 2 very elegant approaches.

    Its interesting to compare your two very different approaches. I will to
    spend some more time studying your techniques before I choose a final
    approach.

    Thank you both again. I'm learning a lot of Python by studying your
    examples! :)

    Malcolm
     
    , May 7, 2008
    #5
  6. Guest

    Castironpi,

    > Do you want send and throw in it too?


    If its not too much trouble, I would love to see how you add these.

    > formatter.NullFormatter.format_counter
    > formatter.NullFormatter.format_letter
    > formatter.NullFormatter.format_roman


    I'm not sure what you mean by the above lines ... <googling> ... are you
    referencing formatter.py?
    http://www.koders.com/python/fid4B7C6E1C20384FC7521414F46DF9DAA33DF2CA11.aspx

    Thanks for your help on this - I'm learning a lot!

    Malcolm

    PS: "throw( up )" ... very funny! :)
     
    , May 7, 2008
    #6
  7. Guest

    Re: Generate labels for a multi-level outline (THANK-YOU!)

    Dennis,

    > I was a touch bored in the last hour at work today so...


    Thank you!!

    Malcolm
     
    , May 8, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    5
    Views:
    1,357
    Christophe
    Feb 16, 2006
  2. msnews.microsoft.com

    Document Outline in VS.NET! WOW!

    msnews.microsoft.com, Jul 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,753
    msnews.microsoft.com
    Jul 26, 2004
  3. King of Red Lions

    Text outline

    King of Red Lions, Apr 8, 2004, in forum: HTML
    Replies:
    9
    Views:
    20,585
    mikewandling
    Oct 25, 2008
  4. Kate
    Replies:
    5
    Views:
    5,050
  5. pabbu
    Replies:
    8
    Views:
    772
    Marc Boyer
    Nov 7, 2005
Loading...

Share This Page