473,320 Members | 1,862 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,320 software developers and data experts.

textwrap.dedent() drops tabs - bug or feature?

So I've recently been making pretty frequent use of textwrap.dedent() to
allow me to use triple-quoted strings at indented levels of code without
getting the extra spaces prefixed to each line. I discovered today that
not only does textwrap.dedent() strip any leading spaces, but it also
substitutes any internal tabs with spaces. For example::

py> def test():
.... x = ('abcd efgh\n'
.... 'ijkl mnop\n')
.... y = textwrap.dedent('''\
.... abcd efgh
.... ijkl mnop
.... ''')
.... return x, y
....
py> test()
('abcd\tefgh\nijkl\tmnop\n', 'abcd efgh\nijkl mnop\n')

Note that even though the tabs are internal, they are still removed by
textwrap.dedent(). The documentation[1] says:

"""
dedent(text)
Remove any whitespace that can be uniformly removed from the left
of every line in text.

This is typically used to make triple-quoted strings line up with
the left edge of screen/whatever, while still presenting it in the
source code in indented form.
"""

So it looks to me like even if this is a "feature" it is undocumented.
I'm planning on filing a bug report, but I wanted to check here first in
case I'm just smoking something.

STeVe

[1] http://docs.python.org/lib/module-textwrap.html
Nov 22 '05 #1
6 2024
Steven Bethard wrote:
Note that even though the tabs are internal, they are still removed by
textwrap.dedent(). The documentation[1] says: .... So it looks to me like even if this is a "feature" it is undocumented.
I'm planning on filing a bug report, but I wanted to check here first in
case I'm just smoking something.


While I wouldn't say it's obvious, I believe it is (indirectly?)
documented and deliberate.

Search for this in the docs:
"""
expand_tabs
(default: True) If true, then all tab characters in text will be
expanded to spaces using the expandtabs() method of text.
"""

(This is not to say a specific rewording of the docs to lessen any
confusion in this area wouldn't be welcomed.)

-Peter
Nov 22 '05 #2
Steven Bethard wrote:
Note that even though the tabs are internal, they are still removed by
textwrap.dedent(). The documentation[1] says: .... So it looks to me like even if this is a "feature" it is undocumented.
I'm planning on filing a bug report, but I wanted to check here first in
case I'm just smoking something.


While I wouldn't say it's obvious, I believe it is (indirectly?)
documented and deliberate.

Search for this in the docs:
"""
expand_tabs
(default: True) If true, then all tab characters in text will be
expanded to spaces using the expandtabs() method of text.
"""

(This is not to say a specific rewording of the docs to lessen any
confusion in this area wouldn't be welcomed.)

-Peter
Nov 22 '05 #3
Peter Hansen wrote:
Steven Bethard wrote:
Note that even though the tabs are internal, they are still removed by
textwrap.dedent(). The documentation[1] says:


...
So it looks to me like even if this is a "feature" it is undocumented.
I'm planning on filing a bug report, but I wanted to check here first
in case I'm just smoking something.


While I wouldn't say it's obvious, I believe it is (indirectly?)
documented and deliberate.

Search for this in the docs:
"""
expand_tabs
(default: True) If true, then all tab characters in text will be
expanded to spaces using the expandtabs() method of text.
"""


Thanks for double-checking this for me. I looked at expand_tabs, and
it's part of the definition of the TextWrapper class, which is not
actually used by textwrap.dedent(). So I think the textwrap.dedent()
expanding-of-tabs behavior is still basically undocumented.

I looked at the source code, and the culprit is the first line of the
function definition:

lines = text.expandtabs().split('\n')

I filed a bug_ report, but left the Category unassigned so that someone
else can decide whether it's a doc bug or a code bug.

STeVe

... _bug: http://python.org/sf/1361643
Nov 22 '05 #4
Peter Hansen wrote:
Steven Bethard wrote:
Note that even though the tabs are internal, they are still removed by
textwrap.dedent(). The documentation[1] says:


...
So it looks to me like even if this is a "feature" it is undocumented.
I'm planning on filing a bug report, but I wanted to check here first
in case I'm just smoking something.


While I wouldn't say it's obvious, I believe it is (indirectly?)
documented and deliberate.

Search for this in the docs:
"""
expand_tabs
(default: True) If true, then all tab characters in text will be
expanded to spaces using the expandtabs() method of text.
"""


Thanks for double-checking this for me. I looked at expand_tabs, and
it's part of the definition of the TextWrapper class, which is not
actually used by textwrap.dedent(). So I think the textwrap.dedent()
expanding-of-tabs behavior is still basically undocumented.

I looked at the source code, and the culprit is the first line of the
function definition:

lines = text.expandtabs().split('\n')

I filed a bug_ report, but left the Category unassigned so that someone
else can decide whether it's a doc bug or a code bug.

STeVe

... _bug: http://python.org/sf/1361643
Nov 22 '05 #5
Steven Bethard wrote:
Thanks for double-checking this for me. I looked at expand_tabs, and
it's part of the definition of the TextWrapper class, which is not
actually used by textwrap.dedent(). So I think the textwrap.dedent()
expanding-of-tabs behavior is still basically undocumented.


Ah, good point. I saw dedent() in there with the wrap() and fill()
convenience functions which use a TextWrapper internally, but you're
quite right that dedent() is different, and in fact merely uses the
expandtabs() functionality of the standard string class, so this has
nothing to do with the expand_tabs attribute I pointed out.

-Peter
Nov 22 '05 #6
Steven Bethard wrote:
Thanks for double-checking this for me. I looked at expand_tabs, and
it's part of the definition of the TextWrapper class, which is not
actually used by textwrap.dedent(). So I think the textwrap.dedent()
expanding-of-tabs behavior is still basically undocumented.


Ah, good point. I saw dedent() in there with the wrap() and fill()
convenience functions which use a TextWrapper internally, but you're
quite right that dedent() is different, and in fact merely uses the
expandtabs() functionality of the standard string class, so this has
nothing to do with the expand_tabs attribute I pointed out.

-Peter
Nov 22 '05 #7

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
1
by: Joshua Burvill | last post by:
Hello, How can I use the textwrap (new in 2.3) module if I have to use python 2.1? Rgds, Josh
3
by: Patrick Cannon | last post by:
I have been using VB.NET since its beta release a few years ago. I was blown away by its debugging capabilities. The ability to drill down into the member variables of an object in the watch...
1
by: gjohannes | last post by:
Hi. I use Idle 1.1.1 on Python 2.4.1. The "Ctrl-" key bindings for indenting do not work on non-us keyboards where brackets are accessed by the "Alt Gr" key. The Tab key seem to work for...
0
by: Steven Bethard | last post by:
So I've recently been making pretty frequent use of textwrap.dedent() to allow me to use triple-quoted strings at indented levels of code without getting the extra spaces prefixed to each line. I...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
15
by: Tom Plunket | last post by:
The documentation for dedent says, "Remove any whitespace than can be uniformly removed from the left of every line in `text`", yet I'm finding that it's also modifying the '\t' characters, which...
3
by: TP | last post by:
Hi everybody, Recently, I have tried to improve the look of the printed text in command line. For this, I was compelled to remove redundant spaces in strings, because in my scripts, often the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.