Icecream separates Strings at whitespaces & '\\n'
When I use
from icecream import ic
ic('A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file')
resulting in:
ic| 'A\veryvery\long\path\to\no\even\longer\Bioblank _01_Heritisfinallythe file.file': ('A\veryvery\long\path\to
o\even\longer\HelloWorld ' '_01_Heritisfinallythe file.file')
Two things happen:
- The escape before
\ni.e\\nis ignored and a new line created - The string is cut into little bits for representation
I tried to figure out where within my code the string does get turned into a tuple, but it turns out it does not, it's just displayed wrong. I know ('' '') is another way to write a sting, but I found it very confusing as it resembled a tuple closely.
If you change quotes to double ones, then everything works as expected?
No, I don't know why I mentioned this. It was months ago. But here is the output with double quotes:
ic("A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file")
ic| ('A\veryvery\long\path\to
o\even\longer\HelloWorld ' '_01_Heritisfinallythe file.file')
So, no changes there.
Bonus: Using raw Strings has the same display.
ic(r"A\veryvery\long\path\to\no\even\longer\HelloWorld _01_Heritisfinallythe file.file")
ic| ('A\veryvery\long\path\to
o\even\longer\HelloWorld ' '_01_Heritisfinallythe file.file')
@JosuaCarl As far as I can tell, it looks like new lines on \n are a bug, but as for others, we have a line wrap width constant, and it's impossible to configure it right now.
I'm working on improvement for this.
@JosuaCarl Workaround for you could be the following: ic.configureOutput(argToStringFunction=repr).
For the code like this:
from icecream import ic
ic.configureOutput(argToStringFunction=repr)
test1 = "A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file"
test2 = r"A\veryvery\long\path\to\no\even\longer\HelloWorld _01_Heritisfinallythe file.file"
# I have added backslach for each invalid sequence here
test3 = "A\veryvery\\long\\path\to\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file"
ic(test1)
ic(test2)
ic(test3)
It give output like this:
ic| test1: 'A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file'
ic| test2: 'A\\veryvery\\long\\path\\to\\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file'
ic| test3: 'A\x0beryvery\\long\\path\to\no\\even\\longer\\HelloWorld _01_Heritisfinallythe file.file'
I'm considering to make this behavior by default, coz it looks strange if you have a line with "\n" but as output you get actually new line in the place.
I don't know whether it makes sense to always have the raw representation as an output. Especially when checking whether the strings are correct, this could be unhelpful behavior. Could the line not just be separated at the current line-width like this:
import os
str = "Some arbitrary string with almost arbitrary ~ \\n symbols" *10
width = os.get_terminal_size().columns
start = 0
for end in range(0, len(str), width):
print(str[start:end] + "<---- line break")
start = end
print(str[start:])
Some arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost <---- line break arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitra<---- line break ry string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbolsSome arbitrary string with almost arbitrary ~ \n symbols
There are some updates due this issue: https://github.com/gruns/icecream/releases/tag/untagged-19e3d02255ee5494672f
@JosuaCarl take a look
The link is unfortunately a 404.
@JosuaCarl Finally we have find time with @gruns to do this: https://github.com/gruns/icecream/releases/tag/v2.1.5
Thanks a bunch :) Great work 👍