471,319 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Why do I require an "elif" statement here?

Jim
Could somebody tell me why I need the "elif char == '\n'" in the
following code?
This is required in order the pick up lines with just spaces in them.
Why doesn't
the "else:" statement pick this up?

OLD_INDENT = 5 # spaces
NEW_INDENT = 4 # spaces

print 'Reindent.py:'
print '\nFrom file %s' % infile
print 'Change %i space indentation to %i space indentation.' % (
OLD_INDENT, NEW_INDENT)
print 'And place revised file into %s' % outfile

whitespace = ' '
n = 0
nline = 0

for line in input.readlines():
nline += 1
# Only look at lines that start with a space.
if line[0] == whitespace:
i = 0
for char in line:
i += 1
if char == whitespace:
pass
elif char == '\n': # Why do I need this for a
blank line with only spaces?
output.write(line)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whitespace*OLD_INDENT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(whitespace*NEW_INDENT*x+line.lstrip() )
n += 1
break
else:
output.write(line)
break
else:
output.write(line)

input.close()
output.close()
print 'Total number of %i lines reindented out of %i lines.' % (n,
nline)

Aug 4 '06 #1
13 1794
Could somebody tell me why I need the "elif char == '\n'" in
the following code?

This is required in order the pick up lines with just spaces
in them.
Why doesn't the "else:" statement pick this up?
Following through with the below code:

if the line consists of only a newline, it gets ignored due to
the "if line[0] == whitespace" line. However, if the line
consists of only whitespace followed by a newline you *do*
successfully get to the "else" in question. There's no other
place for you to go.

However, what happens then? If you fall into you the top half of
your "if x 0 ..." statement:

you strip **all** *true* whitespace from the line with your
lstrip() call. Since there's nothing between your "whitespace"
(simple spaces) and the \n, the \n gets swallowed by the lstrip()
call. Thus, you output.write() an empty string.

I recommend a few judiciously placed "print repr(thing)" lines as
you try to debug to see where things aren't what you expect them
to be.

As another sidelight, rather than using the "i=0, i+= 1" aspect,
you can use the more pythonic idiom of

for i, char in enumerate(line):

(taking into consideration that i becomes zero-based). This will
automatically update "i" on each pass.

-tkc
>
OLD_INDENT = 5 # spaces
NEW_INDENT = 4 # spaces

print 'Reindent.py:'
print '\nFrom file %s' % infile
print 'Change %i space indentation to %i space indentation.' % (
OLD_INDENT, NEW_INDENT)
print 'And place revised file into %s' % outfile

whitespace = ' '
n = 0
nline = 0

for line in input.readlines():
nline += 1
# Only look at lines that start with a space.
if line[0] == whitespace:
i = 0
for char in line:
i += 1
if char == whitespace:
pass
elif char == '\n': # Why do I need this for a
blank line with only spaces?
output.write(line)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whitespace*OLD_INDENT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(whitespace*NEW_INDENT*x+line.lstrip() )
n += 1
break
else:
output.write(line)
break
else:
output.write(line)

input.close()
output.close()
print 'Total number of %i lines reindented out of %i lines.' % (n,
nline)
Aug 4 '06 #2
Jim, what you wrote should work correctly. I'm curious as to why you
are doing it this way though. An easier way would be to take out all
this character processing and use the builtin string processing. See
this code:

-------------------------------
whitespace = " "
old_indent = 3
new_indent = 5

x = " starts with 3 spaces"

x = x.replace(whitespace*old_indent, whitespace*new_indent)
-------------------------------

In this example though, it will replace the 3 spaces no matter where
they are at, not just in the beginning... still, it's probably more
practical for most use cases.
Jim wrote:
Could somebody tell me why I need the "elif char == '\n'" in the
following code?
This is required in order the pick up lines with just spaces in them.
Why doesn't
the "else:" statement pick this up?

OLD_INDENT = 5 # spaces
NEW_INDENT = 4 # spaces

print 'Reindent.py:'
print '\nFrom file %s' % infile
print 'Change %i space indentation to %i space indentation.' % (
OLD_INDENT, NEW_INDENT)
print 'And place revised file into %s' % outfile

