def make_lambda(expression, args): def make_arg(name): if sys.version_info >= (3, 0): return ast.arg(arg=name, annotation=None) else: return ast.Name(id=arg, ctx=ast.Param(), lineno=1, col_offset=0) lambda_ = ast.Lambda( args=ast.arguments( args=[make_arg(arg) for arg in args], varargs=None, varargannotation=None, kwonlyargs=[], kwarg=None, kwargannotation=None, defaults=[], kw_defaults=[]), body=expression.body, ) lambda_ = ast.copy_location(lambda_, expression.body) exp = ast.Expression(body=lambda_, lineno=1, col_offset=0) ast.dump(exp) return eval(compile(exp, "<lambda>", "eval"), __GLOBALS)
def visit_BoolOp(self, node): 'A custom BoolOp can be used in flattened AST' if type(node.op) not in (ast.Add, ast.Mult, ast.BitXor, ast.BitAnd, ast.BitOr): return self.generic_visit(node) # get constant parts of node: list_cste = [ child for child in node.values if isinstance(child, ast.Num) ] if len(list_cste) < 2: return self.generic_visit(node) rest_values = [n for n in node.values if n not in list_cste] fake_node = Unflattening().visit(ast.BoolOp(node.op, list_cste)) fake_node = ast.Expression(fake_node) ast.fix_missing_locations(fake_node) code = compile(fake_node, '<constant folding>', 'eval') obj_env = globals().copy() exec code in obj_env value = eval(code, obj_env) new_node = ast.Num(value) rest_values.append(new_node) return ast.BoolOp(node.op, rest_values)
def make_access_rule(self, body): rule_str = ast.dump(body, False) if rule_str not in self.rule_cache: # requires consistent iteration on dicts kwargs = [ast.arg(arg=k) for k in kwarg_defaults.keys()] kwd = list(map(ast.Constant, kwarg_defaults.values())) try: self.rule_cache[rule_str] = eval(compile( ast.fix_missing_locations( ast.Expression(ast.Lambda( args=ast.arguments( posonlyargs=[], args=[ast.arg(arg='state')], defaults=[], kwonlyargs=kwargs, kw_defaults=kwd), body=body))), '<string>', 'eval'), # globals/locals. if undefined, everything in the namespace *now* would be allowed allowed_globals) except TypeError as e: raise Exception('Parse Error: %s' % e, self.current_spot.name, ast.dump(body, False)) return self.rule_cache[rule_str]
def evaluate(cls, expression: str) -> int: """Evaluates the expression string with overridden operator precedence. How the operator precedence is overriden: 1. Replace the operator signs in `expressions` according to `cls.swapped_signs` translation table. 2. Let Python build an AST, in compliance with overriden operator precedences. 3. Traverses the tree, replacing operator instances back to what they would be if the original `expression` was parsed. 4. Compiles the expression and evaluates it, returning an integer (the result). """ expr_swapped = expression.translate(str.maketrans(cls.swapped_signs)) p = ast.parse(expr_swapped) for node in ast.walk(p): if isinstance(node, ast.BinOp) and isinstance( node.op, tuple(cls.ops_to_swap)): node.op = cls.ops_to_swap[type(node.op)]() expr_node = cast(ast.Expr, p.body[0]) e = ast.Expression(expr_node.value) return eval(compile(e, "<string>", "eval"))
def eval_code(code, ns): """ Runs a string of code, the last part of which may be an expression. """ # handle mis-indented input from multi-line strings code = dedent(code) mod = ast.parse(code) if len(mod.body) == 0: return None if isinstance(mod.body[-1], ast.Expr): expr = ast.Expression(mod.body[-1].value) del mod.body[-1] else: expr = None if len(mod.body): exec(compile(mod, '<exec>', mode='exec'), ns, ns) if expr is not None: return eval(compile(expr, '<eval>', mode='eval'), ns, ns) else: return None
def evaluate_ast_node(node: ast.expr, locals_: Optional[Dict[str, Any]] = None) -> str: """Evaluates an AST node representing an f-string. Args: node: the AST expression extracted from an f-string. locals_: the dictionary of local variables to be used as context for the expression evaluation. Returns: The evaluated AST node expression. """ try: # pylint: disable=eval-used return eval( # type: ignore compile(ast.Expression(node), filename="<string>", mode="eval"), locals_) except NameError as err: LOGGER.warning( "You tried use an undefined variable. Falling back to an empty string." ) LOGGER.error(err) return ""
def exec_eval(script, globals=None, locals=None, name=''): '''Execute a script and return the value of the last expression''' stmts = list(ast.iter_child_nodes(ast.parse(script))) if not stmts: return None if isinstance(stmts[-1], ast.Expr): # the last one is an expression and we will try to return the results # so we first execute the previous statements if len(stmts) > 1: if sys.version_info >= (3, 8): mod = ast.Module(stmts[:-1], []) else: mod = ast.Module(stmts[:-1]) exec(compile(mod, filename=name, mode="exec"), globals, locals) # then we eval the last one return eval( compile(ast.Expression(body=stmts[-1].value), filename=name, mode="eval"), globals, locals) else: # otherwise we just execute the entire code return exec(compile(script, filename=name, mode="exec"), globals, locals)
def get_model(self, target, pattern): 'When target is constant and wildcards have no value yet' # pylint: disable=exec-used if target.n == 0: # zero is too permissive return False getwild = asttools.GetIdentifiers() getwild.visit(pattern) wilds = getwild.variables # let's reduce the model to one wildcard for now # otherwise it adds a lot of checks... if len(wilds) > 1: return False wil = wilds.pop() if wil in self.wildcards: if not isinstance(self.wildcards[wil], ast.Num): return False folded = deepcopy(pattern) folded = asttools.Unleveling().visit(folded) EvalPattern(self.wildcards).visit(folded) folded = asttools.ConstFolding(folded, self.nbits).visit(folded) return folded.n == target.n else: exec("%s = z3.BitVec('%s', %d)" % (wil, wil, self.nbits)) eval_pattern = deepcopy(pattern) eval_pattern = asttools.Unleveling().visit(eval_pattern) ast.fix_missing_locations(eval_pattern) code = compile(ast.Expression(eval_pattern), '<string>', mode='eval') sol = z3.Solver() sol.add(target.n == eval(code)) if sol.check().r == 1: model = sol.model() for inst in model.decls(): self.wildcards[str(inst)] = ast.Num(int(model[inst].as_long())) return True return False
def __call__(self, selection): if not self.is_initialized: self._initialize() try: parse_result = self.expression.parseString(selection, parseAll=True) except ParseException as e: msg = str(e) lines = ["%s: %s" % (msg, selection), " " * (12 + len("%s: " % msg) + e.loc) + "^^^"] raise ValueError('\n'.join(lines)) # Change __ATOM__ in function bodies. It must bind to the arg # name specified below (i.e. 'atom') astnode = self.transformer.visit(deepcopy(parse_result[0].ast())) # Special check for a single literal if isinstance(astnode, ast.Num) or isinstance(astnode, ast.Str): raise ValueError("Cannot use a single literal as a boolean.") if PY2: args = [ast.Name(id='atom', ctx=ast.Param())] signature = ast.arguments(args=args, vararg=None, kwarg=None, defaults=[]) else: args = [ast.arg(arg='atom', annotation=None)] signature = ast.arguments(args=args, vararg=None, kwarg=None, kwonlyargs=[], defaults=[], kw_defaults=[]) func = ast.Expression(body=ast.Lambda(signature, astnode)) source = codegen.to_source(astnode) expr = eval( compile(ast.fix_missing_locations(func), '<string>', mode='eval'), SELECTION_GLOBALS) return _ParsedSelection(expr, source, astnode)
def _parse_match_stmt(code: str, with_start_line: int) -> _Matcher: full_ast = ast.parse(code) w = _get_with(full_ast, with_start_line) assert len(w.items) == 1 b = w.body cases: list[tuple[Pattern, int, Any]] = [] while len(b) == 1 and isinstance(b[0], ast.If): i, = b assert i.test.lineno != i.body[0].lineno a = i.test if isinstance(a, ast.BoolOp): assert isinstance(a.op, ast.And) l, r = a.values pat, guard = ast2pattern(l), r guard = compile(ast.Expression(guard), '<guard>', 'eval') else: pat, guard = ast2pattern(a), None cases.append((pat, i.body[0].lineno, guard)) b = i.orelse if len(b) == 0: else_body = None else: else_body = b[0].lineno return _Matcher(tuple(cases), else_body)
def _expression_to_function(expression, keys, aliases, functions, getter, scope, file_path, object_path): if expression in keys: return lambda: scope[getter](expression) else: node = _expression_to_node(expression, file_path, object_path) try: expr = _ast_as_branch_expression(node.body[0].value, keys, aliases, functions, getter) except KeyError as err: raise uproot.KeyInFileError( err.args[0], keys=sorted(keys) + list(aliases), file_path=file_path, object_path=object_path, ) function = ast.parse("lambda: None").body[0].value function.body = expr expression = ast.Expression(function) expression.lineno = getattr(function, "lineno", 1) expression.col_offset = getattr(function, "col_offset", 0) return eval(compile(expression, "<dynamic>", "eval"), scope)
def _sympify_string(math_string): "Convert math string into SymPy object." # drop pound symbols ('#') since they denote function names # we detect those automatically expr_string = math_string.replace('#', '') # sympify doesn't recognize LN as ln() expr_string = \ re.sub(r'(?<!\w)LN(?!\w)', 'ln', expr_string, flags=re.IGNORECASE) expr_string = \ re.sub(r'(?<!\w)LOG(?!\w)', 'log', expr_string, flags=re.IGNORECASE) expr_string = \ re.sub(r'(?<!\w)EXP(?!\w)', 'exp', expr_string, flags=re.IGNORECASE) # Convert raw variables into StateVariable objects variable_fixes = {Symbol('T'): v.T, Symbol('P'): v.P, Symbol('R'): v.R} # sympify uses eval, so we need to sanitize the input nodes = ast.parse(expr_string) nodes = ast.Expression(nodes.body[0].value) for node in ast.walk(nodes): if type(node) not in _AST_WHITELIST: #pylint: disable=W1504 raise ValueError('Expression from TDB file not in whitelist: ' '{}'.format(expr_string)) return sympify(expr_string).xreplace(variable_fixes)
def eval_code(code: str, ns: Dict[str, Any]) -> None: """Runs a code string The last part of the provided code may be an expression. Parameters ---------- code the Python code to run. ns `locals()` or `globals()` context where to execute code. Returns ------- None """ # handle mis-indented input from multi-line strings code = dedent(code) mod = ast.parse(code) if len(mod.body) == 0: return None expr: Any if isinstance(mod.body[-1], ast.Expr): expr = ast.Expression(mod.body[-1].value) del mod.body[-1] else: expr = None if len(mod.body): exec(compile(mod, "<exec>", mode="exec"), ns, ns) if expr is not None: return eval(compile(expr, "<eval>", mode="eval"), ns, ns) else: return None
def evaluate(node, namespace): try: return eval(compile(ast.Expression(node), '', 'eval'), namespace) except Exception: return VOLATILE
def interpret(branch, awkwardlib=None, swapbytes=True, cntvers=False, tobject=True, speedbump=True): import uproot.tree awkward = uproot.tree._normalize_awkwardlib(awkwardlib) dims, isjagged = (), False if len(branch._fLeaves) == 1: m = interpret._titlehasdims.match(branch._fLeaves[0]._fTitle) if m is not None: dims = tuple( int(x) for x in re.findall(interpret._itemdimpattern, branch._fLeaves[0]._fTitle)) if dims == (): dims = (branch._fLeaves[0]._fLen, ) if branch._fLeaves[0]._fLen > 1 else () if any( interpret._itemdimpattern.match(x) is None for x in re.findall(interpret._itemanypattern, branch._fLeaves[0]._fTitle)): isjagged = True else: for leaf in branch._fLeaves: if interpret._titlehasdims.match(leaf._fTitle): return None try: if len(branch._fLeaves) == 1: if isinstance(branch._streamer, uproot.rootio.TStreamerObjectPointer): obj = branch._streamer._fTypeName if obj.endswith(b"*"): obj = obj[:-1] obj = uproot.rootio._safename(obj) if obj in branch._context.classes: return _obj_or_genobj(branch._context.classes.get(obj), branch, isjagged, cntvers=cntvers, tobject=tobject, speedbump=speedbump) # Process Double32_t and Float16_t types possibly packed in TLeafElement leaftype = uproot.const.kBase if branch._fLeaves[0].__class__.__name__ == "TLeafElement": leaftype = _normalize_ftype(branch._fLeaves[0]._fType) iskDouble32 = leaftype == uproot.const.kDouble32 iskFloat16 = leaftype == uproot.const.kFloat16 if iskDouble32 or iskFloat16: def transform(node, tofloat=True): if isinstance(node, ast.AST): if isinstance(node, ast.Name) and isinstance( node.ctx, ast.Load) and node.id == "pi": out = ast.Num(3.141592653589793) # TMath::Pi() elif isinstance(node, ast.Num): out = ast.Num(float(node.n)) elif isinstance(node, ast.BinOp) and isinstance( node.op, (ast.Add, ast.Sub, ast.Mult, ast.Div)): out = ast.BinOp(transform(node.left), node.op, transform(node.right)) elif isinstance(node, ast.UnaryOp) and isinstance( node.op, ast.USub): out = ast.UnaryOp(node.op, transform(node.operand)) elif isinstance(node, ast.List) and isinstance( node.ctx, ast.Load) and len(node.elts) == 2: out = ast.List([ transform(node.elts[0]), transform(node.elts[1]) ], node.ctx) elif isinstance(node, ast.List) and isinstance( node.ctx, ast.Load) and len( node.elts) == 3 and isinstance( node.elts[2], ast.Num): out = ast.List([ transform(node.elts[0]), transform(node.elts[1]), node.elts[2] ], node.ctx) else: raise Exception(ast.dump(node)) out.lineno, out.col_offset = node.lineno, node.col_offset return out else: raise Exception(ast.dump(node)) try: left, right = branch._streamer._fTitle.index( b"["), branch._streamer._fTitle.index(b"]") except (ValueError, AttributeError): low, high, numbits = 0, 0, 0 else: try: spec = eval( compile( ast.Expression( transform( ast.parse(branch._streamer. _fTitle[left:right + 1]).body[0].value)), repr(branch._streamer._fTitle), "eval")) if len(spec) == 2: low, high = spec numbits = None else: low, high, numbits = spec except: return None if iskDouble32 and numbits == 0: out = asdtype(awkward.numpy.dtype((">f4", dims)), awkward.numpy.dtype(("f8", dims))) elif iskDouble32 and numbits is None: out = asdouble32(low, high, 32, dims) elif iskDouble32: out = asdouble32(low, high, numbits, dims) elif iskFloat16 and numbits == 0: out = asfloat16(low, high, 12, dims) elif iskFloat16 and numbits is None: out = asfloat16(low, high, 32, dims) elif iskFloat16: out = asfloat16(low, high, numbits, dims) else: return None else: fromdtype = _leaf2dtype(branch._fLeaves[0], awkward).newbyteorder(">") if swapbytes: out = asdtype( awkward.numpy.dtype((fromdtype, dims)), awkward.numpy.dtype( (fromdtype.newbyteorder("="), dims))) else: out = asdtype(awkward.numpy.dtype((fromdtype, dims)), awkward.numpy.dtype((fromdtype, dims))) if branch._fLeaves[0]._fLeafCount is None: return out else: return asjagged(out) elif len(branch._fLeaves) > 1: fromdtype = awkward.numpy.dtype([ (str(leaf._fName.decode("ascii")), _leaf2dtype(leaf, awkward).newbyteorder(">")) for leaf in branch._fLeaves ]) if swapbytes: todtype = awkward.numpy.dtype([ (str(leaf._fName.decode("ascii")), _leaf2dtype(leaf, awkward).newbyteorder("=")) for leaf in branch._fLeaves ]) else: todtype = fromdtype if all(leaf._fLeafCount is None for leaf in branch._fLeaves): return asdtype(awkward.numpy.dtype((fromdtype, dims)), awkward.numpy.dtype((todtype, dims))) else: return None except _NotNumerical: if len(branch._fLeaves) == 1: if len(branch._fBranches) > 0 and all( len(x._fLeaves) == 1 and x._fLeaves[0]._fLeafCount is branch._fLeaves[0] for x in branch._fBranches): return asdtype(">i4") if isinstance(branch._streamer, uproot.rootio.TStreamerObject): obj = uproot.rootio._safename(branch._streamer._fTypeName) if obj == "string": return asgenobj(STLString(awkward), branch._context, 0) elif obj in branch._context.classes: return _obj_or_genobj(branch._context.classes.get(obj), branch, isjagged, cntvers=cntvers, tobject=tobject, speedbump=speedbump) if isinstance(branch._streamer, uproot.rootio.TStreamerInfo): obj = uproot.rootio._safename(branch._streamer._fName) if obj == "string": return asgenobj(STLString(awkward), branch._context, 0) elif obj in branch._context.classes: return _obj_or_genobj(branch._context.classes.get(obj), branch, isjagged, cntvers=cntvers, tobject=tobject, speedbump=speedbump) if branch._fLeaves[0].__class__.__name__ == "TLeafC": return asstring(skipbytes=1) elif branch._fLeaves[0].__class__.__name__ == "TLeafElement": if isinstance(branch._streamer, uproot.rootio.TStreamerBasicType): try: fromdtype = _ftype2dtype(branch._streamer._fType, awkward) except _NotNumerical: pass else: if swapbytes: todtype = fromdtype.newbyteorder("=") else: todtype = fromdtype fromdims, remainder = divmod(branch._streamer._fSize, fromdtype.itemsize) if remainder == 0: todims = dims if reduce(lambda x, y: x * y, todims, 1) != fromdims: todims = (fromdims, ) return asdtype( awkward.numpy.dtype((fromdtype, (fromdims, ))), awkward.numpy.dtype((todtype, todims))) if isinstance(branch._streamer, uproot.rootio.TStreamerBasicPointer): if uproot.const.kOffsetP < branch._streamer._fType < uproot.const.kOffsetP + 20: try: fromdtype = _ftype2dtype( branch._streamer._fType - uproot.const.kOffsetP, awkward) except _NotNumerical: pass else: if swapbytes: todtype = fromdtype.newbyteorder("=") else: todtype = fromdtype if len(branch._fLeaves) == 1 and branch._fLeaves[ 0]._fLeafCount is not None: return asjagged(asdtype(fromdtype, todtype), skipbytes=1) if isinstance(branch._streamer, uproot.rootio.TStreamerObjectAny): if getattr(branch._streamer, "_fTypeName", None) in (b"TArrayC", b"TArrayS", b"TArrayI", b"TArrayL", b"TArrayL64", b"TArrayF", b"TArrayD"): return asjagged(asdtype( getattr( uproot.rootio, branch._streamer._fTypeName.decode( "ascii"))._dtype), skipbytes=4) if isinstance(branch._streamer, uproot.rootio.TStreamerString): return asstring(skipbytes=1) if isinstance(branch._streamer, uproot.rootio.TStreamerSTLstring): return asstring(skipbytes=7) if getattr(branch._streamer, "_fType", None) == uproot.const.kCharStar: return asstring(skipbytes=4) if getattr(branch._streamer, "_fSTLtype", None) == uproot.const.kSTLvector: try: fromdtype = _ftype2dtype(branch._streamer._fCtype, awkward) if swapbytes: ascontent = asdtype(fromdtype, fromdtype.newbyteorder("=")) else: ascontent = asdtype(fromdtype, fromdtype) if branch._isTClonesArray: return asgenobj( SimpleArray(STLVector(asdtype(">i2"))), branch._context, 6) else: return asjagged(ascontent, skipbytes=10) except _NotNumerical: if branch._vecstreamer is not None: try: streamerClass = branch._vecstreamer.pyclass except AttributeError: obj = uproot.rootio._safename( branch._vecstreamer._fName) if obj in branch._context.classes: streamerClass = branch._context.classes.get( obj) if streamerClass.__name__ == "string": return asgenobj(STLVector(STLString(awkward)), branch._context, 6) if len(branch._fBranches) != 0: return None try: recarray = streamerClass._recarray_dtype( cntvers=cntvers, tobject=tobject) except (AttributeError, ValueError): return asgenobj(STLVector(streamerClass), branch._context, 6) else: if streamerClass._methods is None: return asjagged(astable(asdtype(recarray)), skipbytes=10) else: return asjagged(asobj( astable(asdtype(recarray)), streamerClass._methods), skipbytes=10) if hasattr(branch._streamer, "_fTypeName"): m = re.match(b"bitset<([1-9][0-9]*)>", branch._streamer._fTypeName) if m is not None: return asjagged(asstlbitset(int(m.group(1))), skipbytes=6) if getattr(branch._streamer, "_fTypeName", None) == b"vector<bool>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Bool_t>": return asjagged(asdtype(awkward.numpy.bool_), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<char>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Char_t>": return asjagged(asdtype("i1"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<unsigned char>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<UChar_t>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Byte_t>": return asjagged(asdtype("u1"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<short>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Short_t>": return asjagged(asdtype("i2"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<unsigned short>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<UShort_t>": return asjagged(asdtype("u2"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<int>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Int_t>": return asjagged(asdtype("i4"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<unsigned int>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<UInt_t>": return asjagged(asdtype("u4"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<long>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Long_t>": return asjagged(asdtype("i8"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<unsigned long>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<ULong64_t>": return asjagged(asdtype("u8"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<float>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Float_t>": return asjagged(asdtype("f4"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<double>" or getattr( branch._streamer, "_fTypeName", None) == b"vector<Double_t>": return asjagged(asdtype("f8"), skipbytes=10) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<string>": return asgenobj(STLVector(STLString(awkward)), branch._context, 6) if getattr(branch._streamer, "_fTypeName", None) == b"map<string,bool>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Bool_t>": return asgenobj( STLMap(STLString(awkward), asdtype(awkward.numpy.bool_)), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,char>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Char_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i1")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,unsigned char>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,UChar_t>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Byte_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u1")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,short>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Short_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i2")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,unsigned short>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,UShort_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u2")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,int>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Int_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i4")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,unsigned int>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,UInt_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u4")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,long>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Long_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i8")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,unsigned long>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,ULong64_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u8")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,float>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Float_t>": return asgenobj(STLMap(STLString(awkward), asdtype("f4")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,double>" or getattr( branch._streamer, "_fTypeName", None) == b"map<string,Double_t>": return asgenobj(STLMap(STLString(awkward), asdtype("f8")), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"map<string,string>": return asgenobj( STLMap(STLString(awkward), STLString(awkward)), branch._context, 6) if getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<bool> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Bool_t> >": return asgenobj( STLVector(STLVector(asdtype(awkward.numpy.bool_))), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<char> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Char_t> >": return asgenobj(STLVector(STLVector(asdtype("i1"))), branch._context, 6) elif getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<unsigned char> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<UChar_t> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Byte_t> >": return asgenobj(STLVector(STLVector(asdtype("u1"))), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<short> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Short_t> >": return asgenobj(STLVector(STLVector(asdtype(">i2"))), branch._context, 6) elif getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<unsigned short> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<UShort_t> >": return asgenobj(STLVector(STLVector(asdtype(">u2"))), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<int> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Int_t> >": return asgenobj(STLVector(STLVector(asdtype(">i4"))), branch._context, 6) elif getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<unsigned int> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<UInt_t> >": return asgenobj(STLVector(STLVector(asdtype(">u4"))), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<long> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Long_t> >": return asgenobj(STLVector(STLVector(asdtype(">i8"))), branch._context, 6) elif getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<unsigned long> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<ULong64_t> >": return asgenobj(STLVector(STLVector(asdtype(">u8"))), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<float> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Float_t> >": return asgenobj(STLVector(STLVector(asdtype(">f4"))), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<double> >" or getattr( branch._streamer, "_fTypeName", None) == b"vector<vector<Double_t> >": return asgenobj(STLVector(STLVector(asdtype(">f8"))), branch._context, 6) elif getattr(branch._streamer, "_fTypeName", None) == b"vector<vector<string> >": return asgenobj(STLVector(STLVector(STLString(awkward))), branch._context, 6) m = re.match(b"bitset<([1-9][0-9]*)>", branch._fClassName) if m is not None: return asstlbitset(int(m.group(1))) if branch._fClassName == b"string": return asstring(skipbytes=1) if branch._fClassName == b"vector<bool>" or branch._fClassName == b"vector<Bool_t>": return asjagged(asdtype(awkward.numpy.bool_), skipbytes=10) elif branch._fClassName == b"vector<char>" or branch._fClassName == b"vector<Char_t>": return asjagged(asdtype("i1"), skipbytes=10) elif branch._fClassName == b"vector<unsigned char>" or branch._fClassName == b"vector<UChar_t>" or branch._fClassName == b"vector<Byte_t>": return asjagged(asdtype("u1"), skipbytes=10) elif branch._fClassName == b"vector<short>" or branch._fClassName == b"vector<Short_t>": return asjagged(asdtype("i2"), skipbytes=10) elif branch._fClassName == b"vector<unsigned short>" or branch._fClassName == b"vector<UShort_t>": return asjagged(asdtype("u2"), skipbytes=10) elif branch._fClassName == b"vector<int>" or branch._fClassName == b"vector<Int_t>": return asjagged(asdtype("i4"), skipbytes=10) elif branch._fClassName == b"vector<unsigned int>" or branch._fClassName == b"vector<UInt_t>": return asjagged(asdtype("u4"), skipbytes=10) elif branch._fClassName == b"vector<long>" or branch._fClassName == b"vector<Long_t>": return asjagged(asdtype("i8"), skipbytes=10) elif branch._fClassName == b"vector<unsigned long>" or branch._fClassName == b"vector<ULong64_t>": return asjagged(asdtype("u8"), skipbytes=10) elif branch._fClassName == b"vector<float>" or branch._fClassName == b"vector<Float_t>": return asjagged(asdtype("f4"), skipbytes=10) elif branch._fClassName == b"vector<double>" or branch._fClassName == b"vector<Double_t>": return asjagged(asdtype("f8"), skipbytes=10) elif branch._fClassName == b"vector<string>": return asgenobj(STLVector(STLString(awkward)), branch._context, 6) if branch._fClassName == b"vector<vector<bool> >" or branch._fClassName == b"vector<vector<Bool_t> >": return asgenobj( STLVector(STLVector(asdtype(awkward.numpy.bool_))), branch._context, 6) elif branch._fClassName == b"vector<vector<char> >" or branch._fClassName == b"vector<vector<Char_t> >": return asgenobj(STLVector(STLVector(asdtype("i1"))), branch._context, 6) elif branch._fClassName == b"vector<vector<unsigned char> >" or branch._fClassName == b"vector<vector<UChar_t> >" or branch._fClassName == b"vector<vector<Byte_t> >": return asgenobj(STLVector(STLVector(asdtype("u1"))), branch._context, 6) elif branch._fClassName == b"vector<vector<short> >" or branch._fClassName == b"vector<vector<Short_t> >": return asgenobj(STLVector(STLVector(asdtype(">i2"))), branch._context, 6) elif branch._fClassName == b"vector<vector<unsigned short> >" or branch._fClassName == b"vector<vector<UShort_t> >": return asgenobj(STLVector(STLVector(asdtype(">u2"))), branch._context, 6) elif branch._fClassName == b"vector<vector<int> >" or branch._fClassName == b"vector<vector<Int_t> >": return asgenobj(STLVector(STLVector(asdtype(">i4"))), branch._context, 6) elif branch._fClassName == b"vector<vector<unsigned int> >" or branch._fClassName == b"vector<vector<UInt_t> >": return asgenobj(STLVector(STLVector(asdtype(">u4"))), branch._context, 6) elif branch._fClassName == b"vector<vector<long> >" or branch._fClassName == b"vector<vector<Long_t> >": return asgenobj(STLVector(STLVector(asdtype(">i8"))), branch._context, 6) elif branch._fClassName == b"vector<vector<unsigned long> >" or branch._fClassName == b"vector<vector<ULong64_t> >": return asgenobj(STLVector(STLVector(asdtype(">u8"))), branch._context, 6) elif branch._fClassName == b"vector<vector<float> >" or branch._fClassName == b"vector<vector<Float_t> >": return asgenobj(STLVector(STLVector(asdtype(">f4"))), branch._context, 6) elif branch._fClassName == b"vector<vector<double> >" or branch._fClassName == b"vector<vector<Double_t> >": return asgenobj(STLVector(STLVector(asdtype(">f8"))), branch._context, 6) elif branch._fClassName == b"vector<vector<string> >": return asgenobj(STLVector(STLVector(STLString(awkward))), branch._context, 6) if branch._fClassName == b"map<string,bool>" or branch._fClassName == b"map<string,Bool_t>": return asgenobj( STLMap(STLString(awkward), asdtype(awkward.numpy.bool_)), branch._context, 6) elif branch._fClassName == b"map<string,char>" or branch._fClassName == b"map<string,Char_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i1")), branch._context, 6) elif branch._fClassName == b"map<string,unsigned char>" or branch._fClassName == b"map<string,UChar_t>" or branch._fClassName == b"map<string,Byte_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u1")), branch._context, 6) elif branch._fClassName == b"map<string,short>" or branch._fClassName == b"map<string,Short_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i2")), branch._context, 6) elif branch._fClassName == b"map<string,unsigned short>" or branch._fClassName == b"map<string,UShort_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u2")), branch._context, 6) elif branch._fClassName == b"map<string,int>" or branch._fClassName == b"map<string,Int_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i4")), branch._context, 6) elif branch._fClassName == b"map<string,unsigned int>" or branch._fClassName == b"map<string,UInt_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u4")), branch._context, 6) elif branch._fClassName == b"map<string,long>" or branch._fClassName == b"map<string,Long_t>": return asgenobj(STLMap(STLString(awkward), asdtype("i8")), branch._context, 6) elif branch._fClassName == b"map<string,unsigned long>" or branch._fClassName == b"map<string,ULong64_t>": return asgenobj(STLMap(STLString(awkward), asdtype("u8")), branch._context, 6) elif branch._fClassName == b"map<string,float>" or branch._fClassName == b"map<string,Float_t>": return asgenobj(STLMap(STLString(awkward), asdtype("f4")), branch._context, 6) elif branch._fClassName == b"map<string,double>" or branch._fClassName == b"map<string,Double_t>": return asgenobj(STLMap(STLString(awkward), asdtype("f8")), branch._context, 6) elif branch._fClassName == b"map<string,string>": return asgenobj( STLMap(STLString(awkward), STLString(awkward)), branch._context, 6) if branch.name.endswith( b".first") and branch._fClassName.startswith( b"pair<string,"): return asgenobj(SimpleArray(STLString(awkward)), branch._context, 6) if branch.name.endswith(b".second"): m = interpret._pairsecond.match(branch._fClassName) if m is not None: t, = m.groups() if t == b"vector<bool>" or t == b"vector<Bool_t>": return asgenobj( SimpleArray( STLVector(asdtype(awkward.numpy.bool_))), branch._context, 6) elif t == b"vector<char>" or t == b"vector<Char_t>": return asgenobj( SimpleArray(STLVector(asdtype("i1"))), branch._context, 6) elif t == b"vector<unsigned char>" or t == b"vector<UChar_t>" or t == b"vector<Byte_t>": return asgenobj( SimpleArray(STLVector(asdtype("u1"))), branch._context, 6) elif t == b"vector<short>" or t == b"vector<Short_t>": return asgenobj( SimpleArray(STLVector(asdtype("i2"))), branch._context, 6) elif t == b"vector<unsigned short>" or t == b"vector<UShort_t>": return asgenobj( SimpleArray(STLVector(asdtype("u2"))), branch._context, 6) elif t == b"vector<int>" or t == b"vector<Int_t>": return asgenobj( SimpleArray(STLVector(asdtype("i4"))), branch._context, 6) elif t == b"vector<unsigned int>" or t == b"vector<UInt_t>": return asgenobj( SimpleArray(STLVector(asdtype("u4"))), branch._context, 6) elif t == b"vector<long>" or t == b"vector<Long_t>": return asgenobj( SimpleArray(STLVector(asdtype("i8"))), branch._context, 6) elif t == b"vector<unsigned long>" or t == b"vector<ULong64_t>": return asgenobj( SimpleArray(STLVector(asdtype("u8"))), branch._context, 6) elif t == b"vector<float>" or t == b"vector<Float_t>": return asgenobj( SimpleArray(STLVector(asdtype("f4"))), branch._context, 6) elif t == b"vector<double>" or t == b"vector<Double_t>": return asgenobj( SimpleArray(STLVector(asdtype("f8"))), branch._context, 6) elif t == b"vector<string>": return asgenobj( SimpleArray(STLVector(STLString(awkward))), branch._context, 6) return None
def interpret(branch, swapbytes=True): dims = () if len(branch.fLeaves) == 1: m = interpret._titlehasdims.match(branch.fLeaves[0].fTitle) if m is not None: dims = tuple( int(x) for x in re.findall(interpret._itemdimpattern, branch.fLeaves[0].fTitle)) else: for leaf in branch.fLeaves: if interpret._titlehasdims.match(leaf.fTitle): return None try: if len(branch.fLeaves) == 1: if isinstance(branch._streamer, uproot.rootio.TStreamerObjectPointer): obj = branch._streamer.fTypeName.decode("utf-8") if obj.endswith("*"): obj = obj[:-1] if obj in branch._context.classes: return asobj(branch._context.classes.get(obj), branch._context) if branch.fLeaves[ 0].__class__.__name__ == "TLeafElement" and branch.fLeaves[ 0].fType == uproot.const.kDouble32: def transform(node, tofloat=True): if isinstance(node, ast.AST): if isinstance(node, ast.Name) and isinstance( node.ctx, ast.Load) and node.id == "pi": out = ast.Num(3.141592653589793) # TMath::Pi() elif isinstance(node, ast.Num): out = ast.Num(float(node.n)) elif isinstance(node, ast.BinOp) and isinstance( node.op, (ast.Add, ast.Sub, ast.Mult, ast.Div)): out = ast.BinOp(transform(node.left), node.op, transform(node.right)) elif isinstance(node, ast.UnaryOp) and isinstance( node.op, ast.USub): out = ast.UnaryOp(node.op, transform(node.operand)) elif isinstance(node, ast.List) and isinstance( node.ctx, ast.Load) and len(node.elts) == 2: out = ast.List([ transform(node.elts[0]), transform(node.elts[1]) ], node.ctx) elif isinstance(node, ast.List) and isinstance( node.ctx, ast.Load) and len( node.elts) == 3 and isinstance( node.elts[2], ast.Num): out = ast.List([ transform(node.elts[0]), transform(node.elts[1]), node.elts[2] ], node.ctx) else: raise Exception(ast.dump(node)) out.lineno, out.col_offset = node.lineno, node.col_offset return out else: raise Exception(ast.dump(node)) try: left, right = branch._streamer.fTitle.index( b"["), branch._streamer.fTitle.index(b"]") except (ValueError, AttributeError): out = asdtype(">f4", "f8", dims, dims) else: try: spec = eval( compile( ast.Expression( transform( ast.parse(branch._streamer. fTitle[left:right + 1]).body[0].value)), repr(branch._streamer.fTitle), "eval")) if len(spec) == 2: low, high = spec numbits = 32 else: low, high, numbits = spec out = asdouble32(low, high, numbits, dims, dims) except: return None else: fromdtype = _leaf2dtype(branch.fLeaves[0]).newbyteorder(">") if swapbytes: out = asdtype(fromdtype, fromdtype.newbyteorder("="), dims, dims) else: out = asdtype(fromdtype, fromdtype, dims, dims) if branch.fLeaves[0].fLeafCount is None: return out else: return asjagged(out) elif len(branch.fLeaves) > 1: fromdtype = numpy.dtype([(str(leaf.fName.decode("ascii")), _leaf2dtype(leaf).newbyteorder(">")) for leaf in branch.fLeaves]) if swapbytes: todtype = numpy.dtype([(str(leaf.fName.decode("ascii")), _leaf2dtype(leaf).newbyteorder("=")) for leaf in branch.fLeaves]) else: todtype = fromdtype if all(leaf.fLeafCount is None for leaf in branch.fLeaves): return asdtype(fromdtype, todtype, dims, dims) else: return None except _NotNumerical: if len(branch.fLeaves) == 1: if len(branch.fBranches) > 0 and all( len(x.fLeaves) == 1 and x.fLeaves[0].fLeafCount is branch.fLeaves[0] for x in branch.fBranches): return asdtype(">i4") if isinstance(branch._streamer, uproot.rootio.TStreamerObject): obj = branch._streamer.fTypeName.decode("utf-8") if obj in branch._context.classes: return asobj(branch._context.classes.get(obj), branch._context) if isinstance(branch._streamer, uproot.rootio.TStreamerInfo): obj = branch._streamer.fName.decode("utf-8") if obj in branch._context.classes: return asobj(branch._context.classes.get(obj), branch._context) if branch.fLeaves[0].__class__.__name__ == "TLeafC": return asstrings(skip_bytes=1, skip4_if_255=True) elif branch.fLeaves[0].__class__.__name__ == "TLeafElement": if isinstance(branch._streamer, uproot.rootio.TStreamerBasicType): try: fromdtype = _ftype2dtype(branch._streamer.fType) except _NotNumerical: pass else: if swapbytes: todtype = fromdtype.newbyteorder("=") else: todtype = fromdtype fromdims, remainder = divmod(branch._streamer.fSize, fromdtype.itemsize) if remainder == 0: todims = dims if reduce(lambda x, y: x * y, todims, 1) != fromdims: todims = (fromdims, ) return asdtype(fromdtype, todtype, (fromdims, ), todims) if isinstance(branch._streamer, uproot.rootio.TStreamerBasicPointer): if uproot.const.kOffsetP < branch._streamer.fType < uproot.const.kOffsetP + 20: try: fromdtype = _ftype2dtype(branch._streamer.fType - uproot.const.kOffsetP) except _NotNumerical: pass else: if swapbytes: todtype = fromdtype.newbyteorder("=") else: todtype = fromdtype if len(branch.fLeaves) == 1 and branch.fLeaves[ 0].fLeafCount is not None: return asjagged(asdtype(fromdtype, todtype), skip_bytes=1) if isinstance(branch._streamer, uproot.rootio.TStreamerString): return asstrings(skip_bytes=1, skip4_if_255=True) if isinstance(branch._streamer, uproot.rootio.TStreamerSTLstring): return asstrings(skip_bytes=7, skip4_if_255=True ) # FIXME: not sure about skip4_if_255 if getattr(branch._streamer, "fType", None) == uproot.const.kCharStar: return asstrings(skip_bytes=4, skip4_if_255=False) if getattr(branch._streamer, "fSTLtype", None) == uproot.const.kSTLvector: try: fromdtype = _ftype2dtype(branch._streamer.fCtype) if swapbytes: ascontent = asdtype(fromdtype, fromdtype.newbyteorder("=")) else: ascontent = asdtype(fromdtype, fromdtype) return asstlvector(ascontent) except _NotNumerical: if branch._vecstreamer is not None: try: streamerClass = branch._vecstreamer.pyclass except AttributeError: obj = branch._vecstreamer.fName.decode("utf-8") if obj in branch._context.classes: streamerClass = branch._context.classes.get( obj) return asobjs(streamerClass, branch._context) if hasattr(branch._streamer, "fTypeName"): m = re.match(b"bitset<([1-9][0-9]*)>", branch._streamer.fTypeName) if m is not None: return asstlbitset(int(m.group(1))) if getattr(branch._streamer, "fTypeName", None) == b"vector<bool>": return asstlvector(asdtype(numpy.bool_)) elif getattr(branch._streamer, "fTypeName", None) == b"vector<char>": return asstlvector(asdtype("i1")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<unsigned char>": return asstlvector(asdtype("u1")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<short>": return asstlvector(asdtype("i2")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<unsigned short>": return asstlvector(asdtype("u2")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<int>": return asstlvector(asdtype("i4")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<unsigned int>": return asstlvector(asdtype("u4")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<long>": return asstlvector(asdtype("i8")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<unsigned long>": return asstlvector(asdtype("u8")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<float>": return asstlvector(asdtype("f4")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<double>": return asstlvector(asdtype("f8")) elif getattr(branch._streamer, "fTypeName", None) == b"vector<string>": return asstlvecstrings() if getattr(branch._streamer, "fTypeName", None) == b"vector<vector<bool> >": return asstlvectorvector(numpy.bool_) elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<char> >": return asstlvectorvector("i1") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<unsigned char> >": return asstlvectorvector("u1") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<short> >": return asstlvectorvector(">i2") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<unsigned short> >": return asstlvectorvector(">u2") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<int> >": return asstlvectorvector(">i4") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<unsigned int> >": return asstlvectorvector(">u4") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<long> >": return asstlvectorvector(">i8") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<unsigned long> >": return asstlvectorvector(">u8") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<float> >": return asstlvectorvector(">f4") elif getattr(branch._streamer, "fTypeName", None) == b"vector<vector<double> >": return asstlvectorvector(">f8") m = re.match(b"bitset<([1-9][0-9]*)>", branch.fClassName) if m is not None: return asstlbitset(int(m.group(1))) if branch.fClassName == b"string": return asstrings() if branch.fClassName == b"vector<bool>": return asstlvector(asdtype(numpy.bool_)) elif branch.fClassName == b"vector<char>": return asstlvector(asdtype("i1")) elif branch.fClassName == b"vector<unsigned char>": return asstlvector(asdtype("u1")) elif branch.fClassName == b"vector<short>": return asstlvector(asdtype("i2")) elif branch.fClassName == b"vector<unsigned short>": return asstlvector(asdtype("u2")) elif branch.fClassName == b"vector<int>": return asstlvector(asdtype("i4")) elif branch.fClassName == b"vector<unsigned int>": return asstlvector(asdtype("u4")) elif branch.fClassName == b"vector<long>": return asstlvector(asdtype("i8")) elif branch.fClassName == b"vector<unsigned long>": return asstlvector(asdtype("u8")) elif branch.fClassName == b"vector<float>": return asstlvector(asdtype("f4")) elif branch.fClassName == b"vector<double>": return asstlvector(asdtype("f8")) elif branch.fClassName == b"vector<string>": return asstlvecstrings() if branch.fClassName == b"vector<vector<bool> >": return asstlvectorvector(numpy.bool_) elif branch.fClassName == b"vector<vector<char> >": return asstlvectorvector("i1") elif branch.fClassName == b"vector<vector<unsigned char> >": return asstlvectorvector("u1") elif branch.fClassName == b"vector<vector<short> >": return asstlvectorvector(">i2") elif branch.fClassName == b"vector<vector<unsigned short> >": return asstlvectorvector(">u2") elif branch.fClassName == b"vector<vector<int> >": return asstlvectorvector(">i4") elif branch.fClassName == b"vector<vector<unsigned int> >": return asstlvectorvector(">u4") elif branch.fClassName == b"vector<vector<long> >": return asstlvectorvector(">i8") elif branch.fClassName == b"vector<vector<unsigned long> >": return asstlvectorvector(">u8") elif branch.fClassName == b"vector<vector<float> >": return asstlvectorvector(">f4") elif branch.fClassName == b"vector<vector<double> >": return asstlvectorvector(">f8") return None
def execute(cmd): """Execute color commands.""" import coloraide g = { 'Color': coloraide.Color, 'coloraide': coloraide, 'NaN': coloraide.NaN, 'Piecewise': coloraide.Piecewise, 'ColorRow': ColorRow } console = '' colors = [] # Build AST tree src = cmd.strip() lines = src.split('\n') try: tree = ast.parse(src) except Exception: import traceback return '{}'.format(traceback.format_exc()), colors for node in tree.body: result = None # Format source as Python console statements start = node.lineno end = node.end_lineno stmt = lines[start - 1: end] command = '' for i, line in enumerate(stmt, 0): if i == 0: stmt[i] = '>>> ' + line else: stmt[i] = '... ' + line command += '\n'.join(stmt) if isinstance(node, AST_BLOCKS): command += '\n... ' try: # Capture anything sent to standard out text = '' with std_output() as s: # Execute code if isinstance(node, ast.Expr): _eval = ast.Expression(node.value) result = eval(compile(_eval, '<string>', 'eval'), g) else: _exec = ast.Module([node], []) exec(compile(_exec, '<string>', 'exec'), g) # Execution went well, so append command console += command # Output captured standard out after statements text = s.getvalue() if text: clist = find_colors(text) if clist: colors.append(clist) console += '\n{}'.format(text) s.flush() except Exception: import traceback console += '{}\n{}'.format(command, traceback.format_exc()) # Failed for some reason, so quit break # If we got a result, output it as well if result is not None: clist = get_colors(result) if clist: colors.append(clist) console += '{}{}\n'.format('\n' if not text else '', str(result)) else: console += '\n' if not text else '' return console, colors
def comp(node): return compile(ast.Expression(node), filename=f_code.co_filename, mode='eval')
def test_hdl_expression(): """Test expressions.""" expr_1 = "PARAM-2" expr_2 = "PARAM_X+1" expr_3 = "a and ~b" hdl_expr_1 = HDLExpression(ast.parse(expr_1, mode="eval")) hdl_expr_2 = HDLExpression(ast.parse(expr_2, mode="eval")) hdl_expr_3 = HDLExpression(expr_3) print(hdl_expr_3.dumps()) sum = hdl_expr_1 + hdl_expr_2 neg = ~sum bool_neg = sum.bool_neg() bool_and = hdl_expr_1.bool_and(hdl_expr_2) bool_or = hdl_expr_1.bool_or(hdl_expr_2) print(sum.dumps()) print(neg.dumps()) print(bool_neg.dumps()) print(bool_and.dumps()) print(bool_or.dumps()) _ = hdl_expr_1 & 0x1 _ = 0x1 | hdl_expr_1 _ = 0x1 & hdl_expr_1 _ = 0x1 ^ hdl_expr_1 _ = hdl_expr_1 ^ 0x1 my_signal = HDLSignal("reg", "signal_a", size=2) _ = HDLExpression(HDLIntegerConstant(1)) _ = HDLExpression(1) _ = HDLExpression(my_signal) _ = HDLExpression(HDLSignalSlice(my_signal, 1)) _ = HDLExpression(my_signal[1:0]) # test reduction expr_a = HDLExpression("value_a") expr_b = HDLExpression("value_b") full_expr = expr_a << 0 | expr_b << 16 | HDLExpression(0) case_1 = ast.BinOp(left=ast.Constant(value=0), op=ast.BitOr(), right=ast.Name(id="VAR")) case_2 = ast.BinOp(left=ast.Constant(value=1), op=ast.Mult(), right=ast.Name(id="VAR")) case_3 = ast.BinOp(left=ast.Constant(value=0), op=ast.Mult(), right=ast.Name(id="VAR")) hdl_expr = HDLExpression(ast.Expression(body=case_1)) hdl_expr_2 = HDLExpression(ast.Expression(body=case_2)) hdl_expr_3 = HDLExpression(ast.Expression(body=case_3)) print(hdl_expr.dumps()) print(hdl_expr_2.dumps()) reduced_1 = HDLExpression._reduce_binop(case_1) hdl_expr = HDLExpression(ast.Expression(body=reduced_1)) print(hdl_expr.dumps()) reduced_2 = HDLExpression._reduce_binop(case_2) hdl_expr_2 = HDLExpression(ast.Expression(body=reduced_2)) print(hdl_expr_2.dumps()) reduced_3 = HDLExpression._reduce_binop(case_3) hdl_expr_3 = HDLExpression(ast.Expression(body=reduced_3)) print(hdl_expr_3.dumps()) print(full_expr.dumps()) full_expr.reduce_expr() print(full_expr.dumps())
def test_Expression(self): self.verify(ast.Expression(ast.Num(42)), '42')
def taskRunEval( tree, process, shell, env=None, extra_env=None, context=None, context_vals=None, pre_code="", expr_code="", name="", copy=True, tempname="_evaluation_object_", call=None, ): try: # Prepare code and mode ----------------------------------------------- if (expr_code and name) or (not expr_code and isinstance(tree, ast.Module)): mode = "exec" else: mode = "eval" tree = ast.Expression(tree) # Expression code takes precedence over tree code if expr_code: code = expr_code tree = ast.parse(code, mode=mode) else: code = compile(tree, "<script>", mode) # Set up environment -------------------------------------------------- # avoid deepy copy if specified, or just looking up variable by name if isinstance(tree, ast.Expression): tree = tree.body if not copy or (isinstance(tree, (ast.Name, ast.Subscript)) and isinstance(tree.ctx, ast.Load)): new_env = dict(get_env(shell.user_ns)) else: # might raise an error if object refuses pickle interface # used by deepcopy to restore class new_env = utils.copy_env(get_env(shell.user_ns)) if env is not None: new_env.update(deepcopy(env)) if extra_env is not None: new_env.update(deepcopy(extra_env)) if context is not None: set_context_vals(new_env, context, context_vals) # Execute code -------------------------------------------------------- # Run pre_code if specified if pre_code: exec(pre_code, new_env) if mode == "eval": obj = eval(code, new_env) else: exec(code, new_env) obj = "exec only" # If name given, get from new_env if name: try: obj = eval(name, new_env) except NameError: return UndefinedValue() # call object if dict with args and kwargs was passed if call is not None: obj = obj(*call["args"], **call["kwargs"]) # Set object as temp variable in original environment, so we can # later get its class, etc.., in order to extract it from process get_env(shell.user_ns)[tempname] = obj return str(obj) except Exception as e: return e
def test_module(self): m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))]) self.mod(m, "must have Load context", "single") m = ast.Expression(ast.Name("x", ast.Store())) self.mod(m, "must have Load context", "eval")
def as_ast(dct): """See https://docs.python.org/2/library/ast.html""" if dct['ast_type'] == "Module": return ast.Module(dct["body"]) elif dct['ast_type'] == "Interactive": return ast.Interactive(dct["body"]) elif dct['ast_type'] == "Expression": return ast.Expression(dct["body"]) elif dct['ast_type'] == "Suite": return ast.Suite(dct["body"]) elif dct['ast_type'] == "FunctionDef": return ast.FunctionDef(dct["name"], dct["args"], dct["body"], dct["decorator_list"]) elif dct['ast_type'] == "ClassDef": return ast.ClassDef(dct["name"], dct["bases"], dct["body"], dct["decorator_list"]) elif dct['ast_type'] == "Return": return ast.Return(dct["value"]) elif dct['ast_type'] == "Delete": return ast.Delete(dct["targets"]) elif dct['ast_type'] == "Assign": return ast.Assign(dct["targets"], dct["value"]) elif dct['ast_type'] == "AugAssign": return ast.AugAssign(dct["target"], dct["op"], dct["value"]) elif dct['ast_type'] == "Print": return ast.Print(dct["dest"], dct["values"], dct["nl"]) elif dct['ast_type'] == "For": return ast.For(dct["target"], dct["iter"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "While": return ast.While(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "If": return ast.If(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "With": return ast.With(dct["context_expr"], dct["optional_vars"], dct["body"]) elif dct['ast_type'] == "Raise": return ast.Raise(dct["type"], dct["inst"], dct["tback"]) elif dct['ast_type'] == "TryExcept": return ast.TryExcept(dct["body"], dct["handlers"], dct["orelse"]) elif dct['ast_type'] == "TryFinally": return ast.TryFinally(dct["body"], dct["finalbody"]) elif dct['ast_type'] == "Assert": return ast.Assert(dct["test"], dct["msg"]) elif dct['ast_type'] == "Import": return ast.Import(dct["names"]) elif dct['ast_type'] == "ImportFrom": return ast.ImportFrom(dct["module"], dct["names"], dct["level"]) elif dct['ast_type'] == "Exec": return ast.Exec(dct["body"], dct["globals"], dct["locals"]) elif dct['ast_type'] == "Global": return ast.Global(dct["names"]) elif dct['ast_type'] == "Expr": return ast.Expr(dct["value"]) elif dct['ast_type'] == "Pass": return ast.Pass() elif dct['ast_type'] == "Break": return ast.Break() elif dct['ast_type'] == "Continue": return ast.Continue() elif dct['ast_type'] == "BoolOp": return ast.BoolOp(dct["op"], dct["values"]) elif dct['ast_type'] == "BinOp": return ast.BinOp(dct["left"], dct["op"], dct["right"]) elif dct['ast_type'] == "UnaryOp": return ast.UnaryOp(dct["op"], dct["operand"]) elif dct['ast_type'] == "Lambda": return ast.Lambda(dct["args"], dct["body"]) elif dct['ast_type'] == "IfExp": return ast.IfExp(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "Dict": return ast.Dict(dct["keys"], dct["values"]) elif dct['ast_type'] == "Set": return ast.Set(dct["elts"]) elif dct['ast_type'] == "ListComp": return ast.ListComp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "SetComp": return ast.SetComp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "DictComp": return ast.DictComp(dct["key"], dct["value"], dct["generators"]) elif dct['ast_type'] == "GeneratorExp": return ast.GeneratorExp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "Yield": return ast.Yield(dct["value"]) elif dct['ast_type'] == "Compare": return ast.Compare(dct["left"], dct["ops"], dct["comparators"]) elif dct['ast_type'] == "Call": return ast.Call(dct["func"], dct["args"], dct["keywords"], dct["starargs"], dct["kwargs"]) elif dct['ast_type'] == "Repr": return ast.Repr(dct["value"]) elif dct['ast_type'] == "Num": return ast.Num(dct["n"]) elif dct['ast_type'] == "Str": # Converting to ASCII return ast.Str(dct["s"].encode('ascii', 'ignore')) elif dct['ast_type'] == "Attribute": return ast.Attribute(dct["value"], dct["attr"], dct["ctx"]) elif dct['ast_type'] == "Subscript": return ast.Subscript(dct["value"], dct["slice"], dct["ctx"]) elif dct['ast_type'] == "Name": return ast.Name(dct["id"], dct["ctx"]) elif dct['ast_type'] == "List": return ast.List(dct["elts"], dct["ctx"]) elif dct['ast_type'] == "Tuple": return ast.Tuple(dct["elts"], dct["ctx"]) elif dct['ast_type'] == "Load": return ast.Load() elif dct['ast_type'] == "Store": return ast.Store() elif dct['ast_type'] == "Del": return ast.Del() elif dct['ast_type'] == "AugLoad": return ast.AugLoad() elif dct['ast_type'] == "AugStore": return ast.AugStore() elif dct['ast_type'] == "Param": return ast.Param() elif dct['ast_type'] == "Ellipsis": return ast.Ellipsis() elif dct['ast_type'] == "Slice": return ast.Slice(dct["lower"], dct["upper"], dct["step"]) elif dct['ast_type'] == "ExtSlice": return ast.ExtSlice(dct["dims"]) elif dct['ast_type'] == "Index": return ast.Index(dct["value"]) elif dct['ast_type'] == "And": return ast.And() elif dct['ast_type'] == "Or": return ast.Or() elif dct['ast_type'] == "Add": return ast.Add() elif dct['ast_type'] == "Sub": return ast.Sub() elif dct['ast_type'] == "Mult": return ast.Mult() elif dct['ast_type'] == "Div": return ast.Div() elif dct['ast_type'] == "Mod": return ast.Mod() elif dct['ast_type'] == "Pow": return ast.Pow() elif dct['ast_type'] == "LShift": return ast.LShift() elif dct['ast_type'] == "RShift": return ast.RShift() elif dct['ast_type'] == "BitOr": return ast.BitOr() elif dct['ast_type'] == "BitXor": return ast.BitXor() elif dct['ast_type'] == "BitAnd": return ast.BitAnd() elif dct['ast_type'] == "FloorDiv": return ast.FloorDiv() elif dct['ast_type'] == "Invert": return ast.Invert() elif dct['ast_type'] == "Not": return ast.Not() elif dct['ast_type'] == "UAdd": return ast.UAdd() elif dct['ast_type'] == "USub": return ast.USub() elif dct['ast_type'] == "Eq": return ast.Eq() elif dct['ast_type'] == "NotEq": return ast.NotEq() elif dct['ast_type'] == "Lt": return ast.Lt() elif dct['ast_type'] == "LtE": return ast.LtE() elif dct['ast_type'] == "Gt": return ast.Gt() elif dct['ast_type'] == "GtE": return ast.GtE() elif dct['ast_type'] == "Is": return ast.Is() elif dct['ast_type'] == "IsNot": return ast.IsNot() elif dct['ast_type'] == "In": return ast.In() elif dct['ast_type'] == "NotIn": return ast.NotIn() elif dct['ast_type'] == "comprehension": return ast.comprehension(dct["target"], dct["iter"], dct["ifs"]) elif dct['ast_type'] == "ExceptHandler": return ast.ExceptHandler(dct["type"], dct["name"], dct["body"]) elif dct['ast_type'] == "arguments": return ast.arguments(dct["args"], dct["vararg"], dct["kwarg"], dct["defaults"]) elif dct['ast_type'] == "keyword": return ast.keyword(dct["arg"], dct["value"]) elif dct['ast_type'] == "alias": return ast.alias(dct["name"], dct["asname"]) else: return dct
def time(self, line='', cell=None, local_ns=None): """Time execution of a Python statement or expression. The CPU and wall clock times are printed, and the value of the expression (if any) is returned. Note that under Win32, system time is always reported as 0, since it can not be measured. This function can be used both as a line and cell magic: - In line mode you can time a single-line statement (though multiple ones can be chained with using semicolons). - In cell mode, you can time the cell body (a directly following statement raises an error). This function provides very basic timing functionality. Use the timeit magic for more controll over the measurement. Examples -------- :: In [1]: %time 2**128 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 Out[1]: 340282366920938463463374607431768211456L In [2]: n = 1000000 In [3]: %time sum(range(n)) CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s Wall time: 1.37 Out[3]: 499999500000L In [4]: %time print 'hello world' hello world CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 Note that the time needed by Python to compile the given expression will be reported if it is more than 0.1s. In this example, the actual exponentiation is done by Python at compilation time, so while the expression can take a noticeable amount of time to compute, that time is purely due to the compilation: In [5]: %time 3**9999; CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 s In [6]: %time 3**999999; CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 s Compiler : 0.78 s """ # fail immediately if the given expression can't be compiled if line and cell: raise UsageError("Can't use statement directly after '%%time'!") if cell: expr = self.shell.input_transformer_manager.transform_cell(cell) else: expr = self.shell.input_transformer_manager.transform_cell(line) # Minimum time above which parse time will be reported tp_min = 0.1 t0 = clock() expr_ast = ast.parse(expr) tp = clock() - t0 # Apply AST transformations expr_ast = self.shell.transform_ast(expr_ast) # Minimum time above which compilation time will be reported tc_min = 0.1 if len(expr_ast.body) == 1 and isinstance(expr_ast.body[0], ast.Expr): mode = 'eval' source = '<timed eval>' expr_ast = ast.Expression(expr_ast.body[0].value) else: mode = 'exec' source = '<timed exec>' t0 = clock() code = compile(expr_ast, source, mode) tc = clock() - t0 # skew measurement as little as possible glob = self.shell.user_ns wtime = time.time # time execution wall_st = wtime() if mode == 'eval': st = clock2() out = eval(code, glob, local_ns) end = clock2() else: st = clock2() exec code in glob, local_ns end = clock2() out = None wall_end = wtime() # Compute actual times and report wall_time = wall_end - wall_st cpu_user = end[0] - st[0] cpu_sys = end[1] - st[1] cpu_tot = cpu_user + cpu_sys # On windows cpu_sys is always zero, so no new information to the next print if sys.platform != 'win32': print "CPU times: user %s, sys: %s, total: %s" % \ (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)) print "Wall time: %s" % _format_time(wall_time) if tc > tc_min: print "Compiler : %s" % _format_time(tc) if tp > tp_min: print "Parser : %s" % _format_time(tp) return out
def visit_Expression(self, node): return ast.Expression(self.visit(node.body))
def convertExpr2Expression(Expr): Expr.lineno = 0 Expr.col_offset = 0 result = ast.Expression(Expr.value, lineno=0, col_offset=0) return result
def substitute_in_assignment(varname, new_env): assignment = find_line(varname) g = globals().copy() g.update(new_env) return eval(compile(ast.Expression(ast.parse(assignment).body[0].value), '', 'eval'), g)
def _filter(node): code = compile(ast.Expression(node), filename="<string>", mode="eval") return eval(code, env) != decorator
def _float16_or_double32(branch, context, leaf, is_float16, dims): if leaf.classname in ("TLeafF16", "TLeafD32"): title = leaf.member("fTitle") elif branch.streamer is not None: title = branch.streamer.title else: title = "" try: left = title.index("[") right = title.index("]") except (ValueError, AttributeError): low, high, num_bits = 0, 0, 0 else: source = title[left:right + 1] try: parsed = ast.parse(source).body[0].value except SyntaxError: raise UnknownInterpretation( "cannot parse streamer title {0} (as Python)".format( repr(source)), branch.file.file_path, branch.object_path, ) transformed = ast.Expression( _float16_double32_walk_ast(parsed, branch, source)) spec = eval(compile(transformed, repr(title), "eval")) if (len(spec) == 2 and uproot._util.isnum(spec[0]) and uproot._util.isnum(spec[1])): low, high = spec num_bits = None elif (len(spec) == 3 and uproot._util.isnum(spec[0]) and uproot._util.isnum(spec[1]) and uproot._util.isint(spec[2])): low, high, num_bits = spec else: raise UnknownInterpretation( "cannot interpret streamer title {0} as (low, high) or " "(low, high, num_bits)".format(repr(source)), branch.file.file_path, branch.object_path, ) if not is_float16: if num_bits == 0: return uproot.interpretation.numerical.AsDtype( numpy.dtype((">f4", dims)), numpy.dtype(("f8", dims))) elif num_bits is None: return uproot.interpretation.numerical.AsDouble32( low, high, 32, dims) else: return uproot.interpretation.numerical.AsDouble32( low, high, num_bits, dims) else: if num_bits == 0: return uproot.interpretation.numerical.AsFloat16( low, high, 12, dims) elif num_bits is None: return uproot.interpretation.numerical.AsFloat16( low, high, 32, dims) else: return uproot.interpretation.numerical.AsFloat16( low, high, num_bits, dims)
def eval_html(src, variables=None): tree = apply_dialects(src, "html") if len(tree.body) > 1 or not isinstance(tree.body[0], ast.Expr): raise ValueError(f"Expected a single expression, not {src!r}") code = compile(ast.Expression(tree.body[0].value), "<string>", "eval") return eval(code, {"html": html}, variables)