Discussion:
Is there an easy way to control indents in Python
ryguy7272
2014-10-15 02:13:41 UTC
Permalink
I'm just learning Python. It seems like indents are EXTREMELY important. I guess, since there are no brackets, everything is controlled by indents. Well, I'm reading a couple books on Python now, and in almost all of the examples they don't have proper indents, so when I copy/paste the code (from the PDF to the IDE) the indents are totally screwed up. I'm thinking that there should be some control, or setting, for this. I hope. :)

I have PyCharm 3.4 and Python 3.4.
Juan Christian
2014-10-15 02:23:03 UTC
Permalink
Using PyCharm is easy:

File > Settings > (IDE Settings) Editor > Smart Keys > Reformat on paste >
choose "Reformat Block"
Post by ryguy7272
I'm just learning Python. It seems like indents are EXTREMELY important.
I guess, since there are no brackets, everything is controlled by indents.
Well, I'm reading a couple books on Python now, and in almost all of the
examples they don't have proper indents, so when I copy/paste the code
(from the PDF to the IDE) the indents are totally screwed up. I'm thinking
that there should be some control, or setting, for this. I hope. :)
I have PyCharm 3.4 and Python 3.4.
--
https://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141014/3f0678f4/attachment.html>
Chris Angelico
2014-10-15 02:18:46 UTC
Permalink
Post by ryguy7272
I'm just learning Python. It seems like indents are EXTREMELY important. I guess, since there are no brackets, everything is controlled by indents. Well, I'm reading a couple books on Python now, and in almost all of the examples they don't have proper indents, so when I copy/paste the code (from the PDF to the IDE) the indents are totally screwed up. I'm thinking that there should be some control, or setting, for this. I hope. :)
That probably depends on the person who made the PDF. You may simply
have to copy and paste one line at a time; if you're using an editor
that understands Python syntax, it'll do some of your indenting for
you, and you'll just have to manually mark the unindents.
Alternatively, just paste it all in without indentation, then go
through and select blocks of code and hit Tab; in many editors,
that'll indent the selected code by one level. But ultimately, the
fault is almost certainly with the PDF.

ChrisA
Zachary Ware
2014-10-15 02:35:12 UTC
Permalink
Post by Chris Angelico
Post by ryguy7272
I'm just learning Python. It seems like indents are EXTREMELY important. I guess, since there are no brackets, everything is controlled by indents. Well, I'm reading a couple books on Python now, and in almost all of the examples they don't have proper indents, so when I copy/paste the code (from the PDF to the IDE) the indents are totally screwed up. I'm thinking that there should be some control, or setting, for this. I hope. :)
That probably depends on the person who made the PDF. You may simply
have to copy and paste one line at a time; if you're using an editor
that understands Python syntax, it'll do some of your indenting for
you, and you'll just have to manually mark the unindents.
Alternatively, just paste it all in without indentation, then go
through and select blocks of code and hit Tab; in many editors,
that'll indent the selected code by one level. But ultimately, the
fault is almost certainly with the PDF.
Agreed, although I'd say the PDF viewer could also be at fault.
Earlier today I tried to copy just a paragraph of mostly plain text
from the Firefox built-in PDF viewer, but when pasting it elsewhere,
it was pasted one character per line. Opening it in the Windows 8.1
default PDF viewer (of all things...), copying and pasting worked like
a charm.
--
Zach
Chris Angelico
2014-10-15 02:47:19 UTC
Permalink
On Wed, Oct 15, 2014 at 1:35 PM, Zachary Ware
Post by Zachary Ware
Post by Chris Angelico
But ultimately, the
fault is almost certainly with the PDF.
Agreed, although I'd say the PDF viewer could also be at fault.
Good point, there are some really terrible PDF viewers around. Either
way, the workaround of grabbing one line at a time will be effective -
albeit tedious for anything more than a dozen lines or so.

ChrisA
Dan Stromberg
2014-10-15 02:38:15 UTC
Permalink
Post by ryguy7272
I'm just learning Python. It seems like indents are EXTREMELY important. I guess, since there are no brackets, everything is controlled by indents. Well, I'm reading a couple books on Python now, and in almost all of the examples they don't have proper indents, so when I copy/paste the code (from the PDF to the IDE) the indents are totally screwed up. I'm thinking that there should be some control, or setting, for this. I hope. :)
Perhaps if you share a screenshot of your PDF and the name of your PDF
viewer, we can help you more.

