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

Function expand_sub

numpy/distutils/from_template.py:130–188  ·  view source on GitHub ↗
(substr, names)

Source from the content-addressed store, hash-verified

128
129template_name_re = re.compile(r'\A\s*(\w[\w\d]*)\s*\Z')
130def expand_sub(substr, names):
131 substr = substr.replace(r'\>', '@rightarrow@')
132 substr = substr.replace(r'\<', '@leftarrow@')
133 lnames = find_repl_patterns(substr)
134 substr = named_re.sub(r"<\1>", substr) # get rid of definition templates
135
136 def listrepl(mobj):
137 thelist = conv(mobj.group(1).replace(r'\,', '@comma@'))
138 if template_name_re.match(thelist):
139 return "<%s>" % (thelist)
140 name = None
141 for key in lnames.keys(): # see if list is already in dictionary
142 if lnames[key] == thelist:
143 name = key
144 if name is None: # this list is not in the dictionary yet
145 name = unique_key(lnames)
146 lnames[name] = thelist
147 return "<%s>" % name
148
149 substr = list_re.sub(listrepl, substr) # convert all lists to named templates
150 # newnames are constructed as needed
151
152 numsubs = None
153 base_rule = None
154 rules = {}
155 for r in template_re.findall(substr):
156 if r not in rules:
157 thelist = lnames.get(r, names.get(r, None))
158 if thelist is None:
159 raise ValueError('No replicates found for <%s>' % (r))
160 if r not in names and not thelist.startswith('_'):
161 names[r] = thelist
162 rule = [i.replace('@comma@', ',') for i in thelist.split(',')]
163 num = len(rule)
164
165 if numsubs is None:
166 numsubs = num
167 rules[r] = rule
168 base_rule = r
169 elif num == numsubs:
170 rules[r] = rule
171 else:
172 print("Mismatch in number of replacements (base <%s=%s>)"
173 " for <%s=%s>. Ignoring." %
174 (base_rule, ','.join(rules[base_rule]), r, thelist))
175 if not rules:
176 return substr
177
178 def namerepl(mobj):
179 name = mobj.group(1)
180 return rules.get(name, (k+1)*[name])[k]
181
182 newstr = ''
183 for k in range(numsubs):
184 newstr += template_re.sub(namerepl, substr) + '\n\n'
185
186 newstr = newstr.replace('@rightarrow@', '>')
187 newstr = newstr.replace('@leftarrow@', '<')

Callers 1

process_strFunction · 0.70

Calls 7

printFunction · 0.85
replaceMethod · 0.80
startswithMethod · 0.80
find_repl_patternsFunction · 0.70
getMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected