Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [OT] code is data

Reply
Thread Tools

[OT] code is data

 
 
Laurent Pointal
Guest
Posts: n/a
 
      06-19-2006
bruno at modulix a écrit :
> Anton Vredegoor wrote:
>> bruno at modulix wrote:
>>
>>> I still don't get the point.

>>
>> Well, I've got to be careful here, lest I'd be associated with the
>> terr.., eh, the childp..., eh the macro-enablers.
>>
>> The idea is to have a way to transform a Python (.py) module into XML
>> and then do source code manipulations in XML-space using ElementTree.

>
> My my my... I'm not against the idea of dynamic source code
> transformation, but for heaven's sake, *why* would one put XML in the
> mix ???????


Because its "à la mode", and is better for a commercial point of view,
even if inefficient for this problem.


 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      06-19-2006
Laurent Pointal wrote:

>>> The idea is to have a way to transform a Python (.py) module into XML
>>> and then do source code manipulations in XML-space using ElementTree.

>>
>> My my my... I'm not against the idea of dynamic source code
>> transformation, but for heaven's sake, *why* would one put XML in the
>> mix ???????


because lots of people know how to describe XML transformations, and
there are plenty of tools that implement such transformations efficiently ?

> Because its "à la mode", and is better for a commercial point of view,
> even if inefficient for this problem.


why would XML be inefficient ?

</F>

 
Reply With Quote
 
 
 
 
Kay Schluehr
Guest
Posts: n/a
 
      06-19-2006

Ravi Teja wrote:

> People have however written various language interpreters (Scheme,
> Forth and yes, even Basic) in Python, just for kicks. Still does not
> make it a DSL language anymore than it makes C a DSL language.
>
> At present, the closest thing to writing a DSL in Python is Logix
> http://livelogix.net/logix/
> Too bad though, the project is defunct and there has never been enough
> interest in it.


You might be interested in EasyExtend:

http://www.fiber-space.de/EasyExtend/doc/EE.html

Unlike Logix there are no macros defined in application code and there
are no runtime macro expansions. So extension language semantics is
always fixed at compile time.

> Personally, I would like to see macros in Python (actually Logix
> succeeding is good enough). But I am no language designer and the
> community has no interest in it. When I absolutely need macros, I will
> go elsewhere.


Although Logix was written in Python and compiled to CPython bytecodes
it was a language on its own right: a Python / Lisp hybrid. Opposed to
this EasyExtend is a Python framework for language extensions and not
itself a language. A quite typical use case may not involve any new
grammar rules or terminals, but just generates code. See the coverage
fiber as an example.

 
Reply With Quote
 
bruno at modulix
Guest
Posts: n/a
 
      06-19-2006
Fredrik Lundh wrote:
> Laurent Pointal wrote:


>> Bruno Desthuilliers wrote:
>>> Anton Vredegoor wrote:


>>>> The idea is to have a way to transform a Python (.py) module into XML
>>>> and then do source code manipulations in XML-space using ElementTree.

>
>>>

>
>>> My my my... I'm not against the idea of dynamic source code
>>> transformation, but for heaven's sake, *why* would one put XML in the
>>> mix ???????

>
>
> because lots of people know how to describe XML transformations, and
> there are plenty of tools that implement such transformations efficiently ?


Efficiently enough for dynamic (runtime) use ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Ravi Teja
Guest
Posts: n/a
 
      06-19-2006

BJörn Lindqvist wrote:
> > > > community has no interest in it. When I absolutely need macros, I will
> > > > go elsewhere.

> > I *like* 1..5 (ada, ruby) instead of range(5). If I had macros, I would
> > have done it myself for *my* code.


> I think this example more is a symptom of a childish need to get
> things your way than of a deficiency in Python.


I thought I had enough asterisks in there to indicate that it is a
preference that I will not be defending on rational grounds. I had a
better argument before it in the same post. But you had to choose only
the trivial one to dismiss me as childish. Didn't you?

> BTW, range(5) = 0..4 in Ada and Ruby.


My bad. I usually write range(1, 5 + 1) to get 1..5.
I could write range(1, 6). But I would like to see the upper bound
explicitly. Of course, I could write a function to wrap that up.

> You said "when I absolutely need macros" but none of your examples
> demonstrate any "absolute need." I can't see your point.


Did you miss the word - *WHEN*?
I don't need them absolutely now. And I know, that I won't get them
here. And just so you don't misinterpret, I don't call that a
"deficiency". Just a mismatch between the personal and the community
mindset.
BTW, the recent language changes - decorators, conditional expressions
and with statements are not absolute either. That did not stop them
from being welcome additions.

 
Reply With Quote
 
Ravi Teja
Guest
Posts: n/a
 
      06-19-2006
Kay Schluehr wrote:
> Ravi Teja wrote:
>
> > People have however written various language interpreters (Scheme,
> > Forth and yes, even Basic) in Python, just for kicks. Still does not
> > make it a DSL language anymore than it makes C a DSL language.
> >
> > At present, the closest thing to writing a DSL in Python is Logix
> > http://livelogix.net/logix/
> > Too bad though, the project is defunct and there has never been enough
> > interest in it.

