Simplify Code

Victor Subervi victorsubervi at gmail.com
Tue Jul 15 14:37:47 CEST 2008


Hi;
Forgive multiple posts in one day: online very infrequently
I set the following variables:
# Headers are kept in order to determine nesting of chapters
# They are labeled according to font size
h36 = ''
h26 = ''
h22 = ''
h18 = ''
h14 = ''
h12 = ''
header_sizes = [36, 26, 22, 18, 14, 12]
# Size is the font size of the header
size = 0

I write code that grabs the size var.
Then I have the following very laborious spaghetti code:

if size == 36:
  h36 = line
  h26 = ''
  h22 = ''
  h18 = ''
  h14 = ''
  h12 = ''
elif size == 26:
  h26 = line
  h22 = ''
  h18 = ''
  h14 = ''
  h12 = ''
elif size == 22:
  h22 = line
  h18 = ''
  h14 = ''
  h12 = ''
elif size == 18:
  h18 = line
  h14 = ''
  h12 = ''
elif size == 14:
  h14 = line
  h12 = ''
elif size == 12:
  h12 = line
else:
  # THROW ERROR!

I would like something more elegant, like this:
# del is used to determine if should reset the lower header values to ''
del = 0
for header_size in header_sizes:
  if header_size == size:
    # The following line is ILLEGAL
    h + string(header_size) = line
    del = 1
  if del = 1
    # The following line is ILLEGAL
    h + string(header_size) = ''

How can I rewrite those illegal lines, such that I can create the proper
name for the variable, and write the necessary data?
TIA,
Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080715/a95ac9e8/attachment.html>


More information about the Python-list mailing list