MCPcopy
hub / github.com/pydata/xarray / _re_compile

Method _re_compile

xarray/core/accessor_str.py:243–281  ·  view source on GitHub ↗
(
        self,
        *,
        pat: str | bytes | Pattern | Any,
        flags: int = 0,
        case: bool | None = None,
    )

Source from the content-addressed store, hash-verified

241 )
242
243 def _re_compile(
244 self,
245 *,
246 pat: str | bytes | Pattern | Any,
247 flags: int = 0,
248 case: bool | None = None,
249 ) -> Pattern | Any:
250 is_compiled_re = isinstance(pat, re.Pattern)
251
252 if is_compiled_re and flags != 0:
253 raise ValueError("Flags cannot be set when pat is a compiled regex.")
254
255 if is_compiled_re and case is not None:
256 raise ValueError("Case cannot be set when pat is a compiled regex.")
257
258 if is_compiled_re:
259 # no-op, needed to tell mypy this isn't a string
260 return re.compile(pat)
261
262 if case is None:
263 case = True
264
265 # The case is handled by the re flags internally.
266 # Add it to the flags if necessary.
267 if not case:
268 flags |= re.IGNORECASE
269
270 if getattr(pat, "dtype", None) != np.object_:
271 pat = self._stringify(pat)
272
273 def func(x):
274 return re.compile(x, flags=flags)
275
276 if isinstance(pat, np.ndarray):
277 # apply_ufunc doesn't work for numpy arrays with output object dtypes
278 func_ = np.vectorize(func)
279 return func_(pat)
280 else:
281 return _apply_str_ufunc(func=func, obj=pat, dtype=np.object_)
282
283 def len(self) -> T_DataArray:
284 """

Callers 10

countMethod · 0.95
containsMethod · 0.95
matchMethod · 0.95
replaceMethod · 0.95
extractMethod · 0.95
extractallMethod · 0.95
findallMethod · 0.95
test_extract_broadcastFunction · 0.80
test_findall_broadcastFunction · 0.80

Calls 2

_stringifyMethod · 0.95
_apply_str_ufuncFunction · 0.85

Tested by 3

test_extract_broadcastFunction · 0.64
test_findall_broadcastFunction · 0.64