multiline repr (code generation)

Rasmus Fogh rhf22 at mole.bio.cam.ac.uk
Wed Sep 3 16:40:08 CEST 2003


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

---------------------------------------------------------------------------
Dr. Rasmus H. Fogh                  Email: r.h.fogh at bioc.cam.ac.uk
Dept. of Biochemistry, University of Cambridge,
80 Tennis Court Road, Cambridge CB2 1GA, UK.     FAX (01223)766002






More information about the Python-list mailing list