Forums
Multiline strings
Re: Multiline strings
Chuck wrote:For Cobra, I'm playing with the idea of requiring that the multiline strings be indented one level. My experience with Python is that my programs can look quite strange when the string contents are flush against the left side, which breaks up the indentation of the code. So that would be:def foo
one = """
foo foo
bar bar bar"""
print one
two = """
foo foo
bar bar bar
"""
print two
In my opinion this is the best possible solution. That uber-raw or whatever it's called approach seems too hard on the eyes, with all the "|" characters. I'm assuming that the above approach would allow us to introduce whitespace at the beginning of lines, right?
def foo
one = """
First line has more indentation
Remaining lines have 1 level
This one too
Here I want even more whitespace
"""
print one
I don't think you'll find a solution that is cleaner and easier to type than this.
- jonathandavid
- Posts: 159
Re: Multiline strings
By the way, I really like another of the things proposed in Ideas for Python Enhancements.
Namely:
Chuck: would that be doable? Cobra's line continuation character _ is not as ugly as Python's, but no character would be even better.
Namely:
Line continuation
The use of a backslash to signify line continuation is, in my opinion, one of the very few ugly things about Python syntax. The tragedy is that it's not even necessary! Indentation could perfectly well be used to infer continuation of an expression or statement over multiple lines, e.g.
- Code: Select all
fred = spam + (ham - eggs) *
sqrt(lettuce / tomato)
Chuck: would that be doable? Cobra's line continuation character _ is not as ugly as Python's, but no character would be even better.
- jonathandavid
- Posts: 159
Re: Multiline strings
jonathandavid wrote:By the way, I really like another of the things proposed in Ideas for Python Enhancements.
Namely:Line continuation
The use of a backslash to signify line continuation is, in my opinion, one of the very few ugly things about Python syntax. The tragedy is that it's not even necessary! Indentation could perfectly well be used to infer continuation of an expression or statement over multiple lines, e.g.
- Code: Select all
fred = spam + (ham - eggs) *
sqrt(lettuce / tomato)
Chuck: would that be doable? Cobra's line continuation character _ is not as ugly as Python's, but no character would be even better.
I think it's doable, but I wonder why the Python folks shied away from it. We currently have implicit continuation inside ( ... ), { ... } and [ ... ]. I have speculated in the past that we could also have it for binary operators. It's interesting to see in your example that your first line ends on such.
But for now this is a low priority. I don't see much line continuation coming in Cobra source code outside or parens, lists and dictionaries.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Multiline strings
that uber-raw or whatever it's called approach seems too hard on the eyes, with all the "|" characters. I'm assuming that the above approach would allow us to introduce whitespace at the beginning of lines, right?
One of the points of that syntax is so you dont have to ask that question (ever again).
The "|" chars show exactly what whitespace (leading and trailing) will be included in the output lines
(pedantically, not including the trailing newline).
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Multiline strings
hopscc wrote:that uber-raw or whatever it's called approach seems too hard on the eyes, with all the "|" characters. I'm assuming that the above approach would allow us to introduce whitespace at the beginning of lines, right?
One of the points of that syntax is so you dont have to ask that question (ever again).
The "|" chars show exactly what whitespace (leading and trailing) will be included in the output lines
(pedantically, not including the trailing newline).
I see your point, but I still prefer to ask the question, memorize the rule, and then spare myself the need to write / read a zillion '|' characters during my developer's life.
- jonathandavid
- Posts: 159
Re: Multiline strings
Please keep it simple, it is something quite used on code generation.
It must be easy to type and easy to read.
So please leave it as:
in most IDE is easy to indent a bunch of code/text just hitting tab, while typing \r\n at the end, line continuations, and/or "|" is at least tiresome if not anger prone.
It must be easy to type and easy to read.
So please leave it as:
- Code: Select all
s = """
|indent|
|indent|"""
in most IDE is easy to indent a bunch of code/text just hitting tab, while typing \r\n at the end, line continuations, and/or "|" is at least tiresome if not anger prone.
- agustech
- Posts: 37
Re: Multiline strings
Theres now a patch for supporting this on ticket:120
Its implemented as a new literal stringtype which may be either a single or multiple lines in length (bigRaw string)
Two conveniences;
If the second line of the bigRawString is indented one level greater than the line the bigRawString started on then any leading indentation to that level in each line will be stripped off
This preserves code formatting and gives some visual formatting of the output
If the second line isnt indented to that extent then no indentation stripping will be done ( What You Cant See is What Youll Get).
If a line has a deficit of indentation compared to the first line then its stripped off up to the first non whitespace.
Secondly as per the previous uberstring discusssion bigraw strings can be optionally explicitly bounded for whitespace trimming and visual alignment
If the first 3 characters of a multistring bigraw string start with
processed so that any leading whitespace up to and including a '| character is removed and any trailing '|' (and any non eoln whitespace after it) is also removed.
If there are no leading or trailing "|' on the line the line is left as is ( in either case the above auto indent removal if applicable is suppressed as well)
heres a readability improvement webnov8 might recognise
Its implemented as a new literal stringtype which may be either a single or multiple lines in length (bigRaw string)
- Code: Select all
a = R"
This is a line
Here is another line"
b = R'{
"profile_color":"9ae4e8",
"url":null,
"statuses_count":1,
"protected":false}'
Two conveniences;
If the second line of the bigRawString is indented one level greater than the line the bigRawString started on then any leading indentation to that level in each line will be stripped off
This preserves code formatting and gives some visual formatting of the output
- Code: Select all
# these strings are the same
a = R'xxx
yyy
zzz'
b = R'xxx
yyy
zzz'
# same as 'xxx\nyyy\nzzz'
If the second line isnt indented to that extent then no indentation stripping will be done ( What You Cant See is What Youll Get).
If a line has a deficit of indentation compared to the first line then its stripped off up to the first non whitespace.
Secondly as per the previous uberstring discusssion bigraw strings can be optionally explicitly bounded for whitespace trimming and visual alignment
If the first 3 characters of a multistring bigraw string start with
- Code: Select all
r'+|\n'
processed so that any leading whitespace up to and including a '| character is removed and any trailing '|' (and any non eoln whitespace after it) is also removed.
If there are no leading or trailing "|' on the line the line is left as is ( in either case the above auto indent removal if applicable is suppressed as well)
- Code: Select all
a = R'+|
| here is line 1 |
| here is line 2 |
| line3'
assert a == ' here is line1 \n here is line2 \n line3'
b=R'+|
line 1|
| line 2
line3 '
assert a == '\t\tline1\n line2 \n\t\t line3 '
heres a readability improvement webnov8 might recognise
- Code: Select all
js1='{"profile_background_color":"9ae4e8","description":null,"utc_offset":null,"profile_text_color":"000000","followers_count":0,"following":null,"time_zone":null,"friends_count":0,"profile_link_color":"0000ff","url":null,"name":"test123","protected":false,"status":{"text":"testing","truncated":false,"in_reply_to_status_id":null,"in_reply_to_user_id":null,"favorited":false,"created_at":"Fri May 08 19:08:52 +0000 2009","in_reply_to_screen_name":null,"id":1740374460,"source":"web"}"favourites_count":0,}'
js1 = R'{
"profile_background_color":"9ae4e8",
"description":null,
"utc_offset":null,
"profile_text_color":"000000",
"followers_count":0,
"following":null,
"time_zone":null,
"friends_count":0,
"profile_link_color":"0000ff",
"url":null,
"name":"test123",
"protected":false,
"status":{"text":"testing",
"truncated":false,
"in_reply_to_status_id":null,
"in_reply_to_user_id":null,
"favorited":false,
"created_at":"Fri May 08 19:08:52 +0000 2009",
"in_reply_to_screen_name":null,
"id":1740374460,"source":"web"},
"favourites_count":0}'
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Multiline strings
Followup question:
Should this capability be folded into the existing raw strings
or left as a new standalone literal string form
??
Should this capability be folded into the existing raw strings
- Code: Select all
r'......'
or left as a new standalone literal string form
- Code: Select all
R'...........'
# and
R'...........
......
.......''
??
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Multiline strings
I'm looking forward for hopscc patch application!
I'm using cobra to tackle some light ETL scripts (via rhino ETL goodness) and concatenation of long SQL statement is well, a h**sle
Also, a bit after all the rage on the topic, but I thought the perl solution to the problem is also a viable solution we may look for:
http://perl.about.com/od/perltutorials/ ... eredoc.htm
I'm using cobra to tackle some light ETL scripts (via rhino ETL goodness) and concatenation of long SQL statement is well, a h**sle
Also, a bit after all the rage on the topic, but I thought the perl solution to the problem is also a viable solution we may look for:
http://perl.about.com/od/perltutorials/ ... eredoc.htm
- gauthier
- Posts: 116
Who is online
Users browsing this forum: No registered users and 35 guests