Csound Csound-dev Csound-tekno Search About

string weirdness in pyruni

Date2016-11-07 13:45
FromRichard
Subjectstring weirdness in pyruni
Some perfectly legal Python code generates a syntax error in pyruni:

pyruni: python exception
   File "", line 31
     score = rowstring.rstrip('
                              ^
SyntaxError: EOL while scanning string literal

Only if I replace the single quotes in line 31 with triple quotes I get 
no error and it works..

Here's the code:



-dnm0



pyinit

pyruni {{
import sqlite3 as lite

rows = []

def getscore(i):
     con = lite.connect('test.db')
     cur = con.cursor()

     cur.execute("""SELECT score FROM scores WHERE name='Rock3' """)
     rows = cur.fetchall()
     rowstring = rows[0][0]
     con.close()
     # following line gives the syntax error
     score = rowstring.rstrip('\n').split('\n')
     print score
     return 1.0

}}

instr 10 ;call it
;ival   pycall1i "select", 1
ival   pycall1i "getscore", 0
prints "val = %f\n", ival
endin



i 10 0 1  100  200
i 10 1 1  1000 2000



Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2016-11-07 14:21
FromFrancois PINOT
SubjectRe: string weirdness in pyruni
This is a tricky issue. The python code passed to pyruni is a long string delimited by double braces. Csound will evaluate this long string before passing it to pyruni. During this evaluation, Csound uses C rules for evaluating literal strings; this means that you have to escape special characters like \n so that Csound will not interpret them during the string evaluation. So your code should be like this:

          score = rowstring.rstrip('\\n').split('\\n')

François

2016-11-07 14:45 GMT+01:00 Richard <zappfinger@gmail.com>:
Some perfectly legal Python code generates a syntax error in pyruni:

pyruni: python exception
  File "<string>", line 31
    score = rowstring.rstrip('
                             ^
SyntaxError: EOL while scanning string literal

Only if I replace the single quotes in line 31 with triple quotes I get no error and it works..

Here's the code:

<CsoundSynthesizer>
<CsOptions>
-dnm0
</CsOptions>
<CsInstruments>

pyinit

pyruni {{
import sqlite3 as lite

rows = []

def getscore(i):
    con = lite.connect('test.db')
    cur = con.cursor()

    cur.execute("""SELECT score FROM scores WHERE name='Rock3' """)
    rows = cur.fetchall()
    rowstring = rows[0][0]
    con.close()
    # following line gives the syntax error
    score = rowstring.rstrip('\n').split('\n')
    print score
    return 1.0

}}

instr 10 ;call it
;ival   pycall1i "select", 1
ival   pycall1i "getscore", 0
prints "val = %f\n", ival
endin

</CsInstruments>
<CsScore>
i 10 0 1  100  200
i 10 1 1  1000 2000
</CsScore>
</CsoundSynthesizer>

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
       https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2016-11-07 15:17
FromJoe Knapka
SubjectRe: string weirdness in pyruni
Hi Richard,

Strings containing literal line breaks do require triple quotes. Try it in the Python interpreter to confirm.

Cheers,

- Joe

On Mon, Nov 7, 2016 at 6:45 AM, Richard <zappfinger@gmail.com> wrote:
Some perfectly legal Python code generates a syntax error in pyruni:

pyruni: python exception
  File "<string>", line 31
    score = rowstring.rstrip('
                             ^
SyntaxError: EOL while scanning string literal

Only if I replace the single quotes in line 31 with triple quotes I get no error and it works..

Here's the code:

<CsoundSynthesizer>
<CsOptions>
-dnm0
</CsOptions>
<CsInstruments>

pyinit

pyruni {{
import sqlite3 as lite

rows = []

def getscore(i):
    con = lite.connect('test.db')
    cur = con.cursor()

    cur.execute("""SELECT score FROM scores WHERE name='Rock3' """)
    rows = cur.fetchall()
    rowstring = rows[0][0]
    con.close()
    # following line gives the syntax error
    score = rowstring.rstrip('\n').split('\n')
    print score
    return 1.0

}}

instr 10 ;call it
;ival   pycall1i "select", 1
ival   pycall1i "getscore", 0
prints "val = %f\n", ival
endin

</CsInstruments>
<CsScore>
i 10 0 1  100  200
i 10 1 1  1000 2000
</CsScore>
</CsoundSynthesizer>

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
       https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here




--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2016-11-07 15:48
FromRichard
SubjectRe: string weirdness in pyruni

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split('\n')


Date2016-11-07 17:40
FromJoe Knapka
SubjectRe: string weirdness in pyruni
Escaped newlines are fine ('abc\nxyz'). Literal newlines are not ('abc
xyy'), unless the string is triple-quoted.

- Joe

On Mon, Nov 7, 2016 at 8:48 AM, Richard <zappfinger@gmail.com> wrote:

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split('\n')

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here



--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2016-11-07 18:15
FromRichard
SubjectRe: string weirdness in pyruni

Ok, I see your point about literal newlines. But I am only using escaped newlines in this example.
So this might be a bug in pyruni.

Richard


On 07/11/16 18:40, Joe Knapka wrote:
Escaped newlines are fine ('abc\nxyz'). Literal newlines are not ('abc
xyy'), unless the string is triple-quoted.

- Joe

On Mon, Nov 7, 2016 at 8:48 AM, Richard <zappfinger@gmail.com> wrote:

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split('\n')

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here



--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here


Date2016-11-07 18:52
FromJohn ff
SubjectRe: string weirdness in pyruni
Depends on who is reading the string and who expands escaped characters

Sent from TypeApp
On 7 Nov 2016, at 18:16, Richard <zappfinger@GMAIL.COM> wrote:

Ok, I see your point about literal newlines. But I am only using escaped newlines in this example.
So this might be a bug in pyruni.

Richard


On 07/11/16 18:40, Joe Knapka wrote:
Escaped newlines are fine ('abc\nxyz'). Literal newlines are not ('abc
xyy'), unless the string is triple-quoted.

- Joe

On Mon, Nov 7, 2016 at 8:48 AM, Richard <zappfinger@gmail.com> wrote:

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split(' \n')

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here



--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2016-11-07 19:09
FromFrancois PINOT
SubjectRe: string weirdness in pyruni
This is a tricky issue. The python code passed to pyruni is a long string delimited by double braces. Csound will evaluate this long string before passing it to pyruni. During this evaluation, Csound uses C rules for evaluating literal strings; this means that you have to escape special characters like \n so that Csound will not interpret them during the string evaluation. So your code should be like this:

          score = rowstring.rstrip('\\n').split('\\n')

François

2016-11-07 19:52 GMT+01:00 John ff <jpff@codemist.co.uk>:
Depends on who is reading the string and who expands escaped characters

Sent from TypeApp
On 7 Nov 2016, at 18:16, Richard <zappfinger@GMAIL.COM> wrote:

Ok, I see your point about literal newlines. But I am only using escaped newlines in this example.
So this might be a bug in pyruni.

Richard


On 07/11/16 18:40, Joe Knapka wrote:
Escaped newlines are fine ('abc\nxyz'). Literal newlines are not ('abc
xyy'), unless the string is triple-quoted.

- Joe

On Mon, Nov 7, 2016 at 8:48 AM, Richard <zappfinger@gmail.com> wrote:

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split(' \n')

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here



--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2016-11-07 19:26
FromRichard
SubjectRe: string weirdness in pyruni

You are right, the double escape seems to work.
Is this documented somewhere? If not, I think it should be.

Richard


On 07/11/16 20:09, Francois PINOT wrote:
This is a tricky issue. The python code passed to pyruni is a long string delimited by double braces. Csound will evaluate this long string before passing it to pyruni. During this evaluation, Csound uses C rules for evaluating literal strings; this means that you have to escape special characters like \n so that Csound will not interpret them during the string evaluation. So your code should be like this:

          score = rowstring.rstrip('\\n').split('\\n')

François

2016-11-07 19:52 GMT+01:00 John ff <jpff@codemist.co.uk>:
Depends on who is reading the string and who expands escaped characters

Sent from TypeApp
On 7 Nov 2016, at 18:16, Richard <zappfinger@GMAIL.COM> wrote:

Ok, I see your point about literal newlines. But I am only using escaped newlines in this example.
So this might be a bug in pyruni.

Richard


On 07/11/16 18:40, Joe Knapka wrote:
Escaped newlines are fine ('abc\nxyz'). Literal newlines are not ('abc
xyy'), unless the string is triple-quoted.

- Joe

On Mon, Nov 7, 2016 at 8:48 AM, Richard <zappfinger@gmail.com> wrote:

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split(' \n')

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here



--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here


Date2016-11-08 09:29
FromFrancois PINOT
SubjectRe: string weirdness in pyruni
I added a note about it in the Strings article of the manual.

François

2016-11-07 20:26 GMT+01:00 Richard <zappfinger@gmail.com>:

You are right, the double escape seems to work.
Is this documented somewhere? If not, I think it should be.

Richard


On 07/11/16 20:09, Francois PINOT wrote:
This is a tricky issue. The python code passed to pyruni is a long string delimited by double braces. Csound will evaluate this long string before passing it to pyruni. During this evaluation, Csound uses C rules for evaluating literal strings; this means that you have to escape special characters like \n so that Csound will not interpret them during the string evaluation. So your code should be like this:

          score = rowstring.rstrip('\\n').split('\\n')

François

2016-11-07 19:52 GMT+01:00 John ff <jpff@codemist.co.uk>:
Depends on who is reading the string and who expands escaped characters

Sent from TypeApp
On 7 Nov 2016, at 18:16, Richard <zappfinger@GMAIL.COM> wrote:

Ok, I see your point about literal newlines. But I am only using escaped newlines in this example.
So this might be a bug in pyruni.

Richard


On 07/11/16 18:40, Joe Knapka wrote:
Escaped newlines are fine ('abc\nxyz'). Literal newlines are not ('abc
xyy'), unless the string is triple-quoted.

- Joe

On Mon, Nov 7, 2016 at 8:48 AM, Richard <zappfinger@gmail.com> wrote:

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split(' \n')

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here



--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2016-11-08 15:12
FromRichard
SubjectRe: string weirdness in pyruni

Great, thanks!

Richard


On 08/11/16 10:29, Francois PINOT wrote:
I added a note about it in the Strings article of the manual.

François

2016-11-07 20:26 GMT+01:00 Richard <zappfinger@gmail.com>:

You are right, the double escape seems to work.
Is this documented somewhere? If not, I think it should be.

Richard


On 07/11/16 20:09, Francois PINOT wrote:
This is a tricky issue. The python code passed to pyruni is a long string delimited by double braces. Csound will evaluate this long string before passing it to pyruni. During this evaluation, Csound uses C rules for evaluating literal strings; this means that you have to escape special characters like \n so that Csound will not interpret them during the string evaluation. So your code should be like this:

          score = rowstring.rstrip('\\n').split('\\n')

François

2016-11-07 19:52 GMT+01:00 John ff <jpff@codemist.co.uk>:
Depends on who is reading the string and who expands escaped characters

Sent from TypeApp
On 7 Nov 2016, at 18:16, Richard <zappfinger@GMAIL.COM> wrote:

Ok, I see your point about literal newlines. But I am only using escaped newlines in this example.
So this might be a bug in pyruni.

Richard


On 07/11/16 18:40, Joe Knapka wrote:
Escaped newlines are fine ('abc\nxyz'). Literal newlines are not ('abc
xyy'), unless the string is triple-quoted.

- Joe

On Mon, Nov 7, 2016 at 8:48 AM, Richard <zappfinger@gmail.com> wrote:

Hi Joe,

I did just that in the Python2.7 interpreter, and it works..

>>> li = 'a b c\nd b g\nx y z\n'
>>> l1 = li.split('\n')
>>> l1
['a b c', 'd b g', 'x y z', '']
>>> l1 = li.rstrip('\n').split('\n')
>>> l1
['a b c', 'd b g', 'x y z']


Richard


On 07/11/16 16:17, Joe Knapka wrote:
score = rowstring.rstrip('\n').split(' \n')

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here



--
It is always best to think of reality as perfectly normal.  Since the beginning, not one unusual thing has ever happened. - Less Wrong
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here