Forums

Multiline strings

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Re: Multiline strings

Postby hopscc » Mon Jan 05, 2009 3:12 am

hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Multiline strings

Postby jonathandavid » Tue Jan 06, 2009 6:07 am

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

Postby jonathandavid » Tue Jan 06, 2009 8:45 am

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.
jonathandavid
 
Posts: 159

Re: Multiline strings

Postby Charles » Sun Jan 11, 2009 7:49 pm

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

Postby hopscc » Mon Jan 12, 2009 5:01 am

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

Postby jonathandavid » Mon Jan 12, 2009 10:30 am

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

Postby agustech » Tue Jan 13, 2009 12:41 am

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:

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

Postby hopscc » Thu Aug 27, 2009 8:20 am

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)
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'
then this is removed and all subsequent lines are
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

Postby hopscc » Thu Aug 27, 2009 8:26 am

Followup question:

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

Postby gauthier » Thu Aug 27, 2009 2:48 pm

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
gauthier
 
Posts: 116

PreviousNext

Return to Discussion

Who is online

Users browsing this forum: No registered users and 39 guests