def to_tree(expr, names=None): """ Represent Blaze expression with core data structures Transform a Blaze expression into a form using only strings, dicts, lists and base types (int, float, datetime, ....) This form can be useful for serialization. Parameters ---------- expr : Expr A Blaze expression Examples -------- >>> t = symbol('t', 'var * {x: int32, y: int32}') >>> to_tree(t) # doctest: +SKIP {'op': 'Symbol', 'args': ['t', 'var * { x : int32, y : int32 }', False]} >>> to_tree(t.x.sum()) # doctest: +SKIP {'op': 'sum', 'args': [{'op': 'Column', 'args': [{'op': 'Symbol' 'args': ['t', 'var * { x : int32, y : int32 }', False]} 'x']}]} Simplify expresion using explicit ``names`` dictionary. In the example below we replace the ``Symbol`` node with the string ``'t'``. >>> tree = to_tree(t.x, names={t: 't'}) >>> tree # doctest: +SKIP {'op': 'Column', 'args': ['t', 'x']} >>> from_tree(tree, namespace={'t': t}) t.x See Also -------- from_tree """ if isinstance(expr, slice): # NOTE: This case must come first, since `slice` objects are not # hashable, so a dict lookup inside `names` will raise an execption. return {u'op': 'slice', u'args': [to_tree(arg, names=names) for arg in [expr.start, expr.stop, expr.step]]} if names and expr in names: return names[expr] if isinstance(expr, tuple): return [to_tree(arg, names=names) for arg in expr] if isinstance(expr, expr_utils._slice): return to_tree(expr.as_slice(), names=names) elif isinstance(expr, _Data): return to_tree(symbol(u8(expr._name), expr.dshape), names) elif isinstance(expr, Expr): return {u'op': u8(type(expr).__name__), u'args': [to_tree(arg, names) for arg in expr._args]} elif isinstance(expr, str): return u8(expr) else: return expr
def json_dumps_trusted(f): # let the server serialize any callable - this is only used for testing # at present - do the error handling when json comes from client so in # object_hook, catch anything that is not pandas_numpy fcn = u8(".".join([f.__module__, f.__name__])) return {u'__!callable': fcn}