Google Groups Home
Help | Sign in
Message from discussion Preserving newlines
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Cecil Westerhof  
View profile  
 More options May 13 2008, 5:35 am
From: "Cecil Westerhof" <cldwester...@gmail.com>
Date: Tue, 13 May 2008 11:35:12 +0200
Local: Tues, May 13 2008 5:35 am
Subject: Re: [TurboGears] Re: Preserving newlines
2008/5/13 Daniel Fetchinson <fetchin...@googlemail.com>:

>  And if this part of your code becomes the real performance bottleneck
>  you might want to do the search/replace using the re module. Take a
>  look at xml.etree.ElementTree in the stdlib for an example (if you
>  have python > 2.4).

I do not think this will be the bottleneck. Displaying is offcourse
what is done most, but retreiving the values from the database is more
expensive as the replacements I think.
But it never hurts to think about performance I think. So I made it like:
#####
startSpaces = re.compile(' +')
def splitLines(thisText):
    if thisText == None:
        return None
    thisText = xml.etree.ElementTree._encode_entity(thisText)
    lines    = thisText.split('\n')
    for i in range(len(lines)):
        match = startSpaces.match(lines[i])
        if match:
            lines[i] = '&nbsp;' * match.end() + lines[i][match.end():]
    return '<br/>'.join(lines)
#####

With xml.etree.ElementTree._encode_entity the conversion is done more
efficiënt and at the same time there is more done.
I find the spaces at the start of a line significant. That is why I
added the for-loop. In this way the spaces at the start of a line are
replaced with &nbsp; and is the indent of a line saved.

--
Cecil Westerhof


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google