whitespace = ' '
n = 0
nline = 0

for line in input.readlines():
nline += 1
# Only look at lines that start with a space.
if line[0] == whitespace:
i = 0
for char in line:
i += 1
if char == whitespace:
pass
elif char == '\n': # Why do I need this for a
blank line with only spaces?
output.write(line)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whitespace*OLD_INDENT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(whitespace*NEW_INDENT*x+line.lstrip() )
n += 1
break
else:
output.write(line)
break
else:
output.write(line)

input.close()
output.close()
print 'Total number of %i lines reindented out of %i lines.' % (n,
nline)
Aug 4 '06 #3
Jim wrote:
Could somebody tell me why I need the "elif char == '\n'" in the
following code?
This is required in order the pick up lines with just spaces in them.
Why doesn't
the "else:" statement pick this up?
No idea. Look at the profile of your program: for.. if.. for.. if..
else.. if.. This is NOT good. The reason why you are having trouble
getting it to work is that you are not writing it in a way that is easy
to debug and test. If one block of code ends up being indented halfway
across the screen it means you are doing something wrong.

This program should be split up into a handful of small functions that
each do one thing. The following is slightly longer, but immensely
simpler. Most importantly, it can be imported from the python shell
and each function can be tested individually.

def leading_spaces(line):
"""Return the number of leading spaces"""
num = 0
for char in line:
if char != ' ':
break
num += 1
return num

def change_indent(line, old, new):
"""Change the indent of this line using a ratio of old:new"""
ws = leading_spaces(line)

#if there was no leading whitespace,
#or it wasn't a multiple of the old indent, do nothing
if ws == 0 or ws % old:
return line

#otherwise change the indent
new_spaces = ws/old*new
new_indent = ' ' * new_spaces
return new_indent + line.lstrip(' ')
def reindent(ifname, ofname, old, new):
f = open(ifname)
o = open(ofname, 'w')

for line in f:
line = change_indent(line, old, new)
o.write(line)

f.close()
o.close()

if __name__ == "__main__":
try :
ifname, ofname, old, new = sys.argv[1:]
old = int(old)
new = int(new)
except ValueError:
print "blah"
sys.exit(1)

reindent(ifname, ofname, old, new)

Aug 4 '06 #4

sp*****@gmail.com wrote:
Jim, what you wrote should work correctly. I'm curious as to why you
are doing it this way though. An easier way would be to take out all
this character processing and use the builtin string processing.
I'm curious as to why Jim is doing it at all.

Extract from C:\Python24\Tools\Scripts\reindent.py:

'''
# Released to the public domain, by Tim Peters, 03 October 2000.

"""reindent [-d][-r][-v] [ path ... ]

-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
-r (--recurse) Recurse. Search for all .py files in subdirectories
too.
-v (--verbose) Verbose. Print informative msgs; else no output.
-h (--help) Help. Print this usage information and exit.

Change Python (.py) files to use 4-space indents and no hard tab
characters.
Also trim excess spaces and tabs from ends of lines, and remove empty
lines
at the end of files. Also ensure the last line ends with a newline.
'''

Cheers,
John

Aug 4 '06 #5
Jim
Tim Chase wrote:
Could somebody tell me why I need the "elif char == '\n'" in
the following code?

This is required in order the pick up lines with just spaces
in them.
Why doesn't the "else:" statement pick this up?

Following through with the below code:

if the line consists of only a newline, it gets ignored due to
the "if line[0] == whitespace" line. However, if the line
consists of only whitespace followed by a newline you *do*
successfully get to the "else" in question. There's no other
place for you to go.

However, what happens then? If you fall into you the top half of
your "if x 0 ..." statement:

you strip **all** *true* whitespace from the line with your
lstrip() call. Since there's nothing between your "whitespace"
(simple spaces) and the \n, the \n gets swallowed by the lstrip()
call. Thus, you output.write() an empty string.

I recommend a few judiciously placed "print repr(thing)" lines as
you try to debug to see where things aren't what you expect them
to be.

As another sidelight, rather than using the "i=0, i+= 1" aspect,
you can use the more pythonic idiom of

