MCPcopy
hub / github.com/huggingface/transformers / replace_code

Function replace_code

utils/check_copies.py:473–495  ·  view source on GitHub ↗

Replace `code` by a pattern of the form `with X1->X2,Y1->Y2,Z1->Z2`. Args: code (`str`): The code to be modified. replace_pattern (`str`): The pattern used to modify `code`. Returns: `str`: The modified code.

(code: str, replace_pattern: str)

Source from the content-addressed store, hash-verified

471
472
473def replace_code(code: str, replace_pattern: str) -> str:
474 """Replace `code` by a pattern of the form `with X1->X2,Y1->Y2,Z1->Z2`.
475
476 Args:
477 code (`str`): The code to be modified.
478 replace_pattern (`str`): The pattern used to modify `code`.
479
480 Returns:
481 `str`: The modified code.
482 """
483 if len(replace_pattern) > 0:
484 patterns = replace_pattern.replace("with", "").split(",")
485 patterns = [_re_replace_pattern.search(p) for p in patterns]
486 for pattern in patterns:
487 if pattern is None:
488 continue
489 obj1, obj2, option = pattern.groups()
490 code = re.sub(obj1, obj2, code)
491 if option.strip() == "all-casing":
492 code = re.sub(obj1.lower(), obj2.lower(), code)
493 code = re.sub(obj1.upper(), obj2.upper(), code)
494
495 return code
496
497
498def find_code_and_splits(object_name: str, base_path: str, buffer: dict | None = None):

Callers 1

is_copy_consistentFunction · 0.85

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected