def _format_node(self, expr): op = expr.op() formatted_args = [] def visit(what, extra_indents=0): if isinstance(what, ir.Expr): result = self._format_subexpr(what) else: result = self._indent(str(what)) if extra_indents > 0: result = util.indent(result, self.indent_size) formatted_args.append(result) arg_names = getattr(op, 'display_argnames', op.argnames) if not arg_names: for arg in op.args: if util.is_iterable(arg): for x in arg: visit(x) else: visit(arg) else: for arg, name in zip(op.args, arg_names): if name == 'arg' and isinstance(op, ops.ValueOp): # don't display first argument's name in repr name = None if name is not None: name = self._indent('{0}:'.format(name)) if util.is_iterable(arg): if name is not None and len(arg) > 0: formatted_args.append(name) indents = 1 else: indents = 0 for x in arg: visit(x, extra_indents=indents) else: if name is not None: formatted_args.append(name) indents = 1 else: indents = 0 visit(arg, extra_indents=indents) opname = type(op).__name__ type_display = self._get_type_display(expr) opline = '%s[%s]' % (opname, type_display) return '\n'.join([opline] + formatted_args)
def eq(left, right): if type(left) != type(right): return False if is_iterable(left) and is_iterable(right): return all(map(eq, left, right)) if not isinstance(left, ast.AST) and not isinstance(right, ast.AST): return left == right assert hasattr(left, "_fields") and hasattr(right, "_fields") return left._fields == right._fields and all( eq(getattr(left, left_name), getattr(right, right_name)) for left_name, right_name in zip(left._fields, right._fields))
def eq(left, right): if type(left) != type(right): return False if is_iterable(left) and is_iterable(right): return all(map(eq, left, right)) if not isinstance(left, ast.AST) and not isinstance(right, ast.AST): return left == right assert hasattr(left, '_fields') and hasattr(right, '_fields') return left._fields == right._fields and all( eq(getattr(left, left_name), getattr(right, right_name)) for left_name, right_name in zip(left._fields, right._fields) )
def tuplize_iter(values): yield from ( tuple(tuplize_iter(value)) if util.is_iterable(value) else value for value in values )
def _format_node(self, expr): op = expr.op() formatted_args = [] def visit(what, extra_indents=0): if isinstance(what, ir.Expr): result = self._format_subexpr(what) else: result = self._indent(str(what)) if extra_indents > 0: result = util.indent(result, self.indent_size) formatted_args.append(result) arg_names = getattr(op, 'display_argnames', op.argnames) if not arg_names: for arg in op.flat_args(): visit(arg) else: arg_name_pairs = zip(op.args, arg_names) for arg, name in arg_name_pairs: if name == 'arg' and isinstance(op, ops.ValueOp): # don't display first argument's name in repr name = None if name is not None: name = self._indent(f'{name}:') if util.is_iterable(arg): if name is not None and len(arg) > 0: formatted_args.append(name) indents = 1 else: indents = 0 for x in arg: visit(x, extra_indents=indents) else: if name is not None: formatted_args.append(name) indents = 1 else: indents = 0 visit(arg, extra_indents=indents) opname = type(op).__name__ type_display = self._get_type_display(expr) if isinstance(op, ops.TableNode): try: opline = f"{opname}[{self.memo.get_alias(expr)}]" except KeyError: opline = opname else: opline = f"{type_display} = {opname}" return '\n'.join([opline] + formatted_args)
def _format_node(self, expr): op = expr.op() formatted_args = [] def visit(what, extra_indents=0): if isinstance(what, ir.Expr): result = self._format_subexpr(what) else: result = self._indent(str(what)) if extra_indents > 0: result = util.indent(result, self.indent_size) formatted_args.append(result) arg_names = getattr(op, 'display_argnames', op.argnames) if not arg_names: for arg in op.flat_args(): visit(arg) else: signature = op.signature arg_name_pairs = ( (arg, name) for arg, name in zip(op.args, arg_names) if signature[name].show ) for arg, name in arg_name_pairs: if name == 'arg' and isinstance(op, ops.ValueOp): # don't display first argument's name in repr name = None if name is not None: name = self._indent('{}:'.format(name)) if util.is_iterable(arg): if name is not None and len(arg) > 0: formatted_args.append(name) indents = 1 else: indents = 0 for x in arg: visit(x, extra_indents=indents) else: if name is not None: formatted_args.append(name) indents = 1 else: indents = 0 visit(arg, extra_indents=indents) opname = type(op).__name__ type_display = self._get_type_display(expr) opline = '{}[{}]'.format(opname, type_display) return '\n'.join([opline] + formatted_args)
def container_of(inner, arg, *, type, min_length=0, flatten=False, **kwargs): if not util.is_iterable(arg): raise com.IbisTypeError('Argument must be a sequence') if len(arg) < min_length: raise com.IbisTypeError( f'Arg must have at least {min_length} number of elements') if flatten: arg = util.flatten_iterable(arg) return type(inner(item, **kwargs) for item in arg)
def output_shape(self): arg = getattr(self, name) if util.is_iterable(arg): for expr in arg: try: op = expr.op() except AttributeError: continue if op.output_shape is Shape.COLUMNAR: return Shape.COLUMNAR return Shape.SCALAR else: return arg.op().output_shape
def shape_like(arg, dtype=None): if util.is_iterable(arg): datatype = dtype or highest_precedence_dtype(arg) columnar = util.any_of(arg, ir.AnyColumn) else: datatype = dtype or arg.type() columnar = isinstance(arg, ir.AnyColumn) dtype = dt.dtype(datatype) if columnar: return dtype.column_type() else: return dtype.scalar_type()
def _lift_arg(self, arg, block=None): changed = 0 def _lift(expr): nonlocal changed if isinstance(expr, ir.Expr): lifted_arg = self.lift(expr, block=block) changed += lifted_arg is not expr else: # a string or some other thing lifted_arg = expr return lifted_arg if arg is None: return arg, True if util.is_iterable(arg): result = list(map(_lift, arg)) else: result = _lift(arg) return result, not changed
def _fmt_value_value_op(op: ops.ValueOp, *, aliases: Aliases) -> str: args = [] # loop over argument names and original expression for argname, orig_expr in zip(op.argnames, op.args): # promote argument to a list, so that we don't accidentially repr # entire subtrees when all we want is the formatted argument value if exprs := [ expr for expr in util.promote_list(orig_expr) if expr is not None ]: # format the individual argument values formatted_args = ", ".join( fmt_value(expr, aliases=aliases) for expr in exprs) # if the original argument was a non-string iterable, display it as # a list value = (f"[{formatted_args}]" if util.is_iterable(orig_expr) else formatted_args) # `arg` and `expr` are noisy, so we ignore printing them as a # special case if argname not in ("arg", "expr"): formatted = f"{argname}={value}" else: formatted = value args.append(formatted)
def _sequence_to_tuple(x): return tuple(x) if util.is_iterable(x) else x
def output_dtype(self): arg = getattr(self, name) if util.is_iterable(arg): return highest_precedence_dtype(arg) else: return arg.type()
def _tuple_to_list(t: tuple): for element in t: if util.is_iterable(element): yield list(_tuple_to_list(element)) else: yield element
def flat_args(self): for arg in self.args: if not isinstance(arg, Schema) and is_iterable(arg): yield from arg else: yield arg