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


Groups > comp.lang.python > #65653

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

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'output': 0.05; 'true,': 0.05; '"""': 0.07; 'none:': 0.07; 'variables': 0.07; 'false,': 0.09; 'inserted': 0.09; 'insertion': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:$ 30': 0.09; 'subject:build': 0.09; 'python': 0.11; 'def': 0.12; 'template': 0.14; "'email": 0.16; 'conditional': 0.16; 'create:': 0.16; 'fields:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sorting': 0.16; 'subject:most': 0.16; 'true:': 0.16; 'student': 0.16; 'language': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'advance.': 0.19; "skip:' 30": 0.19; 'thoughts': 0.19; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'jquery': 0.24; 'looks': 0.24; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'chris': 0.29; 'list:': 0.30; 'code': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'class': 0.32; 'another': 0.32; 'subject:the': 0.34; 'but': 0.35; 'false': 0.36; 'yield': 0.36; 'method': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'searching': 0.37; 'skip:o 20': 0.38; 'sometimes': 0.38; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'realize': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'name:': 0.61; 'email addr:gmail.com': 0.63; 'default': 0.69; 'ideas.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: What is the most pythonic way to build up large strings?
Date Sat, 08 Feb 2014 09:51:19 +0100
Organization None
References <fd73769f-b291-4543-82a3-872451f67342@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9cc2.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6529.1391849487.18130.python-list@python.org> (permalink)
Lines 96
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391849487 news.xs4all.nl 2860 [2001:888:2000:d::a6]:50780
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65653

Show key headers only | View raw


cstrutton11@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" '

Try to call the function with jquery=False ;)

>         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.

I sometimes use a variation of the above

def output_header(...):
    if jquery:
        yield """src=..."""
    if theme is not None:
        yield """<link href..."""

and then use it

sys.stdout.writelines(output_header(...))

but if you are doing this a lot you should pick one of the many template 
languages and use that to build your html

> I have another method that will build some javascript that looks like
> this:
> 
> $('#StudentTableContainer').jtable({
>     title: 'The Student List',
>     paging: true, //Enable paging
>     pageSize: 10, //Set page size (default: 10)
>     sorting: true, //Enable sorting
>     defaultSorting: 'Name ASC', //Set default sorting
>     actions: {
>         listAction: '/Demo/StudentList',
>         deleteAction: '/Demo/DeleteStudent',
>         updateAction: '/Demo/UpdateStudent',
>         createAction: '/Demo/CreateStudent'
>     },
>     fields: {
>         StudentId: {
>             key: true,
>             create: false,
>             edit: false,
>             list: false
>         },
>         Name: {
>             title: 'Name',
>             width: '23%'
>         },
>         EmailAddress: {
>             title: 'Email address',
>             list: false
>         },
>         ...
> 
> Almost every line in this code will require variable insertion or if
> statements.
> 
> Any thoughts on how to improve this?  Thanks in advance.  Chris

Again, you can build this with Python proper

"... title: {title} ...".format(title="The Student List", ...)

but a template language will make sure that your data is escaped properly, 
think

"... title: {title} ...".format(title="The Student's List", ...)

Back to comp.lang.python | Previous | NextPrevious 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