heredoc and variables

Discussion in 'Python' started by flupke, Jun 4, 2004.

  1. flupke

    flupke Guest

    Hi,

    i have php script where i use a heredoc type of variabele
    to print out some html. In the heredoc, i use the values of
    several other vars.

    php snippet:

    $div=<<<END_DIV
    <DIV class="linkblock">
    <DIV class="linkblock_title">$titel_name</DIV>
    $url
    </DIV>
    </DIV>

    So the variabele div is a heredoc which will contain some
    html woth the vars titel_name and url in it.

    1) Can i do this in python and how?
    2) Consider following heredoc

    end_html="""</BODY>
    </HTML>"""

    I have to write it like this to avoid having an empty line added above
    AND below the actual string when using it.
    So this would produce the extra empty lines

    end_html="""
    </BODY>
    </HTML>
    """

    I actually think that the last piece of code is more readable. Is there
    a way to stop that behaviour or is there a kind of trim function i
    can apply to get rid of the 2 extra lines?

    Thanks
    flupke, Jun 4, 2004
    #1
    1. Advertising

  2. flupke

    flupke Guest

    "flupke" <> schreef in bericht
    news:3U%vc.849$-ops.be...
    > Hi,
    >
    > i have php script where i use a heredoc type of variabele
    > to print out some html. In the heredoc, i use the values of
    > several other vars.
    >
    > php snippet:
    >
    > $div=<<<END_DIV
    > <DIV class="linkblock">
    > <DIV class="linkblock_title">$titel_name</DIV>
    > $url
    > </DIV>
    > </DIV>
    >
    > So the variabele div is a heredoc which will contain some
    > html woth the vars titel_name and url in it.
    >
    > 1) Can i do this in python and how?


    To answer the first part of the question: Yes
    div="""<DIV class="linkblock">
    <DIV class="linkblock_title">%s</DIV>
    %s
    </DIV>
    """

    print div % (title_name,url) works.
    Any idea on the second problem?
    flupke, Jun 4, 2004
    #2
    1. Advertising

  3. flupke

    Peter Otten Guest

    flupke wrote:

    > 2) Consider following heredoc
    >
    > end_html="""</BODY>
    > </HTML>"""
    >
    > I have to write it like this to avoid having an empty line added above
    > AND below the actual string when using it.


    Why would you care about whitespace in HTML?

    > So this would produce the extra empty lines
    >
    > end_html="""
    > </BODY>
    > </HTML>
    > """
    >
    > I actually think that the last piece of code is more readable. Is there
    > a way to stop that behaviour or is there a kind of trim function i
    > can apply to get rid of the 2 extra lines?



    >>> print """\

    .... here's how \
    .... to escape \
    .... newlines"""
    here's how to escape newlines
    >>>


    Peter
    Peter Otten, Jun 4, 2004
    #3
  4. flupke wrote:
    > 2) Consider following heredoc
    >
    > end_html="""</BODY>
    > </HTML>"""
    >
    > I have to write it like this to avoid having an empty line added
    > above AND below the actual string when using it.
    > So this would produce the extra empty lines
    >
    > end_html="""
    > </BODY>
    > </HTML>
    > """
    >
    > I actually think that the last piece of code is more readable. Is
    > there a way to stop that behaviour or is there a kind of trim
    > function i can apply to get rid of the 2 extra lines?


    If you can live with the newline at the end of the text (which in most cases
    is what you want anyway), this is the cleanest way to do it:

    end_html = """\
    </BODY>
    </HTML>
    """

    Or, you can get rid of both newlines this way:

    end_html = """
    </BODY>
    </HTML>
    """[1:-1]

    -Mike
    Michael Geary, Jun 4, 2004
    #4
  5. flupke

    flupke Guest

    Michael Geary wrote:
    > flupke wrote:
    >> 2) Consider following heredoc
    >>
    >> end_html="""</BODY>
    >> </HTML>"""
    >>
    >> I have to write it like this to avoid having an empty line added
    >> above AND below the actual string when using it.
    >> So this would produce the extra empty lines
    >>
    >> end_html="""
    >> </BODY>
    >> </HTML>
    >> """
    >>
    >> I actually think that the last piece of code is more readable. Is
    >> there a way to stop that behaviour or is there a kind of trim
    >> function i can apply to get rid of the 2 extra lines?

    >
    > If you can live with the newline at the end of the text (which in
    > most cases is what you want anyway), this is the cleanest way to do
    > it:
    >
    > end_html = """\
    > </BODY>
    > </HTML>
    > """
    >
    > Or, you can get rid of both newlines this way:
    >
    > end_html = """
    > </BODY>
    > </HTML>
    > """[1:-1]
    >
    > -Mike


    Thanks for the sollution!
    flupke, Jun 5, 2004
    #5
  6. flupke

    flupke Guest

    Peter Otten wrote:
    > flupke wrote:
    >
    >> 2) Consider following heredoc
    >>
    >> end_html="""</BODY>
    >> </HTML>"""
    >>
    >> I have to write it like this to avoid having an empty line added
    >> above AND below the actual string when using it.

    >
    > Why would you care about whitespace in HTML?


    For the same reason i care about nicely spaced code. To make
    it readable. I do not use any HTML editor so i want the html code
    to be clean and easily readable.

    >>>> print """\

    > ... here's how \
    > ... to escape \
    > ... newlines"""
    > here's how to escape newlines
    >>>>

    >
    > Peter


    Thanks
    flupke, Jun 5, 2004
    #6
  7. > Michael Geary wrote:
    > > If you can live with the newline at the end of the text
    > > (which in most cases is what you want anyway), this
    > > is the cleanest way to do it:
    > >
    > > end_html = """\
    > > </BODY>
    > > </HTML>
    > > """
    > >
    > > Or, you can get rid of both newlines this way:
    > >
    > > end_html = """
    > > </BODY>
    > > </HTML>
    > > """[1:-1]


    flupke wrote:
    > Thanks for the sollution!


    De nada.

    Now that I think of it, if you do want to get rid of both newlines, here are
    a couple of other ways to do it:

    end_html = """\
    </BODY>
    </HTML>\
    """

    That is a bit ugly, but this seems reasonable (and better than the [1:-1]
    slice trick):

    end_html = """\
    </BODY>
    </HTML>"""

    -Mike
    Michael Geary, Jun 5, 2004
    #7
  8. Michael Geary wrote:

    > Now that I think of it, if you do want to get rid of both newlines,
    > here are
    > a couple of other ways to do it:
    >
    > end_html = """\
    > </BODY>
    > </HTML>\
    > """
    >
    > That is a bit ugly, but this seems reasonable (and better than the [1:-1]
    > slice trick):
    >
    > end_html = """\
    > </BODY>
    > </HTML>"""
    >
    > -Mike
    >
    >

    Just for completeness, you can also use lstrip(), rstrip() and strip()
    string methods to remove whitespace at the beggining, end or both parts
    of a string, respectively. I.e.:

    end_html = """
    </BODY>
    </HTML>
    """.lstrip()

    will get rid of the beginning newline. Using strip(), will get rid of
    both.

    I still prefer Mike's backslash solution, as getting rid of the newline
    is done at parse time rather than at run time. Also lstrip() will eat
    too much space if the first line of the string ("</BODY") is indented
    (and you want to preserve that indentation).

    - Xavier
    =?ISO-8859-1?Q?Xavier_Mart=EDnez?=, Jun 7, 2004
    #8
    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. Paul Vudmaska

    lil problem w/heredoc

    Paul Vudmaska, Apr 7, 2004, in forum: Ruby
    Replies:
    2
    Views:
    126
    Paul Vudmaska
    Apr 7, 2004
  2. David Tran
    Replies:
    6
    Views:
    738
    Eric Hodel
    Mar 17, 2005
  3. Replies:
    3
    Views:
    152
    Wolfgang NĂ¡dasi-Donner
    Jan 27, 2007
  4. ashishwave
    Replies:
    3
    Views:
    135
    ashishwave
    Aug 17, 2007
  5. phillyfan

    Firefox and heredoc problem

    phillyfan, Feb 1, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    168
    Tintin
    Feb 2, 2006
Loading...

Share This Page