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

2014-02-07 Thread cstrutton11
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 = '"

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

2014-02-08 Thread cstrutton11
On Saturday, February 8, 2014 3:35:34 AM UTC-5, Rustom Mody wrote:
> On Saturday, February 8, 2014 1:11:53 PM UTC+5:30, [email protected] 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.
> 
> 
> 
> For creating html the method of choice is a template engine -- cheetah, mako 
> 
> and a dozen others
> 
> 
> 
> You can of course roll your own (poor mans version) template engine
> 
> For that look up 
> 
> 1. triple quoted strings
> 
> 2. format operator

I am using this with a template engine.  This method is going into a pyramid 
view class which will be rendered with chameleon or any other template engine.  
Actually it is going into a base class to be inherited into a view class.  The 
idea is to setup all the parameters for the HTML when the view class is created 
and reuse all that info for the initial page as well as all the ajax calls.

I didn't realize I could use formatting with triple quoted strings.  I will 
look into that.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-02-08 Thread cstrutton11
On Saturday, February 8, 2014 3:13:54 AM UTC-5, Asaf Las wrote:
 
> 
> 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. 
> 

I get what your saying here about immutable strings.  Is there anyway 
efficiently build large strings with lots of conditional inclusions, repetative 
sections built dynamically by looping (see the field section above)etc.  Is 
there a mutable string class?
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-02-08 Thread cstrutton11
On Saturday, February 8, 2014 3:13:54 AM UTC-5, Asaf Las wrote:

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

I think I going to rewrite this to build up a list of strings and then run a 
join on them at the end.  Each section can be conditionally built up with 
variable insertions as required.  This should be more efficient and will scale 
nicely as required.
-- 
https://mail.python.org/mailman/listinfo/python-list