473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 1898
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Raymond Arthur St. Marie II of III | last post by:
Del's "except"ional PEP Rejection Ladieees and Gentilmen and Pyth-O-neers of all ages. Step right up. Don't be shy. Come one come all. Be the first on your block TO GROK *THE* one *THE* only...
26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
13
by: darthbob88 | last post by:
Problem: I wish to run an infinite loop and initialize a variable on each iteration. Sort of like, "Enter Data", test it, "No good!", "Next Try?", test it, etc. What I've tried is simply while 1:...
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
8
by: aine_canby | last post by:
>>v = raw_input("Enter: ") Enter: kjjkj Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'kjjkj' In my program I need...
11
by: Stef Mientki | last post by:
hello, Is there some handy/ nice manner to view the properties of some variable ? As a newbie, I often want to see want all the properties of a var, and also some corner values (large arrays)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.