Repository: 3b1b/manim
File: manimlib/mobject/svg/labelled_string.py (Python)

Code before:
            self.set_fill(self.base_color)
        for submob, label in zip(self.submobjects, submob_labels):
            submob.label = label

    def pre_parse(self) -> None:
        self.string_len = len(self.string)
        self.full_span = (0, self.string_len)

    def parse(self) -> None:
        self.command_repl_items = self.get_command_repl_items()
        self.command_spans = self.get_command_spans()
        self.ignored_spans = self.get_ignored_spans()
        self.skipped_spans = self.get_skipped_spans()
        self.internal_specified_spans = self.get_internal_specified_spans()
        self.external_specified_spans = self.get_external_specified_spans()
        self.specified_spans = self.get_specified_spans()
        self.label_span_list = self.get_label_span_list()
        self.check_overlapping()

    # Toolkits

    def get_substr(self, span: Span) -> str:
        return self.string[slice(*span)]

    def handle_regex_method(func):
        def wrapper(self, pattern, pos=0, endpos=9223372036854775807):
            return func()(
                re.compile(pattern), self.string, pos=pos, endpos=endpos
            )
        return wrapper

    @handle_regex_method
    def finditer():
        return re.Pattern.finditer

    @handle_regex_method
    def search():
        return re.Pattern.search

    @handle_regex_method
    def match():
        return re.Pattern.match

    def find_spans(self, pattern: str) -> list[Span]:
        return [match_obj.span() for match_obj in self.finditer(pattern)]

    def find_substr(self, substr: str) -> list[Span]:
        if not substr:
            return []
        return self.find_spans(re.escape(substr))


Review comment (suggestion):
please add `@wraps(func)` between these two lines, where `wraps` is imported from `functools`. this is more standardized.

Code after:
            self.set_fill(self.base_color)
        for submob, label in zip(self.submobjects, submob_labels):
            submob.label = label

    def pre_parse(self) -> None:
        self.string_len = len(self.string)
        self.full_span = (0, self.string_len)

    def parse(self) -> None:
        self.command_repl_items = self.get_command_repl_items()
        self.command_spans = self.get_command_spans()
        self.ignored_spans = self.get_ignored_spans()
        self.skipped_spans = self.get_skipped_spans()
        self.internal_specified_spans = self.get_internal_specified_spans()
        self.external_specified_spans = self.get_external_specified_spans()
        self.specified_spans = self.get_specified_spans()
        self.label_span_list = self.get_label_span_list()
        self.check_overlapping()

    # Toolkits

    def get_substr(self, span: Span) -> str:
        return self.string[slice(*span)]

    def finditer(self, pattern, **kwargs):
        return re.compile(pattern).finditer(self.string, **kwargs)

    def search(self, pattern, **kwargs):
        return re.compile(pattern).search(self.string, **kwargs)

    def match(self, pattern, **kwargs):
        return re.compile(pattern).match(self.string, **kwargs)

    def find_spans(self, pattern: str) -> list[Span]:
        return [match_obj.span() for match_obj in self.finditer(pattern)]

    def find_substr(self, substr: str) -> list[Span]:
        if not substr:
            return []
        return self.find_spans(re.escape(substr))

    def find_substrs(self, substrs: list[str]) -> list[Span]:
        return list(it.chain(*[
            self.find_substr(substr)
            for substr in remove_list_redundancies(substrs)
        ]))

    @staticmethod
    def get_neighbouring_pairs(iterable: list) -> list[tuple]:
        return list(zip(iterable[:-1], iterable[1:]))