for i, char in enumerate(line):

(taking into consideration that i becomes zero-based). This will
automatically update "i" on each pass.

-tkc

OLD_INDENT = 5 # spaces
NEW_INDENT = 4 # spaces

print 'Reindent.py:'
print '\nFrom file %s' % infile
print 'Change %i space indentation to %i space indentation.' % (
OLD_INDENT, NEW_INDENT)
print 'And place revised file into %s' % outfile

whitespace = ' '
n = 0
nline = 0

for line in input.readlines():
nline += 1
# Only look at lines that start with a space.
if line[0] == whitespace:
i = 0
for char in line:
i += 1
if char == whitespace:
pass
elif char == '\n': # Why do I need this for a
blank line with only spaces?
output.write(line)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whitespace*OLD_INDENT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(whitespace*NEW_INDENT*x+line.lstrip() )
n += 1
break
else:
output.write(line)
break
else:
output.write(line)

input.close()
output.close()
print 'Total number of %i lines reindented out of %i lines.' % (n,
nline)
Thank you Tim.
Hard to believe that lstrip() produces an empty string on lines with
just spaces and doesn't remove the '\n' with lines that have
characters.
I'm now using all your suggestions, even "print repr(thing)" which I
wasn't aware of.
Thanks,
Jim

Aug 5 '06 #6
Hard to believe that lstrip() produces an empty string on lines with
just spaces and doesn't remove the '\n' with lines that have
characters.
It's easy to understand that lstrip() is doing exactly what it's
supposed to. It evaluates from the left of your string,
discarding whitespace (spaces, tabs, and cr/lf characters) until
it hits a non-whitespace character or the end of the string.
When there's no non-whitespace, it returns an empty string.

If you wanted to remove the \n from the right of lines, there was
an earlier discussion on the list where someone (Bruno?) and I
went back and forth and I think we finally decided that the
"best" solution was

s.rstrip('\n')

which had the fewest side-effects.

However, when you use the output.write() method, you'd then have
to add the \n back in to make sure it ended up in the output stream.

If you wanted to continue to use lstrip(), you could also just
ensure that you're only stripping spaces (chr(0x20)) by using

s.lstrip(' ')

This would leave \t and \n characters unmolested.

More info can be found at
>>help("".lstrip)
help("".rstrip)
help("".strip)
Hope this helps,

-tkc

Aug 5 '06 #7
Jim
Good stuff!
Since I'm only interested in spaces being my only whitespace it makes
sense for me to use "line.lstrip(whitespace)" in my script, thus
eliminating the "elif char == '\n':" statement.
Thanks,
Jim

Tim Chase wrote:
Hard to believe that lstrip() produces an empty string on lines with
just spaces and doesn't remove the '\n' with lines that have
characters.

It's easy to understand that lstrip() is doing exactly what it's
supposed to. It evaluates from the left of your string,
discarding whitespace (spaces, tabs, and cr/lf characters) until
it hits a non-whitespace character or the end of the string.
When there's no non-whitespace, it returns an empty string.

If you wanted to remove the \n from the right of lines, there was
an earlier discussion on the list where someone (Bruno?) and I
went back and forth and I think we finally decided that the
"best" solution was

s.rstrip('\n')

which had the fewest side-effects.

However, when you use the output.write() method, you'd then have
to add the \n back in to make sure it ended up in the output stream.

If you wanted to continue to use lstrip(), you could also just
ensure that you're only stripping spaces (chr(0x20)) by using

s.lstrip(' ')

This would leave \t and \n characters unmolested.

More info can be found at

>>help("".lstrip)
>>help("".rstrip)
>>help("".strip)

Hope this helps,

-tkc
Aug 6 '06 #8
I'm surprised no one has mentioned neat-er, more pythonic ways of doing
this. I'm also surprised no one mentioned regular expressions. Regular
expressions are really powerful for searching and manipulating text.
Here is where I learned most of the stuff I know about regular
expressions:

http://www.amk.ca/python/howto/regex/

I think this howto does a pretty good job, and doesn't take too long to
read.

Anyway, here's my solution, which does Not use regular expressions:

