Abstract Syntatic Tree

class sqla_helpers.logical.ASTNode(lhs=None, rhs=None, **operand)

A tree’s node represent a logic Sqlalchemy operator. Contains 2 child tree of a set of criterion.

Handled criterions are describes in sqla_helpers.

>>> ast = AndASTNode(id=4, status__name='foo')

Criterions ‘ll be process by the process_param function during processing of subtree by __call__ function.

>>> ast(MyClass, [])
<sqlalchemy.sql.expression.BinaryExpression object at 0x1f04090>

If a node contains criterions, the node is a leaf. (meaning the ASTNode.operand attribute is not None.)

If node contains children, a recursive processing of children subtrees is done.

ASTNode.operator method is excecuted during the return of process_param or during the recursive return of children.

Class:ASTNode is an abstract class which doesn’t implement :method: ASTNode.operator`.
class sqla_helpers.logical.AndASTNode(lhs=None, rhs=None, **operand)
operator(*args)

Excecut a logical and on SQLAlchemy criterions.

class sqla_helpers.logical.OrASTNode(lhs=None, rhs=None, **operand)
operator(*args)

Excecut a logical or on SQLAlchemy criterions.

Q Object

class sqla_helpers.logical.Q(astnode=None, **kwargs)

Class representing logical operators with sqla_helpers syntax.

Q object is only here for processing the sqla_helpers syntax and implements opertators such as or, and, not ...

During operations, the Q object maintains an AST. Each operation return a Q object with an updated AST in relation the current object and the operating object.

The :method: __call__ from Q call the :method:`__call__` from AST children. The return is an SQLAlchemy opertation usable in a Query object.

Some tools to help development

sqla_helpers.utils.call_if_callable(maybe_callable, callable_args=None, callable_kwargs=None, else_=<function <lambda> at 0x7f77eb47aed8>)

If maybe_callable is callable then it’s called with *callable_args and **callable_kwargs. Else the function else_ is called with maybe_callable as argument. else_ default is the identity function, so when maybe_callable is not callable, the function returns maybe_callable.

>>> call_if_callable(u'test')
u'test'
>>> def add2(x):
...     return x + 2
>>> call_if_callable(add2, [4])
6
>>> def add_something(x, something=2)
...     return  x + something
>>> call_if_callable(add_something, [4], {'something': 8})
12
>>> def concat_plop(string):
...        return u'{0}.plop'.format(string)
>>> call_if_callable(u'test', else_=concat_plop)
u'test.plop'
Parameters:
  • maybe_callable – function to call if callable
  • callable_args – List of argument to call with maybe_callable
  • callable_kwargs – Dictonary of (key, value) to call with maybe_callable
  • else – Function to call if maybe_callable is not callable. One parameter supported : maybe_callable