[Tutor] Modifying Source Code while Program is Running

Ed Singleton singletoned at gmail.com
Sat Nov 26 23:04:22 CET 2005

On 26/11/05, Kent Johnson <kent37 at tds.net> wrote:
> 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/

>From this and what Alan said, I think I'm starting to get it.  Each
attribute could be an object or an entry in a list.  Each type would
be a collection or list of a particular set of attributes.  The
collection of types would be an object which is easily persisted.

I could pass the type of the page as a parameter when I create it, and
could have a method in the class that looks up the particular type and
adds all the relevant attributes to the object with sensible default
values.  Adding new pagetypes would be easy enough cause they're just
an entry in the types list.

Can you create an object whose default action is to return a certain
value?  For example could I have a header object that where when you
call page.header it returns a value but I can also have
page.header.update().  Or (I think) I know that methods can have
attributes, but can attributes have methods?

I need a bit more time to sit down and think this through (I've gained
a lot of information and at least a little enlightenment over the last
few days), but I think this is really starting to come together.


More information about the Tutor mailing list