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

Function expand_sub

numpy/f2py/_src_pyf.py:126–183  ·  view source on GitHub ↗
(substr, names)

Source from the content-addressed store, hash-verified

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

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