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?

From Peter Otten <__peter__@web.de>
Subject Re: What is the most pythonic way to build up large strings?
Date 2014-02-08 09:51 +0100
Organization None
References <fd73769f-b291-4543-82a3-872451f67342@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6529.1391849487.18130.python-list@python.org> (permalink)

Show all headers | 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