def reindent(line):
## we use slicing, because we don't know how long line is
head = line[:OLD_INDENT]
tail = line[OLD_INDENT:]
## if line starts with Exactly so many spaces...
if head == whitespace*OLD_INDENT and not tail.startswith(' '):
return whitespace*NEW_INDENT + tail
else: return line # our default

emptyString = ""
lines = input.readlines()
reindented = [reindent(ln) for ln in lines]
output.write( emptyString.join(reindented) )

I'll bet you could put that all on one line using lambda instead of a
def, but this is Python, and we like clarity ;).

A regular expression could have taken the place of our reindent
function, but I'll leave that up to you to explore ;).

Jim wrote:
Good stuff!
Since I'm only interested in spaces being my only whitespace it makes
sense for me to use "line.lstrip(whitespace)" in my script, thus
eliminating the "elif char == '\n':" statement.
Thanks,
Jim

Tim Chase wrote:
Hard to believe that lstrip() produces an empty string on lines with
just spaces and doesn't remove the '\n' with lines that have
characters.
It's easy to understand that lstrip() is doing exactly what it's
supposed to. It evaluates from the left of your string,
discarding whitespace (spaces, tabs, and cr/lf characters) until
it hits a non-whitespace character or the end of the string.
When there's no non-whitespace, it returns an empty string.

If you wanted to remove the \n from the right of lines, there was
an earlier discussion on the list where someone (Bruno?) and I
went back and forth and I think we finally decided that the
"best" solution was

s.rstrip('\n')

which had the fewest side-effects.

However, when you use the output.write() method, you'd then have
to add the \n back in to make sure it ended up in the output stream.

If you wanted to continue to use lstrip(), you could also just
ensure that you're only stripping spaces (chr(0x20)) by using

s.lstrip(' ')

This would leave \t and \n characters unmolested.

More info can be found at

>>help("".lstrip)
>>help("".rstrip)
>>help("".strip)

Hope this helps,

-tkc
Aug 6 '06 #9
danielx wrote:
I'm surprised no one has mentioned neat-er, more pythonic ways of doing
this. I'm also surprised no one mentioned regular expressions. Regular
expressions are really powerful for searching and manipulating text.
[snip]

I'm surprised you don't count my post as a neat and pythonic way of
doing this. I'm also surprised that you mention regular expressions
after neat and pythonic. While regular expressions often serve a
purpose, they are rarely neat.
Anyway, here's my solution, which does Not use regular expressions:

def reindent(line):
## we use slicing, because we don't know how long line is
head = line[:OLD_INDENT]
tail = line[OLD_INDENT:]
## if line starts with Exactly so many spaces...
if head == whitespace*OLD_INDENT and not tail.startswith(' '):
return whitespace*NEW_INDENT + tail
else: return line # our default
[snip]

This function is broken. Not only does it still rely on global
variables to work, it does not actually reindent lines correctly. Your
function only changes lines that start with exactly OLD_INDENT spaces,
ignoring any lines that start with a multiple of OLD_INDENT.

--
- Justin

Aug 6 '06 #10
No offense. I didn't mean there was anything wrong with your way, just
that it wasn't "neat". By that, I meant, you were still using lots of
for loops and if blocks.

Justin Azoff wrote:
danielx wrote:
I'm surprised no one has mentioned neat-er, more pythonic ways of doing
this. I'm also surprised no one mentioned regular expressions. Regular
expressions are really powerful for searching and manipulating text.
I suppose I put those things too close together. I meant I was
surprised for each of the two Separate (non)occurances.
[snip]

I'm surprised you don't count my post as a neat and pythonic way of
doing this. I'm also surprised that you mention regular expressions
after neat and pythonic. While regular expressions often serve a
purpose, they are rarely neat.
Anyway, here's my solution, which does Not use regular expressions:

def reindent(line):
## we use slicing, because we don't know how long line is
head = line[:OLD_INDENT]
tail = line[OLD_INDENT:]
## if line starts with Exactly so many spaces...
if head == whitespace*OLD_INDENT and not tail.startswith(' '):
return whitespace*NEW_INDENT + tail
else: return line # our default
[snip]

