Saturday, 24 August 2013

Group of related functions that require the same expensive setup

Group of related functions that require the same expensive setup

I currently am porting some code from c++ to python that requires loading
a very big file into memory and doing some expensive setup operations
before it can be used. The code makes use of a large lookup table to speed
up the execution time for a group of time-critical related functions (over
5 functions use this same table extensively). I think this situation is
somewhat nicely dealt with in the unittests for the code:
class LookupTableTests(object):
@classmethod
def setup_class(cls):
"""Load the lookup table instance used by the rest of the tests.
"""
from fasteval import LookupTable
cls.lt = LookupTable.loadfile()
@classmethod
def teardown_class(cls):
"""Free the lookup table instance used in these tests.
"""
cls.lt.close()
cls.lt = None
In the context of unit testing this makes a lot of sense to me, we set up
the table once then run a bunch of tests and overall this saves a bunch of
execution time (loading this table and doing the other initialization is
slow).
However, outside of the unit tests I'm not sure of the best approach to
make sure that the setup and cleanup is only performed once.
The best idea I've had so far is to define a global that indicates if the
setup has been performed or not. But this seems ugly to me and also raises
the issue of cleanup not being properly dealt with if an unhandled
exception is thrown somewhere. If I was doing this in c++ I think I would
be trying to use some RAII principles and having the cleanup occur in a
destructor to help me manage the resources but I don't know the proper way
to do this in python.
What is the pythonic way to deal with a situation where you have a group
of related functions that all require the same setup and cleanup? What
sort of structure will this solution have?

No comments:

Post a Comment