Passing indented code to compile()
Lulu of the Lotus-Eaters
mertz at gnosis.cx
Wed May 7 14:08:38 EDT 2003
|Lulu of the Lotus-Eaters wrote:
|> def LeftMargin(txt):
|> """Remove as many leading spaces as possible from whole block"""
|> for l in range(12,-1,-1):
|> re_lead = '(?sm)'+' '*l+'\S'
|> if re.match(re_lead, txt): break
|> txt = re.sub('(?sm)^'+' '*l, '', txt)
|> return txt
Alex Martelli <aleax at aleax.it> wrote previously:
|def leftMargin(lines):
| def numblanks(line):
| return len(line)-len(line.lstrip(' '))
| minblanks = min([numblanks(line) for line in lines])
| return [line[minblanks:] for line in lines]
I can declare that I officially like Alex' version better than mine.
The 12 space max wasn't really an issue for me, since in my text files I
would simply not indent more than that. But in the OP's XML context,
that is not safe.
Moreover, my version also treats the first line in a special way. I
vaguely recall there was once a reason I had to do that for my spec...
but I cannot think of why right now (I wrote the thing before there were
listcomps, so that's a hint to its antiquity). It may have involved a
case like that Alex shows where I actually -wanted- the special behavior
(but I probably dropped it from my markup conventions).
Yours, Lulu...
--
---[ to our friends at TLAs (spread the word) ]--------------------------
Echelon North Korea Nazi cracking spy smuggle Columbia fissionable Stego
White Water strategic Clinton Delta Force militia TEMPEST Libya Mossad
---[ Postmodern Enterprises <mertz at gnosis.cx> ]--------------------------
More information about the Python-list
mailing list