[Tutor] about a function

Jeff Shannon jeff@ccvcorp.com
Tue Mar 25 19:06:50 2003


Abdirizak abdi wrote:

> hi everyone,
>
> I am having a proble with a function that takes a string from a list 
> and searches another string and then process and returns a reformatted 
> sring of this form
>
> <EQN/> <W>of</W> <W>a</W> <W>conditioned</W> <W>word</W> <EQN/>
>

For this specific case, using RE is unnecessarily complex.  For the 
general case, you need an XML parser rather than a RE.  Here's how I'd 
solve your problem:

----test.py-----------
def EQNfunc(text):
    tag1 = "<EQN/>"
    tag2 = "<W>"
    tag3 = "</W>"

    if text.startswith(tag1) and text.endswith(tag1):
        cut = len(tag1)
        text = text[cut:-(cut)]   # strip off tags
        result = ["%s%s%s" % (tag2, item, tag3) for item in text.split()]
        answer = " ".join(result)
        return "%s%s%s" % (tag1, answer, tag1)
    else:
        raise ValueError, "Invalid text: '%s' tags not present" % tag1
------------------------

 >>> import test
 >>> text = "<EQN/>some text in here<EQN/>"
 >>> print test.EQNfunc(text)
<EQN/><W>some</W> <W>text</W> <W>in</W> <W>here</W><EQN/>
 >>> text2 = "Something without <EQN/> tags"
 >>> print test.EQNfunc(text2)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "test.py", line 12, in EQNfunc
    raise ValueError, "Invalid text: '%s' tags not present" % tag1
ValueError: Invalid text: '<EQN/>' tags not present
 >>>

You can easily generalize this a little bit by making the specific tags 
into parameters, or draw them from some global dictionary, or whatever. 
 But this seems to fulfill all of your specifications without resorting 
to regular expressions, which are both more powerful than you need and 
less of a cure-all than you think.

Jeff Shannon
Technician/Programmer
Credit International