Operators and functions for MagicDotPath
OperatorsType, functionsType aand the functions that use them.
col_name_hyper(name_op, operator)
Get the corresponding column name for an hyperbolic functions from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Examples:
>>> col_name_hyper(DotMap(op1="mass"), HyperBFuncType.COSH)
'cosh_mass'
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def col_name_hyper(name_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding column name for an hyperbolic functions from a MagicDotPath.
Args:
name_op: name of the operand(s).
operator: Operator Type.
Returns:
A column name with the correct format.
Raises:
TypeOperatorError: If type of operator is not `HyperBFuncType`.
Examples:
>>> col_name_hyper(DotMap(op1="mass"), HyperBFuncType.COSH)
'cosh_mass'
"""
if not isinstance(operator, HyperBFuncType):
raise TypeOperatorError([HyperBFuncType], type(operator))
return f"{operator.name}_{name_op.op1}"
col_name_math(name_op, operator)
Get the corresponding column name for a mathematical functions from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Examples:
>>> col_name_math(DotMap(op1="mass"), MathFunctType.CEIL)
'ceil_mass'
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def col_name_math(name_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding column name for a mathematical functions from a MagicDotPath.
Args:
name_op: name of the operand(s).
operator: Operator Type.
Returns:
A column name with the correct format.
Raises:
TypeOperatorError: If type of operator is not `MathFunctType`.
Examples:
>>> col_name_math(DotMap(op1="mass"), MathFunctType.CEIL)
'ceil_mass'
"""
if not isinstance(operator, MathFunctType):
raise TypeOperatorError([MathFunctType], type(operator))
if operator in [
MathFunctType.GCD,
MathFunctType.LCM,
MathFunctType.LOG,
MathFunctType.ROUND,
MathFunctType.TRUNC,
MathFunctType.GREATEST,
MathFunctType.LEAST,
]:
return f"{operator.name}_{name_op.op1}_{name_op.op2}"
return f"{operator.name}_{name_op.op1}"
col_name_ope(name_op, operator)
Get the corresponding column name for an operator from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Examples:
>>> col_name_ope(DotMap(op1="mass", op2="power"), OperatorType.POW)
'mass_pow_power'
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def col_name_ope(name_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding column name for an operator from a MagicDotPath.
Args:
name_op: name of the operand(s).
operator: Operator Type.
Returns:
A column name with the correct format.
Raises:
TypeOperatorError: If type of operator is not `OperatorType.`.
Examples:
>>> col_name_ope(DotMap(op1="mass", op2="power"), OperatorType.POW)
'mass_pow_power'
"""
if not isinstance(operator, OperatorType):
raise TypeOperatorError([OperatorType], type(operator))
if operator in [
OperatorType.ABS,
OperatorType.NEG,
OperatorType.POS,
OperatorType.INVERT,
]:
return f"{operator.name}_{name_op.op1}"
return f"{name_op.op1}_{operator.name}_{name_op.op2}"
col_name_str(name_op, operator)
Get the corresponding column name for a str functions from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Examples:
>>> col_name_str(DotMap(op1="star_name"), StrFunctType.LOWER)
>>> 'lower_star_name'
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def col_name_str(name_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding column name for a str functions from a MagicDotPath.
Args:
name_op: name of the operand(s).
operator: Operator Type.
Returns:
A column name with correct format.
Raises:
TypeOperatorErrir: If type of operator is not `StrFunctType`.
Examples:
>>> col_name_str(DotMap(op1="star_name"), StrFunctType.LOWER)
>>> 'lower_star_name'
"""
if not isinstance(operator, StrFunctType):
raise TypeOperatorError([StrFunctType], type(operator))
return f"{operator.name}_{name_op.op1}"
col_name_trigo(name_op, operator)
Get the corresponding column name for a trigonometric functions from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Examples:
>>> col_name_trigo(DotMap(op1="mass"), TrigoFunctType.ASIND)
>>> "asind_mass"
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def col_name_trigo(name_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding column name for a trigonometric functions from a MagicDotPath.
Args:
name_op: name of the operand(s).
operator: Operator Type.
Returns:
A column name with the correct format.
Raises:
TypeOperatorError: If type of operator is not `TrigoFunctType`.
Examples:
>>> col_name_trigo(DotMap(op1="mass"), TrigoFunctType.ASIND)
>>> "asind_mass"
"""
if not isinstance(operator, TrigoFunctType):
raise TypeOperatorError([TrigoFunctType], type(operator))
if operator in [TrigoFunctType.ATAN2, TrigoFunctType.ATAN2D]:
return f"{operator.name}_{name_op.op1}_{name_op.op2}"
return f"{operator.name}_{name_op.op1}"
json_path_hyper(path_op, operator)
Get the corresponding jsonb path for an hyperbolic function from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def json_path_hyper(path_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding jsonb path for an hyperbolic function from a MagicDotPath.
Args:
path_op: path of the operand(s).
operator: Operator Type.
Returns:
A path with the correct jsonb syntax.
Raises:
TypeOperatorError: If type of operator is not `HyperBFuncType`.
"""
if not isinstance(operator, HyperBFuncType):
raise TypeOperatorError([HyperBFuncType], type(operator))
return f"{operator.psql}(CAST({path_op.op1} AS decimal))"
json_path_math(path_op, operator)
Get the corresponding jsonb path for a mathematical function from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def json_path_math(path_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding jsonb path for a mathematical function from a MagicDotPath.
Args:
path_op: path of the operand(s).
operator: Operator Type.
Returns:
A path with the correct jsonb syntax.
Raises:
TypeOperatorError: If type of operator is not `MathFunctType`.
"""
if not isinstance(operator, MathFunctType):
raise TypeOperatorError([MathFunctType], type(operator))
if operator in [MathFunctType.FACTORIAL]:
return f"{operator.psql}(CAST({path_op.op1} AS integer))"
if operator in [
MathFunctType.SQRT,
MathFunctType.CBRT,
]:
return f"{operator.psql} CAST({path_op.op1} AS decimal)"
if operator in [MathFunctType.GCD, MathFunctType.LCM]:
operand_1 = f"CAST({path_op.op1} AS integer)"
operand_2 = f"CAST({path_op.op2} AS integer)"
return f"{operator.psql}({operand_1}, {operand_2})"
if operator in [MathFunctType.TRUNC, MathFunctType.ROUND]:
return f"{operator.psql}(CAST({path_op.op1} AS decimal), {path_op.op2})"
if operator in [MathFunctType.LOG]:
return f"{operator.psql}({path_op.op2}, CAST({path_op.op1} AS decimal))"
if operator in [MathFunctType.GREATEST, MathFunctType.LEAST]:
operand_1 = f"CAST({path_op.op1} AS decimal)"
operand_2 = f"CAST({path_op.op2} AS decimal)"
return f"{operator.psql}({operand_1}, {operand_2})"
return f"{operator.psql}(CAST({path_op.op1} AS decimal))"
json_path_ope(path_op, operator)
Get the corresponding jsonb path for an operator from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def json_path_ope(path_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding jsonb path for an operator from a MagicDotPath.
Args:
path_op: path of the operand(s).
operator: Operator Type.
Returns:
A path with the correct jsonb syntax.
Raises:
TypeOperatorError: If type of operator is not `OperatorType`.
"""
if not isinstance(operator, OperatorType):
raise TypeOperatorError([OperatorType], type(operator))
if operator in [OperatorType.AND, OperatorType.OR, OperatorType.IN]:
return f"{path_op.op1} {operator.psql} {path_op.op2}"
if operator in [OperatorType.INVERT]:
return f"{operator.psql} {path_op.op1}"
return f"{operator.psql} CAST({path_op.op1} AS decimal)"
json_path_str(path_op, operator)
Get the corresponding jsonb path for a str function from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def json_path_str(path_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding jsonb path for a str function from a MagicDotPath.
Args:
path_op: path of the operand(s).
operator: Operator Type.
Returns:
A path with the correct jsonb syntax.
Raises:
TypeOperatorError: If type of operator is not `StrFunctType`.
"""
if not isinstance(operator, StrFunctType):
raise TypeOperatorError([StrFunctType], type(operator))
return f"{operator.psql}(CAST({path_op.op1} AS TEXT))"
json_path_trigo(path_op, operator)
Get the corresponding jsonb path for a trigonometric function from a MagicDotPath.
Parameters: |
|
---|
Returns: |
|
---|
Exceptions: |
|
---|
Source code in py_linq_sql/utils/classes/op_and_func_of_mdp.py
def json_path_trigo(path_op: DotMap, operator: _OPERATORTYPE) -> str:
"""
Get the corresponding jsonb path for a trigonometric function from a MagicDotPath.
Args:
path_op: path of the operand(s).
operator: Operator Type.
Returns:
A path with the correct jsonb syntax.
Raises:
TypeOperatorError: If type of operator is not `TrigoFunctType`.
"""
if not isinstance(operator, TrigoFunctType):
raise TypeOperatorError([TrigoFunctType], type(operator))
if operator in [TrigoFunctType.ATAN2, TrigoFunctType.ATAN2D]:
return f"{operator.psql}(CAST({path_op.op1} AS decimal), {path_op.op2})"
return f"{operator.psql}(CAST({path_op.op1} AS decimal))"