How to load new class definitions at runtime?

Carlos Ribeiro carribeiro at gmail.com
Thu Nov 11 16:06:50 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.

Now, talking about executing arbitrary code retrieved from a database
isn't exactly a safe proposition. But I can't see any other way to
make it work. I'm now looking for more ideas on how do other people
solved the same problem in other projects. Pointers are welcome.
Thanks in advance,

---
[1] ... or at least I assume so.
-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list