def __new__(cls, name, expr, replace=False): # code efound = expr in [ Variable.variables[key].expr for key in Variable.variables ] if efound: key = [ key for key in Variable.variables if expr in Variable.variables[key].expr ] logger.info("Expression '%s' already exists for key %s", expr, key) return else: if replace or not name in Variable.variables: if not valid_name(name): logger.info("Invalid variable key: %s", name) return try: result = parser.expr(expr) except: logger.info("Invalid expression: %s", expr) return return super(Variable, cls).__new__(cls) else: logger.info("Key %s already exists", name)
def vwalk(allv, vname): vxlag, root, plist, lag = vparse(vname) if root in Variable.variables: root_expr = Variable.variables[root].expr expr = vsub(vname, root_expr) av = allvars(expr) for v in av: vwalk(allv, v) else: for p in plist: if valid_name(p): vwalk(allv, p) allv.append(vname) return allv
def allvars(expr): r"""Get the list of valid names in the expression. Parameters ---------- expr : str A valid expression conforming to the Variable Definition Language. Returns ------- vlist : list List of valid variable names. """ regex = re.compile('\w+') items = regex.findall(expr) vlist = [] for item in items: if valid_name(item): vlist.append(item) return vlist