Convert arbitrary function inputs to string

D

David

Hi,

I'd like to have a function that takes arbitrary inputs and returns
them as a single string, with proper escapes for special characters I
can define. For example:

fun( ( + 1 2 ) )
=> "( + 1 2)"

or

fun( (define (myhello str) (begin (print (string-append "Hello "
str)) (newline) )) )
=> "(define (myhello str) (begin (print (string-append \"Hello \"
str)) (newline) ))"

Thanks,

-Dave
 
J

Jean-Michel Pichavant

David said:
Hi,

I'd like to have a function that takes arbitrary inputs and returns
them as a single string, with proper escapes for special characters I
can define. For example:

fun( ( + 1 2 ) )
=> "( + 1 2)"

or

fun( (define (myhello str) (begin (print (string-append "Hello "
str)) (newline) )) )
=> "(define (myhello str) (begin (print (string-append \"Hello \"
str)) (newline) ))"

Thanks,

-Dave
Are you talking about python ??

fun( ( + 1 2 ) )
File "<stdin>", line 1
fun( ( + 1 2 ) )
^
SyntaxError: invalid syntax
 
I

Ian

Hi,

I'd like to have a function that takes arbitrary inputs and returns
them as a single string, with proper escapes for special characters I
can define.  For example:

What sorts of arbitrary inputs? Strings? Sequences of strings?
Something else? It's not clear from your examples, which use invalid
syntax.

If you're looking for something like the "quote" form from Lisp, you
can't really do that in Python. Just use a string.

Cheers,
Ian
 
D

David Dreisigmeyer

Yes, I'm calling Gambit-C from Python and would like to make this
cleaner. Instead of having to do something like:

gambit.eval ("(print \"Hello\n\")")

I want to do this:

gambit.eval (print "Hello\n")

so that the expression following gambit.eval is a standard scheme expression.
 
I

Ian

Yes,  I'm calling Gambit-C from Python and would like to make this
cleaner.  Instead of having to do something like:

gambit.eval ("(print \"Hello\n\")")

I want to do this:

gambit.eval (print "Hello\n")

so that the expression following gambit.eval is a standard scheme expression.

That's much clearer. As I indicated in my previous email, there is no
way to do this in Python. You might try using a raw multi-line string
literal to reduce the amount of escaping you need to do. So this:

"(print \"Hello\\n\")"

becomes this:

r"""(print "Hello\n")"""

Cheers,
Ian
 
R

Robert Kern

Yes, I'm calling Gambit-C from Python and would like to make this
cleaner. Instead of having to do something like:

gambit.eval ("(print \"Hello\n\")")

I want to do this:

gambit.eval (print "Hello\n")

so that the expression following gambit.eval is a standard scheme expression.

Sorry, Python's syntax is not extensible like this.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top