[Tutor] Modifying Source Code while Program is Running

Kent Johnson kent37 at tds.net
Sat Nov 26 13:25:04 CET 2005


Ed Singleton wrote:
>>>This immediately seemed to me to be a case for classes.
>>>You provide a way for a user to create a new class by
>>>subclassing the page class (from their point of view
>>>probably through adding a few new fields to
>>>a form).
> 
> The behaviours of all the
> classes would be the same (and would be fairly simple, a method for
> rendering the page with a template, a method to update the data based
> on a form submission).  Users would basically be able to change the
> data structure (add and remove attributes).

It seems to me that rather than having a new class for each type of data, you need a class whose attributes are dynamic. I imagine you could have a list of page types, each of which has a list of attributes. A new page type is created not by defining a new class but by making a new entry in the types table. The data for a page might be as simple as just a dict, or maybe it makes sense to wrap it in a class, but the class would be the same for all pages. A page would be rendered by combining a template with the dict containing its data. Form submissions would be handled by referring to the list of attributes for the type and extracting the corresponding data.

You might have a class to wrap the page data and another one to wrap the types table. These would provide operations on the contained data and be common to all types.

For persistence you could take a couple of approaches. You could persist the lists and dicts directly using pickle or shelve or an object database. You could persist them in a relational database by using a generic table with one row for each data item - the columns would simply be object id, column name and data. SQLite might work very well for this as it allows multiple data types within a single column.

The shift is from looking at the problem as one of dynamic classes, to one of dynamic data. Python is excellent for working with dynamic data!

BTW have you looked at Django? I don't think it is quite as dynamic as you want on the model side but it has a lot of support for quickly generating a presentation of a changing model.
http://www.djangoproject.com/

Kent



More information about the Tutor mailing list