473,786 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(li ne)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whit espace*OLD_INDE NT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(wh itespace*NEW_IN DENT*x+line.lst rip())
n += 1
break
else:
output.write(li ne)
break
else:
output.write(li ne)

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

Aug 4 '06 #1
13 1930
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(li ne)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whit espace*OLD_INDE NT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(wh itespace*NEW_IN DENT*x+line.lst rip())
n += 1
break
else:
output.write(li ne)
break
else:
output.write(li ne)

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(white space*old_inden t, 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(li ne)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whit espace*OLD_INDE NT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(wh itespace*NEW_IN DENT*x+line.lst rip())
n += 1
break
else:
output.write(li ne)
break
else:
output.write(li ne)

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(l ine, 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(l ine, 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.c om 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\Too ls\Scripts\rein dent.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(li ne)
break
else: # Why doesn't the blank line
get picked up here?
x = line.count(whit espace*OLD_INDE NT,0,i)
# Reindent lines that have exactly a multiple of
OLD_INDENT.
if x 0 and (i-1)%OLD_INDENT == 0:
output.write(wh itespace*NEW_IN DENT*x+line.lst rip())
n += 1
break
else:
output.write(li ne)
break
else:
output.write(li ne)

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("".lstri p)
help("".rstri p)
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(wh itespace)" 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("".lstri p)
>>help("".rstri p)
>>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.joi n(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(wh itespace)" 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("".lstri p)
>>help("".rstri p)
>>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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1737
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 Exception you won't find
26
14158
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 appropriate document, or post an example? I don't relish the idea especially long if-else statements. Joe
27
3079
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 few weeks ago while trying to find the best way to form a if-elif-else block, that on a very general level, an 'also' statement might be useful. So I was wondering what others would think of it.
40
3046
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 goodness of Python whenever I talk with fellow developers, but I always hit a snag when it comes to discussing the finer points of the execution model (specifically, exceptions). Without fail, when I start talking with some of the "old-timers"...
13
7244
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: var1 = raw_input, test var1, then run through the loop again. What results is var1 gets and keeps the first value it receives. If this is in the FAQ, my apologies, I did not find it. Thank you in advance.
1
2042
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
12721
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 != indexList1.pop(): index1+=1 index2+=1 continue
8
20278
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 to be able to enter char strings or int strings on the command line. Then I use an if-elif structure to establish which is which. For example -
11
1538
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) etc. Probably it's not so difficult, but I don't see how to distinguish for example between a string and an array. An array has a shape, a string not etc.
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.