multiline repr (code generation)

R

Rasmus Fogh

Dear All,

I need a way of writing strings or arbitrary Python code that will

a) allow the strings to be read again unchanged (like repr)
b) write multiline strings as multiline strings instead of escaping the
\n's.

A repr function that output triple-quoted strings with explicit
(non-escaped) linebreaks would be perfect.

Failing that, what is the simplest way to get close to the goal?

=======================================================================

Details:

I am working on an automatic code generation project, using Python scripts
to generate Python code.

As part of that I need to:

1) read Python code from a non-Python external file (more precisely, from
storage in a UML editing tool). I do control the strings, but they should
be legible and correct as Python code.

2) write the string to file, in such as way that they will come back
unchanged when the file is imported, e.g.
codeInfo = {'dataConstraint':"""
my
multiline
code string
"""}

3) write the string out to a file as actual executable python code.

4) be able to handle any valid Python code in the string, except for
embedded multiline strings.

5) Have the strings appear as multiline in my various files. A single
400-character line with embedded '\n's may be correct Python code, but it
is so unreadable that I would prefer to avoid it.

If I cannot have all my requirements, I would like the best possible
compromise.

Any helpful suggestions?

Thanks,

Rasmus
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top