How to load new class definitions at runtime?

Robert Brewer fumanchu at amor.org
Thu Nov 11 16:17:28 EST 2004


> I'm looking for ways to load new class definitions at runtime (I'm not
> talking about object instances here, so a persistent object database
> isn't what I am looking for [1]). I'm aware of a few options, but I
> would like to discuss the possible side effects for a potentially big,
> long running application server.
> 
> I am developing a business application based on workflow concepts. The
> model is quite simple: a business process is modelled as a sequence of
> tasks connected by actions. Actions can be re-used in different
> contexts. The result is a very complex graph. Processes, tasks and
> actions are implemented as classes. I opted to declare each action as
> a different class, because each action has different Python code
> attached to it. So simple object instances would not cut it. To allow
> the system to be customized without taking the application server
> offline, there must be a solution for these new class definitions to
> be loaded at runtime.
> 
> My current solution is simple. Class definitions have to be executed,
> and for it to work, the class name and the source code (or the
> compiled bytecode) is stored in a database. A class factory function
> retrieves the correct class definition (based on the name), and simply
> executes the code.

If you have so many such classes that you _must_ put them in a database
(and give up on existing version control systems like CVS), I don't have
any recommendations. But if you have several which are fairly static and
could survive in normal Python modules, feel free to steal my xray.py
from http://www.aminus.org/rbre/python/xray.py and use it to dynamically
load classes from Python modules as needed.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list