[Tutor] To write data in two different fonts?

Nick Raptis airscorp at otenet.gr
Wed Aug 12 23:20:58 CEST 2009


Dave Angel wrote: 
> As I said, you'd probably get in trouble if any of the lines had '&' 
> or '<' characters in them.  The following function from the standard 
> library can be used to escape the line directly, or of course you 
> could use the function Nick supplied.
>
> xml.sax.saxutils.escape(/data/[, /entities/])
>
>    Escape '&', '<', and '>' in a string of data.
>
>    You can escape other strings of data by passing a dictionary as the
>    optional /entities/ parameter. The keys and values must all be
>    strings; each key will be replaced with its corresponding value. The
>    characters '&', '<' and '>' are always escaped, even if /entities/
>    is provided.
>
> Let us know if that doesn't do the trick.
>
> DaveA
>
Thanks Dave for the info on xml.sax.saxutils.escape
Didn't know about this one.

For the rest:
It is sometimes
This is the source code of the xml.sax.saxutils.escape function:

---------------------------------------
def __dict_replace(s, d):
    """Replace substrings of a string using a dictionary."""
    for key, value in d.items():
        s = s.replace(key, value)
    return s

def escape(data, entities={}):
    """Escape &, <, and > in a string of data.

    You can escape other strings of data by passing a dictionary as
    the optional entities parameter.  The keys and values must all be
    strings; each key will be replaced with its corresponding value.
    """

    # must do ampersand first
    data = data.replace("&", "&amp;")
    data = data.replace(">", "&gt;")
    data = data.replace("<", "&lt;")
    if entities:
        data = __dict_replace(data, entities)
    return data
-----------------------------------------

As you can see, it too uses string.replace to do the job.
However, using a built-in function that works for what you want to do is 
preferable.
It's tested and might also be optimized to be faster.
It's easy and fun to look into the source though and know exactly what 
something does.
It's also one of the ways for a begginer (me too) to progress.

 From the source code I can see this for example:
*Don' t pass the entity dictionary I proposed earlier to this function:*
entities = {'&' : '&amp;',
           '<' : '&lt;',
           '>' : '&gt;',
           '"' : '&quot;',
           "'" : '&apos;'}
If you pass an entity for '&' into escape(), it will escape it in the 
already partially escaped string, resulting in chaos.

Think of it, this function not checking for a '&' entity passed to it 
might worth qualifying as a bug :)

Nick


More information about the Tutor mailing list