MCPcopy Create free account
hub / github.com/numpy/numpy / parse_loop_header

Function parse_loop_header

numpy/distutils/conv_template.py:158–208  ·  view source on GitHub ↗

Find all named replacements in the header Returns a list of dictionaries, one for each loop iteration, where each key is a name to be substituted and the corresponding value is the replacement string. Also return a list of exclusions. The exclusions are dictionaries of key va

(loophead)

Source from the content-addressed store, hash-verified

156exclude_vars_re = re.compile(r"(\w*)=(\w*)")
157exclude_re = re.compile(":exclude:")
158def parse_loop_header(loophead) :
159 """Find all named replacements in the header
160
161 Returns a list of dictionaries, one for each loop iteration,
162 where each key is a name to be substituted and the corresponding
163 value is the replacement string.
164
165 Also return a list of exclusions. The exclusions are dictionaries
166 of key value pairs. There can be more than one exclusion.
167 [{'var1':'value1', 'var2', 'value2'[,...]}, ...]
168
169 """
170 # Strip out '\n' and leading '*', if any, in continuation lines.
171 # This should not effect code previous to this change as
172 # continuation lines were not allowed.
173 loophead = stripast.sub("", loophead)
174 # parse out the names and lists of values
175 names = []
176 reps = named_re.findall(loophead)
177 nsub = None
178 for rep in reps:
179 name = rep[0]
180 vals = parse_values(rep[1])
181 size = len(vals)
182 if nsub is None :
183 nsub = size
184 elif nsub != size :
185 msg = "Mismatch in number of values, %d != %d\n%s = %s"
186 raise ValueError(msg % (nsub, size, name, vals))
187 names.append((name, vals))
188
189
190 # Find any exclude variables
191 excludes = []
192
193 for obj in exclude_re.finditer(loophead):
194 span = obj.span()
195 # find next newline
196 endline = loophead.find('\n', span[1])
197 substr = loophead[span[1]:endline]
198 ex_names = exclude_vars_re.findall(substr)
199 excludes.append(dict(ex_names))
200
201 # generate list of dictionaries, one for each template iteration
202 dlist = []
203 if nsub is None :
204 raise ValueError("No substitution variables found")
205 for i in range(nsub):
206 tmp = {name: vals[i] for name, vals in names}
207 dlist.append(tmp)
208 return dlist
209
210replace_re = re.compile(r"@(\w+)@")
211def parse_string(astr, env, level, line) :

Callers 1

parse_stringFunction · 0.85

Calls 2

parse_valuesFunction · 0.85
findMethod · 0.80

Tested by

no test coverage detected