Index: darg.py ================================================================== --- darg.py +++ darg.py @@ -6,12 +6,10 @@ # by Olivier R. # License: MPL 2 import re -import traceback - class DARG: """DIRECT ACYCLIC RULE GRAPH""" # This code is inspired from Steve Hanov’s DAWG, 2011. (http://stevehanov.ca/blog/index.php?id=115) @@ -55,11 +53,11 @@ # Check the lUncheckedNodes for redundant nodes, proceeding from last # one down to the common prefix size. Then truncate the list at that point. self._minimize(nCommonPrefix) # add the suffix, starting from the correct node mid-way through the graph - if len(self.lUncheckedNodes) == 0: + if not self.lUncheckedNodes: oNode = self.oRoot else: oNode = self.lUncheckedNodes[-1][2] iToken = nCommonPrefix @@ -127,32 +125,32 @@ dKeyTrans[nKey] = i # replace keys dNewGraph = {} for nKey, dVal in dGraph.items(): dNewGraph[dKeyTrans[nKey]] = dVal - for nKey, dVal in dGraph.items(): + for _, dVal in dGraph.items(): for sArc, val in dVal.items(): - if type(val) is int: + if isinstance(val, int): dVal[sArc] = dKeyTrans[val] else: - for sArc, nKey in val.items(): - val[sArc] = dKeyTrans[nKey] + for sArc2, nKey in val.items(): + val[sArc2] = dKeyTrans[nKey] return dNewGraph def _sortActions (self, dGraph): "when a pattern is found, several actions may be launched, and it must be performed in a certain order" - for nKey, dVal in dGraph.items(): + for _, dVal in dGraph.items(): if "" in dVal: - for sLineId, nKey in dVal[""].items(): + for _, nKey in dVal[""].items(): # we change the dictionary of actions in a list of actions (values of dictionary all points to the final node) if isinstance(dGraph[nKey], dict): dGraph[nKey] = sorted(dGraph[nKey].keys()) def _checkRegexes (self, dGraph): "check validity of regexes" aRegex = set() - for nKey, dVal in dGraph.items(): + for _, dVal in dGraph.items(): if "" in dVal: for sRegex in dVal[""]: if sRegex not in aRegex: self._checkRegex(sRegex) aRegex.add(sRegex) @@ -171,19 +169,19 @@ if not sNegPattern: print("# Warning! Empty negpattern:", sRegex) re.compile(sPattern) if sNegPattern != "*": re.compile(sNegPattern) - except: + except re.error: print("# Error. Wrong regex:", sRegex) exit() else: try: if not sRegex: print("# Warning! Empty pattern:", sRegex) re.compile(sRegex) - except: + except re.error: print("# Error. Wrong regex:", sRegex) exit() class Node: