Simplify Code

Michiel Overtoom motoom at xs4all.nl
Tue Jul 15 16:37:31 CEST 2008


Victor wrote...

># Headers are kept in order to determine nesting of chapters
># They are labeled according to font size

I'm not sure what you're trying to achieve (I can't divine it from your
example code), but I suspect that a dictionary of sizes and header texts is
somewhat more manegable than a slew of separate variables h12, h14, h16,
h22, h36 etc...

Maybe this gives an idea:


# this could come from an input file or a parser
source=(
    (18,"Hello"),
    (36,"Greetings"),
    # (11,"Boeh") , # uncomment to test this illegal size
    (36,"You too"),
    (12,"Bye"),
    )

# for each font size, remember the last seen header text
headers={12:"", 14:"", 18:"", 22:"", 26:"", 36:""}

# process the source, collect the header texts
for (size,text) in source:
  if not size in headers:
    raise KeyError("%d is an illegal size. It must be one of %s" %
(size,sorted(headers)))
  headers[size]=text

# do something with the collected header texts
for h in sorted(headers):
  print h,headers[h]
  

Greetings,

-- 
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html




More information about the Python-list mailing list