473,513 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generate labels for a multi-level outline

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
Jun 27 '08 #1
6 1433
On May 6, 4:43*pm, pyt...@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).
Jun 27 '08 #2
'''
On May 6, 4:43 pm, pyt...@bdurham.com 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
Jun 27 '08 #3
On May 7, 9:07*am, castiro...@gmail.com wrote:
'''
On May 6, 4:43 pm, pyt...@bdurham.com 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?
Jun 27 '08 #4
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
Jun 27 '08 #5
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/fid4B7C...33DF2CA11.aspx

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

Malcolm

PS: "throw( up )" ... very funny! :)
Jun 27 '08 #6
Dennis,
I was a touch bored in the last hour at work today so...
Thank you!!

Malcolm
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1132
by: frizzle | last post by:
Hi there, I have a multi-language site. Texts are drwan from a mySQL db. At the contact form, there are a few obligate fields. Because the Javascript that processes the form is an external js...
0
1245
by: momina_dar | last post by:
AlachiSoft “TierDeveloper” eradicates the problem of writing thousands of lines of code for your middle tier. Many software solutions on the market can do it for you but according to our research...
3
4817
by: louise | last post by:
hi i am trying to set up a mail merge button which takes records from a multi-select listbox (the contents of which are decided by a query created by a search from) and not from a specific...
29
3709
by: Lauren Wilson | last post by:
Does anyone know how the following info is extracted from the user's computer by a Front Page form? HTTP User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107...
1
7475
by: Kyle Blaney | last post by:
When labels are vertically stacked on top of one another and have their TextAlign property set to MiddleRight, the text is not properly right-aligned. You can reproduce this problem by creating...
1
1948
by: popsovy | last post by:
I am new to the discussion groups and to the .NET world, so this is probably a very basic question.. Is there any quick way in .NET to auto-generate data forms from typed datasets or SQL queries?...
2
1430
by: Steven C | last post by:
Hello: I'm trying to dynamically generate some labels for a form, and I can't seem to get it to work. The code is as follows. The first label is generated, but subsequent labels are not. Is...
3
1819
by: Alex | last post by:
Hi, We're working on what will become a multi-language application, and I wanted to see if this idea is feasable. What we're thinking of doing is creating a table to contain all text of the...
2
1910
by: Sriku | last post by:
Hi Friends, i have page with few options. i need to generate multiple rows in page dynamically. in single row i have three labels and three select boxes, i need to get the number of row to be...
2
1830
by: | last post by:
Hi, I have 2 scenarios: First, an aspx page that contains 300 controls with the labels of course in a table. Second, an empty aspx page that contains on loading a script that creates the 300...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.