can't do __contains__ due to Python behavior:

class Foo(object):
    def __add__(self, other):
        return (self, "add", other)

    def __contains__(self, other):
        return (self, "contains", other)

f1 = Foo()

assert f1 + 5 == (f1, "add", 5)

assert 5 in f1 == (f1, "contains", 5), 5 in f1

second assertion fails, it forces a bool() on the result.   __nonzero__() is 
required to return True/False/int, I suppose we could make a custom int 
subclass but that's getting really weird.




On Sep 4, 2012, at 10:50 AM, Eric Lemoine wrote:

> On Tue, Sep 4, 2012 at 4:45 PM, Michael Bayer <mike...@zzzcomputing.com> 
> wrote:
>> they're in tip.   thanks for testing all this !
> 
> 
> Great!
> 
> 
> Another issue, with "contains" this time:
> 
> 
> 
> ----
> from sqlalchemy.types import UserDefinedType
> 
> 
> class Geometry(UserDefinedType):
> 
>    class comparator_factory(UserDefinedType.Comparator):
> 
>        def contains(self, other):
>            return self.op('~')(other)
> 
> 
> from sqlalchemy import Table, Column, MetaData
> 
> lakes = Table('lake', MetaData(),
>    Column('geom', Geometry)
>    )
> 
> print str(lakes.c.geom.contains('POINT(1 2)'))
> ----
> 
> 
> 
> produces this error:
> 
> 
> Traceback (most recent call last):
>  File "comparator_factory.py", line 18, in <module>
>    print str(lakes.c.geom.contains('POINT(1 2)'))
>  File "/home/elemoine/src/sqlalchemy/lib/sqlalchemy/sql/operators.py",
> line 375, in contains
>    return self.operate(contains_op, other, **kwargs)
>  File "/home/elemoine/src/sqlalchemy/lib/sqlalchemy/sql/expression.py",
> line 2204, in operate
>    return op(self.comparator, *other, **kwargs)
>  File "/home/elemoine/src/sqlalchemy/lib/sqlalchemy/sql/operators.py",
> line 571, in contains_op
>    return a.contains(b, escape=escape)
> TypeError: contains() got an unexpected keyword argument 'escape'
> 
> 
> I should be able to override "contains", no?
> 
> 
> 
> 
> 
> 
> -- 
> Eric Lemoine
> 
> Camptocamp France SAS
> Savoie Technolac, BP 352
> 73377 Le Bourget du Lac, Cedex
> 
> Tel : 00 33 4 79 44 44 96
> Mail : eric.lemo...@camptocamp.com
> http://www.camptocamp.com
> 
> -- 
> 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 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to