multiline repr (code generation)

Discussion in 'Python' started by Rasmus Fogh, Sep 3, 2003.

  1. Rasmus Fogh

    Rasmus Fogh Guest

    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:
    Dept. of Biochemistry, University of Cambridge,
    80 Tennis Court Road, Cambridge CB2 1GA, UK. FAX (01223)766002
     
    Rasmus Fogh, Sep 3, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David C. Fox

    avoiding recursion in repr?

    David C. Fox, Nov 19, 2003, in forum: Python
    Replies:
    2
    Views:
    359
    David C. Fox
    Dec 4, 2003
  2. tcronj

    str() or repr()

    tcronj, Feb 12, 2004, in forum: Python
    Replies:
    0
    Views:
    367
    tcronj
    Feb 12, 2004
  3. Replies:
    2
    Views:
    569
    Jim Sizelove
    Dec 3, 2004
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    357
    John W. Long
    Nov 24, 2003
  5. dale zhang
    Replies:
    8
    Views:
    430
    Tintin
    Nov 30, 2004
Loading...

Share This Page