code is data

Kay Schluehr kay.schluehr at gmx.net
Tue Jun 20 01:00:08 EDT 2006


Ian Bicking wrote:

> > I don't use Django and I made this up quickly, so please don't pick on
> > subtleties.
> >
> > @Poll:
> >     question: char length 200
> >     pub_date('date published'): date
> >
> > @Choice:
> >     poll -> Poll
> >     choice: char length 200
> >     votes: int
>
> That doesn't look that much better.  How do you create it
> programmatically?  I know how to pass a variable to
> CharField(maxlength=200); can I pass a variable to "char length 200"
> just as easily?  Can I use **kw?  Can I read it from a CSV file and
> construct the class that way?  Maybe, but only by recreating all the
> native patterns that I can infer easily looking at the Django class.

If it is just a different kind of representation of common data
structures as in YAML the answer might be a translation of these
declarative blocks into dicts/lists ( or derivatives of those ) at
compile time. The underlying semantics would be that of an "implicitely
embedded DSL" ( there are quite a lot in Python ). Enabling code
generation would just make them more explicit. For example XML syntax
could be considered as an alternate surface syntax for elementrees. XML
elements in Python code might be translated to aequivalent elementree
annotation syntax at compile time.

Under this considerations "choice: char length 200" and
"CharField(maxlength = 200 )" are essentially the same thing. I guess
@Choice.choice would finally represented by a property.

Regards,
Kay




More information about the Python-list mailing list