diff options
author | Stefan Behnel <scoder@users.berlios.de> | 2009-03-13 13:45:28 +0100 |
---|---|---|
committer | Stefan Behnel <scoder@users.berlios.de> | 2009-03-13 13:45:28 +0100 |
commit | df9b64317d9fadc2bdb0c23f23e31bc54781136b (patch) | |
tree | 9a44d9940882774afbc0a3f473b9868bccfcedbc /Cython/Plex | |
parent | fb4dc542ba4daeeeb1171a66aef8ce5263826091 (diff) | |
download | cython-df9b64317d9fadc2bdb0c23f23e31bc54781136b.tar.gz |
major cleanup to fix Py3 code issues (found by 2to3 and 'python2.6 -3')
Diffstat (limited to 'Cython/Plex')
-rw-r--r-- | Cython/Plex/DFA.py | 18 | ||||
-rw-r--r-- | Cython/Plex/Machines.py | 15 | ||||
-rw-r--r-- | Cython/Plex/Regexps.py | 13 | ||||
-rw-r--r-- | Cython/Plex/Traditional.py | 4 | ||||
-rw-r--r-- | Cython/Plex/Transitions.py | 21 |
5 files changed, 31 insertions, 40 deletions
diff --git a/Cython/Plex/DFA.py b/Cython/Plex/DFA.py index a24d3cea2..6d0085a89 100644 --- a/Cython/Plex/DFA.py +++ b/Cython/Plex/DFA.py @@ -29,18 +29,18 @@ def nfa_to_dfa(old_machine, debug = None): # Seed the process using the initial states of the old machine. # Make the corresponding new states into initial states of the new # machine with the same names. - for (key, old_state) in old_machine.initial_states.items(): + for (key, old_state) in old_machine.initial_states.iteritems(): new_state = state_map.old_to_new(epsilon_closure(old_state)) new_machine.make_initial_state(key, new_state) # Tricky bit here: we add things to the end of this list while we're # iterating over it. The iteration stops when closure is achieved. for new_state in new_machine.states: transitions = TransitionMap() - for old_state in state_map.new_to_old(new_state).keys(): - for event, old_target_states in old_state.transitions.items(): + for old_state in state_map.new_to_old(new_state): + for event, old_target_states in old_state.transitions.iteritems(): if event and old_target_states: transitions.add_set(event, set_epsilon_closure(old_target_states)) - for event, old_states in transitions.items(): + for event, old_states in transitions.iteritems(): new_machine.add_transitions(new_state, event, state_map.old_to_new(old_states)) if debug: debug.write("\n===== State Mapping =====\n") @@ -53,8 +53,8 @@ def set_epsilon_closure(state_set): closures of its member states. """ result = {} - for state1 in state_set.keys(): - for state2 in epsilon_closure(state1).keys(): + for state1 in state_set: + for state2 in epsilon_closure(state1): result[state2] = 1 return result @@ -80,7 +80,7 @@ def add_to_epsilon_closure(state_set, state): state_set[state] = 1 state_set_2 = state.transitions.get_epsilon() if state_set_2: - for state2 in state_set_2.keys(): + for state2 in state_set_2: add_to_epsilon_closure(state_set, state2) class StateMap(object): @@ -119,7 +119,7 @@ class StateMap(object): def highest_priority_action(self, state_set): best_action = None best_priority = LOWEST_PRIORITY - for state in state_set.keys(): + for state in state_set: priority = state.action_priority if priority > best_priority: best_action = state.action @@ -142,7 +142,7 @@ class StateMap(object): Convert a set of states into a uniquified sorted tuple suitable for use as a dictionary key. """ - lst = state_set.keys() + lst = list(state_set) lst.sort() return tuple(lst) diff --git a/Cython/Plex/Machines.py b/Cython/Plex/Machines.py index a9c69c638..7bb068ef8 100644 --- a/Cython/Plex/Machines.py +++ b/Cython/Plex/Machines.py @@ -6,7 +6,6 @@ # #======================================================================= -import string import sys from sys import maxint from types import TupleType @@ -54,7 +53,7 @@ class Machine(object): file.write("Plex.Machine:\n") if self.initial_states is not None: file.write(" Initial states:\n") - for (name, state) in self.initial_states.items(): + for (name, state) in self.initial_states.iteritems(): file.write(" '%s': %d\n" % (name, state.number)) for s in self.states: s.dump(file) @@ -108,7 +107,6 @@ class Node(object): return "State %d" % self.number def dump(self, file): - import string # Header file.write(" State %d:\n" % self.number) # Transitions @@ -143,11 +141,11 @@ class FastMachine(object): for old_state in old_machine.states: new_state = self.new_state() old_to_new[old_state] = new_state - for name, old_state in old_machine.initial_states.items(): + for name, old_state in old_machine.initial_states.iteritems(): initial_states[name] = old_to_new[old_state] for old_state in old_machine.states: new_state = old_to_new[old_state] - for event, old_state_set in old_state.transitions.items(): + for event, old_state_set in old_state.transitions.iteritems(): if old_state_set: new_state[event] = old_to_new[old_state_set.keys()[0]] else: @@ -188,13 +186,12 @@ class FastMachine(object): def dump(self, file): file.write("Plex.FastMachine:\n") file.write(" Initial states:\n") - for name, state in self.initial_states.items(): + for name, state in self.initial_states.iteritems(): file.write(" %s: %s\n" % (repr(name), state['number'])) for state in self.states: self.dump_state(state, file) def dump_state(self, state, file): - import string # Header file.write(" State %d:\n" % state['number']) # Transitions @@ -207,7 +204,7 @@ class FastMachine(object): def dump_transitions(self, state, file): chars_leading_to_state = {} special_to_state = {} - for (c, s) in state.items(): + for (c, s) in state.iteritems(): if len(c) == 1: chars = chars_leading_to_state.get(id(s), None) if chars is None: @@ -249,7 +246,7 @@ class FastMachine(object): return tuple(result) def ranges_to_string(self, range_list): - return string.join(map(self.range_to_string, range_list), ",") + return ','.join(map(self.range_to_string, range_list)) def range_to_string(self, range_tuple): (c1, c2) = range_tuple diff --git a/Cython/Plex/Regexps.py b/Cython/Plex/Regexps.py index 90dbe9345..cee03dbf0 100644 --- a/Cython/Plex/Regexps.py +++ b/Cython/Plex/Regexps.py @@ -7,7 +7,6 @@ #======================================================================= import array -import string import types from sys import maxint @@ -330,7 +329,7 @@ class Seq(RE): match_bol = re.match_nl or (match_bol and re.nullable) def calc_str(self): - return "Seq(%s)" % string.join(map(str, self.re_list), ",") + return "Seq(%s)" % ','.join(map(str, self.re_list)) class Alt(RE): @@ -369,7 +368,7 @@ class Alt(RE): re.build_machine(m, initial_state, final_state, 0, nocase) def calc_str(self): - return "Alt(%s)" % string.join(map(str, self.re_list), ",") + return "Alt(%s)" % ','.join(map(str, self.re_list)) class Rep1(RE): @@ -437,7 +436,7 @@ def Str1(s): """ Str1(s) is an RE which matches the literal string |s|. """ - result = apply(Seq, tuple(map(Char, s))) + result = Seq(*tuple(map(Char, s))) result.str = "Str(%s)" % repr(s) return result @@ -449,8 +448,8 @@ def Str(*strs): if len(strs) == 1: return Str1(strs[0]) else: - result = apply(Alt, tuple(map(Str1, strs))) - result.str = "Str(%s)" % string.join(map(repr, strs), ",") + result = Alt(*tuple(map(Str1, strs))) + result.str = "Str(%s)" % ','.join(map(repr, strs)) return result def Any(s): @@ -495,7 +494,7 @@ def Range(s1, s2 = None): ranges = [] for i in range(0, len(s1), 2): ranges.append(CodeRange(ord(s1[i]), ord(s1[i+1]) + 1)) - result = apply(Alt, tuple(ranges)) + result = Alt(*ranges) result.str = "Range(%s)" % repr(s1) return result diff --git a/Cython/Plex/Traditional.py b/Cython/Plex/Traditional.py index 5d8f91300..fe13d3a1b 100644 --- a/Cython/Plex/Traditional.py +++ b/Cython/Plex/Traditional.py @@ -41,7 +41,7 @@ class REParser(object): while self.c == '|': self.next() re_list.append(self.parse_seq()) - re = apply(Alt, tuple(re_list)) + re = Alt(*re_list) return re def parse_seq(self): @@ -49,7 +49,7 @@ class REParser(object): re_list = [] while not self.end and not self.c in "|)": re_list.append(self.parse_mod()) - return apply(Seq, tuple(re_list)) + return Seq(*re_list) def parse_mod(self): """Parse a primitive regexp followed by *, +, ? modifiers.""" diff --git a/Cython/Plex/Transitions.py b/Cython/Plex/Transitions.py index 4119e71b8..66f7a3160 100644 --- a/Cython/Plex/Transitions.py +++ b/Cython/Plex/Transitions.py @@ -6,7 +6,6 @@ # from copy import copy -import string from sys import maxint from types import TupleType @@ -89,10 +88,10 @@ class TransitionMap(object): """ return self.special.get('', none) - def items(self, + def iteritems(self, len = len): """ - Return the mapping as a list of ((code1, code2), state_set) and + Return the mapping as an iterable of ((code1, code2), state_set) and (special_event, state_set) pairs. """ result = [] @@ -108,10 +107,10 @@ class TransitionMap(object): result.append(((code0, code1), set)) code0 = code1 i = i + 2 - for event, set in self.special.items(): + for event, set in self.special.iteritems(): if set: result.append((event, set)) - return result + return iter(result) # ------------------- Private methods -------------------- @@ -178,10 +177,10 @@ class TransitionMap(object): map_strs.append(state_set_str(map[i])) i = i + 1 special_strs = {} - for event, set in self.special.items(): + for event, set in self.special.iteritems(): special_strs[event] = state_set_str(set) return "[%s]+%s" % ( - string.join(map_strs, ","), + ','.join(map_strs), special_strs ) @@ -200,7 +199,7 @@ class TransitionMap(object): while i < n: self.dump_range(map[i], map[i + 2], map[i + 1], file) i = i + 2 - for event, set in self.special.items(): + for event, set in self.special.iteritems(): if set: if not event: event = 'empty' @@ -243,11 +242,7 @@ class TransitionMap(object): # set1[state] = 1 def state_set_str(set): - state_list = set.keys() - str_list = [] - for state in state_list: - str_list.append("S%d" % state.number) - return "[%s]" % string.join(str_list, ",") + return "[%s]" % ','.join(["S%d" % state.number for state in set]) |