Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65641

Re: What is the most pythonic way to build up large strings?

Newsgroups comp.lang.python
Date 2014-02-08 00:13 -0800
References <fd73769f-b291-4543-82a3-872451f67342@googlegroups.com>
Message-ID <e41758ef-bc50-48b9-bd39-3899c0c3e643@googlegroups.com> (permalink)
Subject Re: What is the most pythonic way to build up large strings?
From Asaf Las <roegltd@gmail.com>

Show all headers | View raw


On Saturday, February 8, 2014 9:41:53 AM UTC+2, cstru...@gmail.com wrote:
> I am writing a couple of class methods to build up several 
> lines of html.  Some of the lines are conditional and most need 
> variables inserted in them.  Searching the web has given me a 
> few ideas.  Each has its pro's and cons.
> 
> The best I have come up with is:
> 
> def output_header_js(self, jquery=True, theme=None):
>     if self.static_path is None :
>         return None
> 
>     if jquery is True:
>         output = '"<script type="text/javascript" '
>         output += 'src="/%s/jquery/jqueryui.js"></script>'% static
>         output += '"<script type="text/javascript" '
>         output += 'src="/%s/jquery/jquery.js"></script>'% static
> 
>     if theme is not None:
>         output += '<link href="/%s/jtable/themes/%s/jtable.css" '% static, theme
> 
>         output += 'rel="stylesheet" type="text/css" />'
> 
>     output += '"<script type="text/javascript" '
>     output += 'src="/%s/jtable/jquery.jtable.js"></script>' % "static"
> 
> I realize that a lot of the above looks repetitive but it is 
> designed to eliminate boilerplate HTML.
> 

note, due to strings are immutable - for every line in sum operation 
above you produce new object and throw out older one. you can write 
one string spanned at multiple lines in very clear form. 


/Asaf

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What is the most pythonic way to build up large strings? cstrutton11@gmail.com - 2014-02-07 23:41 -0800
  Re: What is the most pythonic way to build up large strings? Asaf Las <roegltd@gmail.com> - 2014-02-08 00:13 -0800
    Re: What is the most pythonic way to build up large strings? cstrutton11@gmail.com - 2014-02-08 01:56 -0800
      Re: What is the most pythonic way to build up large strings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-08 11:06 +0000
      Re: What is the most pythonic way to build up large strings? Asaf Las <roegltd@gmail.com> - 2014-02-08 03:28 -0800
        Re: What is the most pythonic way to build up large strings? Rustom Mody <rustompmody@gmail.com> - 2014-02-08 04:33 -0800
    Re: What is the most pythonic way to build up large strings? cstrutton11@gmail.com - 2014-02-08 02:11 -0800
      Re: What is the most pythonic way to build up large strings? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-08 13:34 +0000
  Re: What is the most pythonic way to build up large strings? Rustom Mody <rustompmody@gmail.com> - 2014-02-08 00:35 -0800
    Re: What is the most pythonic way to build up large strings? cstrutton11@gmail.com - 2014-02-08 01:51 -0800
      Re: What is the most pythonic way to build up large strings? Dave Angel <davea@davea.name> - 2014-02-08 09:25 -0500
    Re: What is the most pythonic way to build up large strings? Roy Smith <roy@panix.com> - 2014-02-08 07:15 -0500
    Re: What is the most pythonic way to build up large strings? "Eric S. Johansson" <esj@harvee.org> - 2014-02-08 09:42 -0500
  Re: What is the most pythonic way to build up large strings? Peter Otten <__peter__@web.de> - 2014-02-08 09:51 +0100

csiph-web