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) 13 1871
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)
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)
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) 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
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
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
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
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
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
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
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.
"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)
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)
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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:...
|
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
|
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 !=...
|
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...
|
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)...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |