comparison genshi/template/match.py @ 701:52a597419c0d experimental-match-fastpaths

minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
author aflett
date Fri, 28 Mar 2008 17:05:04 +0000
parents 1240ada13334
children af57b12e3dd2
comparison
equal deleted inserted replaced
690:1240ada13334 701:52a597419c0d
47 47
48 self.current_index = 0 48 self.current_index = 0
49 49
50 if parent is None: 50 if parent is None:
51 # merely for indexing. Note that this is shared between 51 # merely for indexing. Note that this is shared between
52 # all MatchSets that share the same root parent. We don't have to worry about exclusions here 52 # all MatchSets that share the same root parent. We don't
53 self.match_order = [] 53 # have to worry about exclusions here
54 self.match_order = {}
54 55
55 # tag_templates are match templates whose path are simply 56 # tag_templates are match templates whose path are simply
56 # a tag, like "body" or "img" 57 # a tag, like "body" or "img"
57 self.tag_templates = {} 58 self.tag_templates = {}
58 59
82 """ 83 """
83 match_template is a tuple the form 84 match_template is a tuple the form
84 test, path, template, hints, namespace, directives 85 test, path, template, hints, namespace, directives
85 """ 86 """
86 87
87 self.match_order.append(match_template) 88 # match_templates are currently tuples that contain unhashable
89 # objects. So we'll use id() for now.
90 self.match_order[id(match_template)] = len(self.match_order)
88 91
89 path = match_template[1] 92 path = match_template[1]
90 93
91 self.current_index += 1 94 self.current_index += 1
92 if is_simple_path(path): 95 if is_simple_path(path):
115 if not template_list: 118 if not template_list:
116 del self.tag_templates[tag_name] 119 del self.tag_templates[tag_name]
117 120
118 else: 121 else:
119 self.other_templates.remove(match_template) 122 self.other_templates.remove(match_template)
123
124 # clean up match_order
125 del self.match_order[id(match_template)]
120 126
121 def single_match(cls, match_template): 127 def single_match(cls, match_template):
122 """ 128 """
123 Factory for creating a MatchSet with just one match 129 Factory for creating a MatchSet with just one match
124 """ 130 """
163 # remove exclusions 169 # remove exclusions
164 matches = filter(lambda template: template not in self.exclude, 170 matches = filter(lambda template: template not in self.exclude,
165 self.find_raw_matches(event)) 171 self.find_raw_matches(event))
166 172
167 # sort the results according to the order they were added 173 # sort the results according to the order they were added
168 return sorted(matches, key=self.match_order.index) 174 return sorted(matches, key=lambda v: self.match_order[id(v)])
169 175
170 def __nonzero__(self): 176 def __nonzero__(self):
171 """ 177 """
172 allow this to behave as a list 178 allow this to behave as a list
173 """ 179 """
Copyright (C) 2012-2017 Edgewall Software