Python abandons text.replace () in one environment

I mocked the next port of dirty support code for the pylons application, which works fine in the python shell, a separate python file, or when run in paster. Now we have placed the application online through mod_wsgi and apache, and this particular piece of code has completely stopped working. Firstly, the code itself:

def fixStyle(self, text):
    t = text.replace('<p>', '<p style="%s">' % (STYLEDEF,))
    t = t.replace('class="wide"', 'style="width: 125px; %s"' % (STYLEDEF,))
    t = t.replace('<td>', '<td style="%s">' % (STYLEDEF,))
    t = t.replace('<a ', '<a style="%s" ' % (LINKSTYLE,))
    return t

It seems pretty simple, and frankly it is. So what happens when I put a piece of text in it, for example:

<table><tr><td>Test!</td></tr></table>

The output should be:

<table><tr><td style="stuff-from-styledef">Test!</td></tr></table>

and this is on most systems. When we add it through an application on Apache / mod_wsgi, the following happens:

<table><tr><td>Test!</td></tr></table>

You guessed it.

, t. , : t , Apache .

, apache ( .py ) , .

, . Googling , , , , , , , .

- , .

+3
3

Apache:

def fixStyle(self, text):
    print "text:", text
    print "STYLEDEF", STYLEDEF
    t = text.replace('<p>', '<p style="%s">' % (STYLEDEF,))
    print "t:", t
0

, , , replace() : , 4 .

IMO, :

def fixStyle(self, text):
    t = text.replace('<p>', '<p style="%s">' % (STYLEDEF,))
    t = t.replace('class="wide"', 'style="width: 125px; %s"' % (STYLEDEF,))
    t = t.replace('<td>', '<td style="%s">' % STYLEDEF)
    t = t.replace('<a ', '<a style="%s" ' % (LINKSTYLE,))
    return t

import re

STYLEDEF = 'stuff-from-styledef'
LINKSTYLE = 'VVVV'

def aux(m, dic = {'<p':('<p style="',STYLEDEF),
                  '<td':('<td style="',STYLEDEF),
                  'class="wide"':('style="width: 125px; ',STYLEDEF),
                  '<a':('<a style="',LINKSTYLE)} ):

    return '%s%s"' % dic[m.group()]

pat = re.compile('<p(?=>)>|class="wide"|<td(?=>)|<a(?= )')

ch = '<table><tr><td>Test!</td></tr></table><a type="brown" >'
print ch
print fixStyle(None, ch)
print pat.sub(aux,ch)

<table><tr><td>Test!</td></tr></table><a type="brown" >
<table><tr><td style="stuff-from-styledef">Test!</td></tr></table><a style="VVVV" type="brown" >
<table><tr><td style="stuff-from-styledef">Test!</td></tr></table><a style="VVVV" type="brown" >

, re.sub() .

dic = > dic aux(), doesn ' . dic : .

, aux() STYLEDEF LINKSTYLE .

, .

.

EDIT: ' style="' STYLEDEF , , ,

def aux(m, dic = {'<p'          :'<p style="%s"',
                  '<td'         :'<td style="%s"',
                  'class="wide"':'style="width: 125px; %s"'} ):
    if m.group(1):
        return '<a style="%s"' % LINKSTYLE
    else:
        return dic[m.group()] % STYLEDEF

pat = re.compile('<p(?=>)|class="wide"|<td(?=>)|(<a)(?= )')

, , , . , , .

:

def aux(m, STY = STYLEDEF,LIN = LINKSTYLE ):
    return ( 'style="width: 125px; ' if m.group(3) else m.group(1)+' style="' ) + \
           ( LIN if m.group(2) else STY) + '"'   

pat = re.compile('(<p(?=>)|<td(?=>)|(<a(?= )))|(class="wide")')

, :

def aux(m, dic = {'<p' :'<p style="%s"' % STYLEDEF,
                  '<td':'<td style="%s"' % STYLEDEF,
                  '<a' :'<a style="%s"' % LINKSTYLE,
                  'class="wide"':'style="%s"' % ('width: 125px; '+STYLEDEF) } ):
    return dic[m.group()] 

pat = re.compile('<p(?=>)|class="wide"|<td(?=>)|<a(?= )')

dic aux().

replace().

0

, : , repr(), . , repr(text) repr(t), NOT text t.

Run a non-working environment and at least one working environment on the same piece of data and edit your question to show the actual code that you used and the actual log output.

0
source

Source: https://habr.com/ru/post/1796324/


All Articles