Performance in exec environnements

Chris Angelico rosuav at gmail.com
Wed Jan 14 14:43:48 CET 2015


On Thu, Jan 15, 2015 at 12:38 AM, Jean-Baptiste Braun
<jbaptiste.braun at gmail.com> wrote:
> 2015-01-14 12:14 GMT+01:00 Chris Angelico <rosuav at gmail.com>:
>>
>> Would it be possible to do a one-off transformation of the entire XSLT
>> file into a Python module with a single function in it, and then every
>> time you need that XSLT, you import that module and call the function?
>> That would potentially be a lot quicker than exec(), *and* would be
>> much clearer - there'd be an actual file on the disk with your
>> generated Python code, and anyone could read it and understand what
>> it's doing.
>
> I've done some tests executing compiled generated python and it doesn't seem
> to be worth over processing xslt directly. What you propose could be a
> solution. In fact I'm not sure yet how it will be designed.
>
> Thank you for feedback anyway.

I don't know how often you need to re-process the same XSLT file, but
if you often run the same file (eg with different input data), then
you could create a .py file and import it, and then call it. Something
like this:

# Python code generated from some_file.xslt - DO NOT MODIFY
def process(gender):
    print('<title>')
    if gender == 'M':
        print('Mr')
    else:
        print('Mrs')
    print('</title>')


And then you'd use it thus:

import some_file
some_file.process(gender='M')

Although rather than using print(), it might be better to use yield:

# Python code generated from some_file.xslt - DO NOT MODIFY
def process(gender):
    yield '<title>'
    if gender == 'M':
        yield 'Mr'
    else:
        yield 'Mrs'
    yield'</title>'

And then usage would be:

import some_file
print("\n".join(some_file.process(gender='M')))

which gives you the flexibility of sending it somewhere other than
stdout, without monkey-patching the print function to do something
else.

ChrisA



More information about the Python-list mailing list