Source code for pysqllike.generic.column_operator

# -*- coding: utf-8 -*-
"""
Creates custom classes to interpret Python expression as column operations.


:githublink:`%|py|6`
"""


[docs]class ColumnOperator: """ defines an operation between two columns :githublink:`%|py|12` """
[docs] def __init__(self): """ initiates the operator :param name: name of the column :githublink:`%|py|19` """ pass
[docs] def __str__(self): """ usual :githublink:`%|py|25` """ raise NotImplementedError()
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|31` """ raise NotImplementedError()
[docs]class OperatorId(ColumnOperator): """ defines a constant :githublink:`%|py|39` """
[docs] def __str__(self): """ usual :githublink:`%|py|44` """ return "="
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|50` """ if len(columns) != 1: raise ValueError( "we expect a single value in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]()
[docs]class OperatorMul(ColumnOperator): """ defines the multiplication :githublink:`%|py|66` """
[docs] def __str__(self): """ usual :githublink:`%|py|71` """ return "*"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|77` """ if len(columns) == 0: raise ValueError( "we expect at least a value in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() r = columns[0]() for c in columns[1:]: r *= c() return r
[docs]class OperatorAdd(ColumnOperator): """ defines the addition :githublink:`%|py|97` """
[docs] def __str__(self): """ usual :githublink:`%|py|102` """ return "+"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|108` """ if len(columns) == 0: raise ValueError( "we expect at least a value in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() r = columns[0]() for c in columns[1:]: r += c() return r
[docs]class OperatorDiv(ColumnOperator): """ defines the division :githublink:`%|py|128` """
[docs] def __str__(self): """ usual :githublink:`%|py|133` """ return "/"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|139` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() / columns[1]()
[docs]class OperatorSub(ColumnOperator): """ defines the subtraction :githublink:`%|py|155` """
[docs] def __str__(self): """ usual :githublink:`%|py|160` """ return "-"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|166` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() - columns[1]()
[docs]class OperatorPow(ColumnOperator): """ defines the power :githublink:`%|py|182` """
[docs] def __str__(self): """ usual :githublink:`%|py|187` """ return "**"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|193` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() ** columns[1]()
[docs]class OperatorMod(ColumnOperator): """ defines the operator mod :githublink:`%|py|210` """
[docs] def __str__(self): """ usual :githublink:`%|py|215` """ return "%"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|221` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() % columns[1]()
[docs]class OperatorDivN(ColumnOperator): """ defines the division // :githublink:`%|py|238` """
[docs] def __str__(self): """ usual :githublink:`%|py|243` """ return "//"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|249` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() // columns[1]()
[docs]class OperatorEq(ColumnOperator): """ defines == :githublink:`%|py|266` """
[docs] def __str__(self): """ usual :githublink:`%|py|271` """ return "=="
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|277` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() == columns[1]()
[docs]class OperatorNe(ColumnOperator): """ defines != :githublink:`%|py|293` """
[docs] def __str__(self): """ usual :githublink:`%|py|298` """ return "!="
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|304` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() != columns[1]()
[docs]class OperatorLt(ColumnOperator): """ defines < :githublink:`%|py|320` """
[docs] def __str__(self): """ usual :githublink:`%|py|325` """ return "<"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|331` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() < columns[1]()
[docs]class OperatorGt(ColumnOperator): """ defines > :githublink:`%|py|347` """
[docs] def __str__(self): """ usual :githublink:`%|py|352` """ return ">"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|358` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() > columns[1]()
[docs]class OperatorLe(ColumnOperator): """ defines <= :githublink:`%|py|374` """
[docs] def __str__(self): """ usual :githublink:`%|py|379` """ return "<="
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|385` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() <= columns[1]()
[docs]class OperatorGe(ColumnOperator): """ defines >= :githublink:`%|py|401` """
[docs] def __str__(self): """ usual :githublink:`%|py|406` """ return ">="
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|412` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() >= columns[1]()
[docs]class OperatorOr(ColumnOperator): """ defines ``or`` :githublink:`%|py|428` """
[docs] def __str__(self): """ usual :githublink:`%|py|433` """ return "or"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|439` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() or columns[1]()
[docs]class OperatorAnd(ColumnOperator): """ defines ``and`` :githublink:`%|py|455` """
[docs] def __str__(self): """ usual :githublink:`%|py|460` """ return "and"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|466` """ if len(columns) != 2: raise ValueError( "we expect two values in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return columns[0]() and columns[1]()
[docs]class OperatorNot(ColumnOperator): """ defines ``not`` :githublink:`%|py|482` """
[docs] def __str__(self): """ usual :githublink:`%|py|487` """ return "not"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|493` """ if len(columns) != 1: raise ValueError( "we expect one value in a array here: {0}".format( str(columns))) if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return not columns[0]()
[docs]class OperatorFunc(ColumnOperator): """ defines a function call :githublink:`%|py|509` """
[docs] def __init__(self, func): """ constructor :githublink:`%|py|514` """ self._func = func
[docs] def __str__(self): """ usual :githublink:`%|py|520` """ return "func"
[docs] def __call__(self, columns): """ returns the results of this operation between a list of columns :githublink:`%|py|526` """ if not isinstance(columns, tuple): raise TypeError("we expect a tuple here") for c in columns: c.IsColumnType() return self._func(* [c() for c in columns])