Thnx a lot Michael!

For interested:


import new

class MethodDescriptor(object):
    def __init__(self, func):
        self.func = func
    def __get__(self, instance, owner):
        if instance is None:
            return new.instancemethod(self.func, owner,
owner.__class__)
        else:
            return new.instancemethod(self.func, instance, owner)

class PropertyDescriptor(object):
    def __init__(self, fget, fset, fdel):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
    def __get__(self, instance, owner):
        if instance is None:
            return self.fget(owner)
        else:
            return self.fget(instance)
    def __set__(self, instance, value):
        self.fset(instance, value)
    def __delete__(self, instance):
        self.fdel(instance)

def hybrid(func):
    return MethodDescriptor(func)

def hybrid_property(fget, fset=None, fdel=None):
    return PropertyDescriptor(fget, fset, fdel)



class AreaProxy(object):
    def __init__(self,s):
        self.s=s
    def __getattr__(self,key):
        return getattr(self.s,"area_"+key)




class Warehouse(object):
    def __init__(self,id,construction_id,area_total,area_storage):
        self.id = id
        self.construction_id = construction_id
        self.area_total = area_total
        self.area_storage = area_storage

    def __repr__(self):
        return "%s" % self.id

    area = hybrid_property(lambda s: AreaProxy(s))

this works with:

filter()
and returns the value as well

On 13 Paź, 23:04, Michael Bayer <[EMAIL PROTECTED]> wrote:
> OK, take a look at an example we have in examples/derived_attributes/
> attributes.py.  This is doing a "dual use" descriptor which works at  
> the class level and instance level.  Its more complicated but with  
> some tweaking can probably work like the example you're giving.
>
> On Oct 13, 2008, at 4:14 PM, g00fy wrote:
>
>
>
> > I did:
>
> >    class AreaProxy(object):
> >        def __init__(self,parent):
> >            self.parent=parent
> >        def __getattr__(self, key):
> >            return self.parent.area_total
> >            return getattr(self.parent, "area_" + key)
> >   [EMAIL PROTECTED]
> >    def area(self):
> >        return Warehouse.AreaProxy(self)
>
> > but when filtering :
>
> > filter(Warehouse.area.total>=100)
> > i get
> > AttributeError: 'property' object has no attribute 'total'
>
> > On 13 Paź, 22:00, g00fy <[EMAIL PROTECTED]> wrote:
> >> I am talking about both.
>
> >> I said it right,
> >> It wasn't working becouse it returned a string column name, and  
> >> should
> >> return a value on instance.
> >> And i want to be able to filter by this property also.
>
> >> On 13 Paź, 21:56, Michael Bayer <[EMAIL PROTECTED]> wrote:
>
> >>> On Oct 13, 2008, at 3:40 PM, g00fy wrote:
>
> >>>> I want to:
> >>>> Warehouse.area.total
> >>>> to return
> >>>> Warehouse.area_total
>
> >>>> and so on :
> >>>> Warehouse.area.storage
> >>>> returns:
> >>>> Warehouse.area_storage
>
> >>> then why did you say:
>
> >>>> And this isn't working becouse:
> >>>> Warehouse.area.total
> >>>> returns
> >>>> .area_total
>
> >>> are you talking about class-level SQL predicates, or instance-level
> >>> attribute values ?   composite will give you the latter, the  
> >>> recipe I
> >>> gave will give you the former.    The recipe can be adjusted to
> >>> produce both but its more complicated.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to