>
> You might be interested in EasyExtend:
>
> http://www.fiber-space.de/EasyExtend/doc/EE.html


Your framework does look very interesting and might just be what I am
looking for. Will give it a try.

Thanks.

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      06-19-2006
>> because lots of people know how to describe XML transformations, and
>> there are plenty of tools that implement such transformations efficiently ?

>
> Efficiently enough for dynamic (runtime) use ?


Using XML-transformation for AST manipulation isn't my first choice
either - yet efficiency concerns aren't really the point here - after
all we're talking about generating code, which would be pretty useless
if the work was to be done by the transformation instead of that very
code generated ...

So the question is: do XML/XSL give an advantage here? As I said - I
personally don't think so, IMHO a standard reducer using a decent
visitor is easy enough and works well. But your (or better Fredrik's) MMV.

Diez
 
Reply With Quote
 
Ian Bicking
Guest
Posts: n/a
 
      06-20-2006
Ravi Teja wrote:
> Fredrik Lundh wrote:
> > Ravi Teja wrote:
> >
> > > Web frameworks, which seem to be the rage now in Python community could
> > > have benefited tremendously from Macro capabilities since they have a
> > > lot of boiler plate.

> >
> > they do? methinks you haven't done much web programming lately...
> >
> > </F>

>
> You blogged on Django. Let's use that. Don't you think model creation
> in Django can be represented better, given that it is done often
> enough?


Actually, no, it's not done that much. Creating and managing tables
isn't something done lightly. It's essential to building a new
application, but (at least in my experience, in similar systems) the
database models stabalize early and you don't spend that much time with
them. Especially not with the DSL aspects. I add and remove methods
often, but I am loathe to add and remove columns.

Now, this might seem like I'm being pedantic, but in my experience lots
of seemingly obvious DSLs end up not being that obvious. XML
generation, for instance. It's nice to have a good syntax -- and you
can get a pretty good syntax in Python (e.g., HTMLGen, stan, etc). But
efforts that go further are generally misplaced, because it's actually
not a very hard or common thing to do, even when you are slinging
around lots of XML.

Or... maybe to be more specific, the hard work later on goes into
*code*. If you are enhancing your model, you do so with methods on the
model classes, and those methods don't effect the DSL, they are just
"code". You create some raw XML in the beginning, but quickly it's
just a matter of gluing those pieces together, using functions instead
of DSLs, and that's just "code".

> Let's take an example from the official tutorial
> from
> http://www.djangoproject.com/documen...reating-models
>
> class Poll(models.Model):
> question = models.CharField(maxlength=200)
> pub_date = models.DateTimeField('date published')
>
> class Choice(models.Model):
> poll = models.ForeignKey(Poll)
> choice = models.CharField(maxlength=200)
> votes = models.IntegerField()
>
> 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.

> The following is my rationale. Annoted variables, symbols and code
> layout visually cue more efficiently to the object nature than do
> explicit text definitions. Of course, this is only sensible when there
> aren't too many of any of those. In that case, the cognitive cost of
> notation outweighs the representational cost of text.


Words are great. Python is light on symbols, and that is good. Python
is not perfect when it comes to expressing data structures (the more I
think about it, the more PEP 359 grows on me), but real DSLs are
questionable to me.

Even the Lisps stick to an incredibly homogenous syntax (far more
homogeneous than Python) to make macros feel familiar.

> Representational minimalism is troublesome in general code (ala Perl),
> but not so in a DSL where the context is constrained.


Constrained context is a step backward! How do you add methods? How
do you do looping? How do you write *code*? If you aren't going to
allow those things, then just make a parser and build the structure
from the file, and make it a DSL implemented entirely external to
Python. That's completely okay, though in my experience it's not very
satisfying for something like a model definition (see MiddleKit for an
example of an ORM that doesn't use Python code).

Ian

 
Reply With Quote
 
K.S.Sreeram
Guest
Posts: n/a
 
      06-20-2006
Fredrik Lundh wrote:
> because lots of people know how to describe XML transformations, and
> there are plenty of tools that implement such transformations efficiently ?
>
> why would XML be inefficient ?


XML Transformations (XSLT) would *certainly* be an overkill here.
They've invented a whole new declarative programming language, and we
certainly don't need that when we've got Python!

XML by itself feels completely out of place in this context. What we
need is, just a flexible, easy to manipulate, in-memory tree structure
(AST) for the Python source.

Regards
Sreeram


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEl2gfrgn0plK5qqURAkIXAKC62Jr35C0mVuyjYDBer3 Imwf6tFACeNFta
wb5h5K0wgcRRJ48znAoLNec=
=3OO3
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Kay Schluehr
Guest
Posts: n/a
 
      06-20-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How add data to Entity Data Model via code? Bobby Edward ASP .Net 0 09-23-2008 03:18 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi C Programming 0 12-29-2004 05:10 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi Cisco 0 12-29-2004 05:03 PM
Passing data/code from code-behind to aspx aratienza ASP .Net Web Controls 1 12-10-2004 04:55 PM
Passing code/data from code-behind to ASPX page A. Atienza ASP .Net Datagrid Control 1 12-08-2004 06:29 PM



Advertisments