class documentation

class Operators(object):

Known subclasses: sqlalchemy.sql.operators.ColumnOperators

View In Hierarchy

Base of comparison and logical operators.

Implements base methods ~sqlalchemy.sql.operators.Operators.operate and ~sqlalchemy.sql.operators.Operators.reverse_operate, as well as ~sqlalchemy.sql.operators.Operators.__and__, ~sqlalchemy.sql.operators.Operators.__or__, ~sqlalchemy.sql.operators.Operators.__invert__.

Usually is used via its most common subclass .ColumnOperators.

Method __and__ Implement the & operator.
Method __invert__ Implement the ~ operator.
Method __or__ Implement the | operator.
Method bool​_op Return a custom boolean operator.
Method op Produce a generic operator function.
Method operate Operate on an argument.
Method reverse​_operate Reverse operate on an argument.
Class Variable __slots__ Undocumented
def __and__(self, other):

Implement the & operator.

When used with SQL expressions, results in an AND operation, equivalent to _expression.and_, that is:

a & b

is equivalent to:

from sqlalchemy import and_
and_(a, b)

Care should be taken when using & regarding operator precedence; the & operator has the highest precedence. The operands should be enclosed in parenthesis if they contain further sub expressions:

(a == 2) & (b == 4)
def __invert__(self):

Implement the ~ operator.

When used with SQL expressions, results in a NOT operation, equivalent to _expression.not_, that is:

~a

is equivalent to:

from sqlalchemy import not_
not_(a)
def __or__(self, other):

Implement the | operator.

When used with SQL expressions, results in an OR operation, equivalent to _expression.or_, that is:

a | b

is equivalent to:

from sqlalchemy import or_
or_(a, b)

Care should be taken when using | regarding operator precedence; the | operator has the highest precedence. The operands should be enclosed in parenthesis if they contain further sub expressions:

(a == 2) | (b == 4)
def bool_op(self, opstring, precedence=0):

Return a custom boolean operator.

This method is shorthand for calling .Operators.op and passing the :paramref:`.Operators.op.is_comparison` flag with True.

See Also

.Operators.op

def op(self, opstring, precedence=0, is_comparison=False, return_type=None):

Produce a generic operator function.

e.g.:

somecolumn.op("*")(5)

produces:

somecolumn * 5

This function can also be used to make bitwise operators explicit. For example:

somecolumn.op('&')(0xff)

is a bitwise AND of the value in somecolumn.

Parameters
opstringUndocumented
precedenceprecedence to apply to the operator, when parenthesizing expressions. A lower number will cause the expression to be parenthesized when applied against another operator with higher precedence. The default value of 0 is lower than all operators except for the comma (,) and AS operators. A value of 100 will be higher or equal to all operators, and -100 will be lower than or equal to all operators.
is​_comparison

if True, the operator will be considered as a "comparison" operator, that is which evaluates to a boolean true/false value, like ==, >, etc. This flag should be set so that ORM relationships can establish that the operator is a comparison operator when used in a custom join condition.

New in version 0.9.2: - added the :paramref:`.Operators.op.is_comparison` flag.
return​_typea .TypeEngine class or object that will force the return type of an expression produced by this operator to be of that type. By default, operators that specify :paramref:`.Operators.op.is_comparison` will resolve to .Boolean, and those that do not will be of the same type as the left-hand operand.
operatora string which will be output as the infix operator between this element and the expression passed to the generated function.
def operate(self, op, *other, **kwargs):

Operate on an argument.

This is the lowest level of operation, raises NotImplementedError by default.

Overriding this on a subclass can allow common behavior to be applied to all operations. For example, overriding .ColumnOperators to apply func.lower() to the left and right side:

class MyComparator(ColumnOperators):
    def operate(self, op, other):
        return op(func.lower(self), func.lower(other))
Parameters
opOperator callable.
*otherthe 'other' side of the operation. Will be a single scalar for most operations.
**kwargsmodifiers. These may be passed by special operators such as ColumnOperators.contains.
__slots__: tuple =

Undocumented