Malcolm Tredinnick <[EMAIL PROTECTED]>
writes:

> On Sat, 2007-02-10 at 08:30 +0800, Russell Keith-Magee wrote:
>> On 2/10/07, David Abrahams <[EMAIL PROTECTED]> wrote:
>> >
>> >    Use natural operators for filtering, excluding, and attribute
>> >    access.
>> 
>> > How about it?
>> 
>> Some immediate problems:
>> 
>> - Your syntax works for < > <= >= and ==; but what about all the other
>> Django query operators? How do you come up with a clean representation
>> for startswith? istartswith? range? month?
>
> Maybe I'm missing something, but I don't see how this can work at all.
> When Python sees "_.foo >= 6", it is going to evaluate it at runtime
> (before calling the function). 

Yes.  _.foo >= 6 returns an expression tree, an object that represents
the comparison of the foo field with 6.  This technique is well-known
among advanced C++ programmers but has also been seen in the Python
world (http://thread.gmane.org/gmane.comp.python.numeric.general/4081,
although I've heard of other instances).  Here's enough of a sketch to
represent the above expression.  Obviously you'd need more along the
same lines to be able to handle everything:

  # represents unary or binary operations
  class Node(object):
        def __init__(self, op, lhs, rhs=None):
            self.op = op  # Label used to identify the operation
            self.lhs = lhs
            self.rhs = rhs

        def __getattr__(self, name):
            return Node('attr',self,name)

        def __ge__(self, other):
            return Node('>=',self,other)

  field = Node(None,None) # the leaf node

now with _=field,

  _.foo >= 6 

is the same as 

  Node('>=', Node('attr', Node(None,None), 'foo'), 6)

which stores all the information needed to construct the correct SQL.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to