This function is broken. Not only does it still rely on global
variables to work, it does not actually reindent lines correctly. Your
It's just an illustration.
function only changes lines that start with exactly OLD_INDENT spaces,
ignoring any lines that start with a multiple of OLD_INDENT.
Maybe I misread, but that's what I thought he wanted. Did you not see
my code comments expressing that very intent? Anyway, it wouldn't be
that hard to modify what I gave to do multiple replacement: just add a
recursive call.
>
--
- Justin
Aug 6 '06 #11
danielx wrote:
No offense. I didn't mean there was anything wrong with your way, just
that it wasn't "neat". By that, I meant, you were still using lots of
for loops and if blocks.

Justin Azoff wrote:
danielx wrote:
I'm surprised no one has mentioned neat-er, more pythonic ways of doing
this. I'm also surprised no one mentioned regular expressions. Regular
expressions are really powerful for searching and manipulating text.

I suppose I put those things too close together. I meant I was
surprised for each of the two Separate (non)occurances.
And what would regexes give you? A Pythonic way of calculating the
number of leading spaces??? N.B. in your (mis)reading of the OP's
intent, you don't need/use the number of leading spaces.

Anyway, here's my solution, which does Not use regular expressions:
>
def reindent(line):
## we use slicing, because we don't know how long line is
head = line[:OLD_INDENT]
tail = line[OLD_INDENT:]
## if line starts with Exactly so many spaces...
if head == whitespace*OLD_INDENT and not tail.startswith(' '):
return whitespace*NEW_INDENT + tail
else: return line # our default
[snip]

This function is broken. Not only does it still rely on global
variables to work, it does not actually reindent lines correctly. Your

It's just an illustration.
An illustration of ... what?
>
function only changes lines that start with exactly OLD_INDENT spaces,
ignoring any lines that start with a multiple of OLD_INDENT.

Maybe I misread, but that's what I thought he wanted.
(a) Such a requirement is rather implausible.
(b) No "maybe":

x = line.count(whitespace*OLD_INDENT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(whitespace*NEW_INDENT*x+line.lstrip() )

Did you not see
my code comments expressing that very intent?
Those comments merely documented the brokenness.
Anyway, it wouldn't be
that hard to modify what I gave to do multiple replacement: just add a
recursive call.
.... which would make it uglier.

Aug 6 '06 #12
"sp*****@gmail.com" <sp*****@gmail.comwrites:
>-------------------------------
whitespace = " "
old_indent = 3
new_indent = 5

x = " starts with 3 spaces"

x = x.replace(whitespace*old_indent, whitespace*new_indent)
-------------------------------

In this example though, it will replace the 3 spaces no matter where
they are at, not just in the beginning... still, it's probably more
practical for most use cases.
You'd corner it with:

if x.startswith(' '*3): x=x.replace(' '*3,' '*5,1)
--
John Savage (my news address is not valid for email)
Aug 7 '06 #13

John Savage wrote:
"sp*****@gmail.com" <sp*****@gmail.comwrites:
-------------------------------
whitespace = " "
old_indent = 3
new_indent = 5

x = " starts with 3 spaces"

x = x.replace(whitespace*old_indent, whitespace*new_indent)
-------------------------------

In this example though, it will replace the 3 spaces no matter where
they are at, not just in the beginning... still, it's probably more
practical for most use cases.

You'd corner it with:

if x.startswith(' '*3): x=x.replace(' '*3,' '*5,1)
As others have stated, this will only get lines that start with only 1
set of "old_indent" and not multiples of "old_indent".
--
John Savage (my news address is not valid for email)
Aug 15 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Raymond Arthur St. Marie II of III | last post: by
26 posts views Thread by Joe Stevenson | last post: by
27 posts views Thread by Ron Adam | last post: by
40 posts views Thread by Steve Juranich | last post: by
13 posts views Thread by darthbob88 | last post: by
1 post views Thread by arnold | last post: by
7 posts views Thread by Girish Sahani | last post: by
8 posts views Thread by aine_canby | last post: by
11 posts views Thread by Stef Mientki | last post: by
reply views Thread by rosydwin | last post: by

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.