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

Function replace_parenthesis

numpy/f2py/symbolic.py:1212–1252  ·  view source on GitHub ↗

Replace substrings of input that are enclosed in parenthesis. Return a new string and a mapping of replacements.

(s)

Source from the content-addressed store, hash-verified

1210
1211
1212def replace_parenthesis(s):
1213 """Replace substrings of input that are enclosed in parenthesis.
1214
1215 Return a new string and a mapping of replacements.
1216 """
1217 # Find a parenthesis pair that appears first.
1218
1219 # Fortran deliminator are `(`, `)`, `[`, `]`, `(/', '/)`, `/`.
1220 # We don't handle `/` deliminator because it is not a part of an
1221 # expression.
1222 left, right = None, None
1223 mn_i = len(s)
1224 for left_, right_ in (('(/', '/)'),
1225 '()',
1226 '{}', # to support C literal structs
1227 '[]'):
1228 i = s.find(left_)
1229 if i == -1:
1230 continue
1231 if i < mn_i:
1232 mn_i = i
1233 left, right = left_, right_
1234
1235 if left is None:
1236 return s, {}
1237
1238 i = mn_i
1239 j = s.find(right, i)
1240
1241 while s.count(left, i + 1, j) != s.count(right, i + 1, j):
1242 j = s.find(right, j + 1)
1243 if j == -1:
1244 raise ValueError(f'Mismatch of {left+right} parenthesis in {s!r}')
1245
1246 p = {'(': 'ROUND', '[': 'SQUARE', '{': 'CURLY', '(/': 'ROUNDDIV'}[left]
1247
1248 k = f'@__f2py_PARENTHESIS_{p}_{COUNTER.__next__()}@'
1249 v = s[i+len(left):j]
1250 r, d = replace_parenthesis(s[j+len(right):])
1251 d[k] = v
1252 return s[:i] + k + r, d
1253
1254
1255def _get_parenthesis_kind(s):

Callers 1

processMethod · 0.85

Calls 3

findMethod · 0.80
countMethod · 0.45
__next__Method · 0.45

Tested by

no test coverage detected