Here's a URL about Python and Whitespace:
http://stromberg.dnsalias.org/~strombrg/significant-whitespace.html
alex23
2014-10-15 07:27:37 UTC
Permalink
Post by Juan Christian
File > Settings > (IDE Settings) Editor > Smart Keys > Reformat on paste
Post by Juan Christian
choose "Reformat Block"
This isn't as straight forward as you imply. Say I have misindented code
like this:

if True:
print 'true'
else:
print 'false'
print 'done'

If I select this block in PyCharm and reformat it, I get:

if True:
print 'true'
else:
print 'false'
print 'done'

Which is still invalid. Even if it did work more fully, though, how
would it determine the correct placement of the last line of code?
Chris “Kwpolska” Warrick
2014-10-15 14:32:28 UTC
Permalink
Post by alex23
Post by Juan Christian
File > Settings > (IDE Settings) Editor > Smart Keys > Reformat on paste
Post by Juan Christian
choose "Reformat Block"
This isn't as straight forward as you imply. Say I have misindented code
print 'true'
print 'false'
print 'done'
print 'true'
print 'false'
print 'done'
Which is still invalid. Even if it did work more fully, though, how would it
determine the correct placement of the last line of code?
--
https://mail.python.org/mailman/listinfo/python-list
It should parse this as

else:
print 'false'
print 'done'

Why? Because things like `print 'done'` usually have an empty line before it:

if True:
print 'true'
else:
print 'false'

print 'done'

That should be parsed the way you want it done. Makes perfect sense
when you look at it.
--
Chris ?Kwpolska? Warrick <http://chriswarrick.com/>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
Terry Reedy
2014-10-15 17:12:51 UTC
Permalink
Post by Chris “Kwpolska” Warrick
Post by alex23
Post by Juan Christian
File > Settings > (IDE Settings) Editor > Smart Keys > Reformat on paste
Post by Juan Christian
choose "Reformat Block"
This isn't as straight forward as you imply. Say I have misindented code
print 'true'
print 'false'
print 'done'
print 'true'
print 'false'
print 'done'
Which is still invalid. Even if it did work more fully, though, how would it
determine the correct placement of the last line of code?
--
https://mail.python.org/mailman/listinfo/python-list
It should parse this as
print 'false'
print 'done'
There is no such rule in Python so it hardly dependable for auto indenting.
Post by Chris “Kwpolska” Warrick
print 'true'
print 'false'
print 'done'
That should be parsed the way you want it done. Makes perfect sense
when you look at it.
--
Terry Jan Reedy
Ian Kelly
2014-10-15 19:30:17 UTC
Permalink
Post by Terry Reedy
Post by Chris “Kwpolska” Warrick
It should parse this as
print 'false'
print 'done'
There is no such rule in Python so it hardly dependable for auto indenting.
I agree. I very rarely use blank lines inside functions. As I see it,
if you feel you need a blank line for separation within a function,
that's an indication your function is overly complex and should be
broken up.

Keeping blank lines out of functions also makes it easy to copy/paste
those functions into the interactive interpreter, which can be handy
e.g. when sharing snippets of code by email.
Simon Kennedy
2014-10-16 14:08:51 UTC
Permalink
Post by Ian Kelly
I agree. I very rarely use blank lines inside functions. As I see it,
if you feel you need a blank line for separation within a function,
that's an indication your function is overly complex and should be
broken up.
Whereas I feel that if I wanted to write code which looked like that I'd have learnt/learned Perl ;-)

Each to their own.
Chris Angelico
2014-10-16 15:02:18 UTC
Permalink
Post by Simon Kennedy
Post by Ian Kelly
I agree. I very rarely use blank lines inside functions. As I see it,
if you feel you need a blank line for separation within a function,
that's an indication your function is overly complex and should be
broken up.
Whereas I feel that if I wanted to write code which looked like that I'd have learnt/learned Perl ;-)
I did learn Perl. That's why I now code in Python.

ChrisA
alex23
2014-10-15 23:51:16 UTC
Permalink
Post by Chris “Kwpolska” Warrick
It should parse this as
print 'false'
print 'done'
print 'true'
print 'false'
print 'done'
That should be parsed the way you want it done. Makes perfect sense
when you look at it.
I don't think it makes any sense at all, for two reasons:

1) Empty lines have no such semantic meaning in Python.
2) Anything that strips tabs is just as likely to strip EOLs.
Gregory Ewing
2014-10-17 05:22:29 UTC
Permalink
Not in my code, they don't. I never put blank lines
inside functions.
--
Greg
Loading...