From: Coby Beck
Subject: printing python
Date: 
Message-ID: <OyH0f.1729$yS6.1424@clgrps12>
Hello,

I have been spending so much time lately writing SQL that I fear I come back 
here after months of debating global warming on sci.environment with a 
newbie question.

I want to generate a DTML string that will be passed in a shell script to a 
python function.  This string has newlines in it but for reasons unknown to 
me and only of very passing interest this function will not accept any 
actual returns in its string parameters.

I want a function PYTHONIZE-STRING that will take this:

"function blah(){
foo='bar'
}
"
and return this:

"function blah(){\nfoo=\\'bar\\'\n}\n"


I wrote this:
(defun pythonize-string (string)
  (substitute-string "\n" (format nil "~C" #\Newline) (python-quote 
string)))

but it does not have the slash in front of the n in the returned string and 
putting two slashes leaves two slashes.

So my problem is down to this:

CL-USER 67 > (format nil "\\n")
"\\n"

CL-USER 68 > (format nil "\n")
"n"

CL-USER 69 > ???
"\n"


-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")


;; my code above uses these two helpers (not written by me)
(defun python-quote (string)
  (substitute-string "\\'" "'" (substitute-string "\\\\" "\\" (enstring 
string))))

(defun substitute-string (new old string)
  (let ((end (length string))
        (olen (length old)))
    (do ((pos (search old string :from-end t :end2 end)
              (search old string :from-end t :end2 end))
         (pieces '()))
      ((not pos)
       (push (subseq string 0 end) pieces)
       (apply #'concatenate 'string pieces))
      (push (subseq string (+ pos olen) end) pieces)
      (push new pieces)
      (setq end pos))))

From: M Jared Finder
Subject: Re: printing python
Date: 
Message-ID: <TJmdnfRJKeiG097eRVn-jw@speakeasy.net>
Coby Beck wrote:
> I have been spending so much time lately writing SQL that I fear I come back 
> here after months of debating global warming on sci.environment with a 
> newbie question.
> 
> I want to generate a DTML string that will be passed in a shell script to a 
> python function.  This string has newlines in it but for reasons unknown to 
> me and only of very passing interest this function will not accept any 
> actual returns in its string parameters.
> 
> I want a function PYTHONIZE-STRING that will take this:
> 
> "function blah(){
> foo='bar'
> }
> "
> and return this:
> 
> "function blah(){\nfoo=\\'bar\\'\n}\n"
> 
> 
> I wrote this:
> (defun pythonize-string (string)
>   (substitute-string "\n" (format nil "~C" #\Newline) (python-quote 
> string)))
> 
> but it does not have the slash in front of the n in the returned string and 
> putting two slashes leaves two slashes.

"\\n" is what you want returned.  "\\n" is the (lisp) readable 
representation of a string that contains two characters, a backslash 
followed by an n.

CL-USER> (princ "\\n")
\n
"\\n"

Just "\n" contains one character, the character n:

CL-USER> (princ "\n")
n
"n"

Make sure to print out your string; Lisp repls show the return value in 
a format suitable to read by Lisp.

   -- MJF
From: Coby Beck
Subject: Re: printing python
Date: 
Message-ID: <lGR0f.2812$Io.2184@clgrps13>
"M Jared Finder" <·····@hpalace.com> wrote in message 
···························@speakeasy.net...
> Coby Beck wrote:
>> I have been spending so much time lately writing SQL that I fear I come 
>> back here after months of debating global warming on sci.environment with 
>> a newbie question.
...
>
> "\\n" is what you want returned.  "\\n" is the (lisp) readable 
> representation of a string that contains two characters, a backslash 
> followed by an n.

Thanks, I knew it was a newbie question.  I tried to PRINT it but should 
have PRINCed it to reassure myself...

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Pisin Bootvong
Subject: Re: printing python
Date: 
Message-ID: <1128485317.609303.166780@z14g2000cwz.googlegroups.com>
Coby Beck เขียน:
> Hello,
>
> I have been spending so much time lately writing SQL that I fear I come back
> here after months of debating global warming on sci.environment with a
> newbie question.
>
> I want to generate a DTML string that will be passed in a shell script to a
> python function.  This string has newlines in it but for reasons unknown to
> me and only of very passing interest this function will not accept any
> actual returns in its string parameters.
>
> I want a function PYTHONIZE-STRING that will take this:
>
> "function blah(){
> foo='bar'
> }
> "
> and return this:
>
> "function blah(){\nfoo=\\'bar\\'\n}\n"
>
>
> I wrote this:
> (defun pythonize-string (string)
>   (substitute-string "\n" (format nil "~C" #\Newline) (python-quote
> string)))
>
> but it does not have the slash in front of the n in the returned string and
> putting two slashes leaves two slashes.
>
> So my problem is down to this:
>
> CL-USER 67 > (format nil "\\n")
> "\\n"
>
> CL-USER 68 > (format nil "\n")
> "n"
>
> CL-USER 69 > ???
> "\n"
>
>
> --
> Coby Beck
> (remove #\Space "coby 101 @ bigpond . com")
>
>
> ;; my code above uses these two helpers (not written by me)
> (defun python-quote (string)
>   (substitute-string "\\'" "'" (substitute-string "\\\\" "\\" (enstring
> string))))
>
> (defun substitute-string (new old string)
>   (let ((end (length string))
>         (olen (length old)))
>     (do ((pos (search old string :from-end t :end2 end)
>               (search old string :from-end t :end2 end))
>          (pieces '()))
>       ((not pos)
>        (push (subseq string 0 end) pieces)
>        (apply #'concatenate 'string pieces))
>       (push (subseq string (+ pos olen) end) pieces)
>       (push new pieces)
>       (setq end pos))))

You are just fooled by by REPL. (format nil) return the formatted
string, but REPL prints it in backslash-escaped looks.


CL-USER> (format t "~S" "\\n")
"\\n"
NIL
CL-USER> (format t "\n")
n
NIL
CL-USER> (format t "\\n")
\n
NIL

>From SLIME, The first line of output and the second line is indifferent
colours. The first line is the output text written to screen. The
second line is the return value of FORMAT. So It seems that the string
"\\n" is really printed as "\n".

Now with (format nil):

CL-USER> (format nil "\\n")
"\\n"

Only one line of REPL output, the returned value of format. Right click
on the return value to inspect it, I got.

An array.
 [type: (SIMPLE-BASE-STRING 2)]
--------------------
Dimensions: (2)
Its element type is: CHARACTER
Total size: 2
Adjustable: NIL
Contents:
0: #\\
1: #\n

Voila, It's actually two character, it's just printed on screen the
different way it is actually write in to files.

SLIME is nice!

Cheers,
From: Lars Brinkhoff
Subject: Re: printing python
Date: 
Message-ID: <85mzlojt8l.fsf@junk.nocrew.org>
"Coby Beck" <·····@mercury.bc.ca> writes:
> (format nil "~C" #\Newline)

I suggest #.(string #\Newline)
From: Marco Baringer
Subject: Re: printing python
Date: 
Message-ID: <m27jcsqtqe.fsf@soma.local>
Lars Brinkhoff <·········@nocrew.org> writes:

> "Coby Beck" <·····@mercury.bc.ca> writes:
>> (format nil "~C" #\Newline)
>
> I suggest #.(string #\Newline)

or (terpri nil)
From: Zach Beane
Subject: Re: printing python
Date: 
Message-ID: <m3u0fw9oy4.fsf@unnamed.xach.com>
Marco Baringer <··@bese.it> writes:

> Lars Brinkhoff <·········@nocrew.org> writes:
> 
> > "Coby Beck" <·····@mercury.bc.ca> writes:
> >> (format nil "~C" #\Newline)
> >
> > I suggest #.(string #\Newline)
> 
> or (terpri nil)

If only this worked! "nil" doesn't have the same meaning for terpri as
it does for format, though.

Zach
From: Marco Baringer
Subject: Re: printing python
Date: 
Message-ID: <m264sbvu1y.fsf@soma.local>
Zach Beane <····@xach.com> writes:

> Marco Baringer <··@bese.it> writes:
>
>> Lars Brinkhoff <·········@nocrew.org> writes:
>> 
>> > "Coby Beck" <·····@mercury.bc.ca> writes:
>> >> (format nil "~C" #\Newline)
>> >
>> > I suggest #.(string #\Newline)
>> 
>> or (terpri nil)
>
> If only this worked! "nil" doesn't have the same meaning for terpri as
> it does for format, though.

terpri's second argument is a stream designator which, according to
the glossary, is:

a designator for a stream; that is, an object that denotes a stream
and that is one of: t (denoting the value of *terminal-io*), nil
(denoting the value of *standard-input* for input stream designators
or denoting the value of *standard-output* for output stream
designators), or a stream (denoting itself).

am i missing something?

-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
	-Leonard Cohen
From: Marco Baringer
Subject: Re: printing python
Date: 
Message-ID: <m21x2zvtzx.fsf@soma.local>
"Marco Baringer" <··@bese.it> writes:

> am i missing something?

most definetely yes. sorry for the noise.

-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
	-Leonard Cohen
From: Wade Humeniuk
Subject: Re: printing python
Date: 
Message-ID: <PMQ0f.934$S4.396@edtnps84>
Just as a demo, to show that \n shows up as \\n in the repl

(defun pythstring (string)
   (with-output-to-string (*standard-output*)
     (loop for c across string
           if (char= c #\newline) do (write-char #\\) (write-char #\n)
           else do (write-char c))))

CL-USER 9 > (pythstring "function blah(){
foo='bar'
}
")
"function blah(){\\nfoo='bar'\\n}\\n"

CL-USER 10 >

Wade