Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83754
| References | <mailman.17684.1421168544.18130.python-list@python.org> <54b592ac$0$12995$c3e8da3$5496439d@news.astraweb.com> <CABUUr0yYBM=EJpcZXX2mJ0pz13Tum8px3Z8y3EZcr0VEo6Bfpw@mail.gmail.com> <CAPTjJmoTyMuLaY-z06Bfiw-SSratCkRMktQ1mQ2UhG=7jm6vZg@mail.gmail.com> <CABUUr0wj5-WKmwFb7RgoFzLOtMjEgQqm2nQDPtjLJGPhXiVPvQ@mail.gmail.com> |
|---|---|
| Date | 2015-01-15 00:43 +1100 |
| Subject | Re: Performance in exec environnements |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17720.1421243030.18130.python-list@python.org> (permalink) |
On Thu, Jan 15, 2015 at 12:38 AM, Jean-Baptiste Braun
<jbaptiste.braun@gmail.com> wrote:
> 2015-01-14 12:14 GMT+01:00 Chris Angelico <rosuav@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Performance in exec environnements Jean-Baptiste Braun <jbaptiste.braun@gmail.com> - 2015-01-13 18:02 +0100
Re: Performance in exec environnements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-14 08:48 +1100
Re: Performance in exec environnements Jean-Baptiste Braun <jbaptiste.braun@gmail.com> - 2015-01-14 10:02 +0100
Re: Performance in exec environnements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-15 12:51 +1100
Re: Performance in exec environnements Chris Angelico <rosuav@gmail.com> - 2015-01-14 22:14 +1100
Re: Performance in exec environnements Jean-Baptiste Braun <jbaptiste.braun@gmail.com> - 2015-01-14 14:38 +0100
Re: Performance in exec environnements Chris Angelico <rosuav@gmail.com> - 2015-01-15 00:43 +1100
Re: Performance in exec environnements Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-14 10:11 -0700
csiph-web