Skip to content

Reference

Code dataclass

Dataclass representing code object with attribute language and code

Attributes:

Name Type Description
code Union[str, None]

The code text

language Union[str, None]

the programming language. The programming language is guessed

Source code in codesnipper/codesnipper.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
@dataclass
class Code:
    """
    Dataclass representing code object with attribute `language` and `code`

    Attributes:
        code: The code text
        language: the programming language. The programming language is guessed
        based on the text after first triple backticks.
    """

    code: Union[str, None]
    language: Union[str, None]

CodeSnipper

Source code in codesnipper/codesnipper.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class CodeSnipper:
    @validate_call
    def __init__(self, text: Union[str, None] = None, file_path: Union[str, None] = None):
        """Reads for code snippets within a text. A section is considered a code snippet if
        it's text is enclosed in triple backticks.

        Args:
            text: text to check code in
            file_path: filepath from where to read the text

        Raises:
            ValueError: If both `text` and `file_path` are provided
            ValueError: If none of `text` and `file_path` are provided
        """
        self.text = text
        self.file_path = file_path

        if self.text is None:
            if self.file_path is not None:
                with open(self.file_path) as file:
                    self.text = file.read()
            else:
                raise ValueError("both `text` and `file_path` cannot be `None`. At least one is required")
        else:
            if self.file_path is not None:
                raise ValueError("both text and file_path are provided. Only one is required")

    def _code_sections(self, pattern: str = "(?<=```)(.*?)(?=```)") -> List[Any]:
        matches = re.findall(pattern, self.text, flags=re.S)  # type: ignore
        codes = []
        for i in range(0, len(matches), 2):
            codes.append(matches[i])
        return codes

    def _parse_code_sections(self, matches: List[Any]) -> List[Code]:
        codes = []
        for match in matches:
            if match:
                language = re.search(r"^ *\w*?\n", match, re.S)
                if language is not None:
                    language = language.group(0).strip()
                    code = re.sub(language, "", match, 1).strip()
                else:
                    code = match.strip()
                if not language:
                    language = None
                c = Code(code=code, language=language)
                codes.append(c)
        return codes

    def codes(self) -> List[Code]:
        sections = self._parse_code_sections(self._code_sections())
        return sections

__init__(text=None, file_path=None)

Reads for code snippets within a text. A section is considered a code snippet if it's text is enclosed in triple backticks.

Parameters:

Name Type Description Default
text Union[str, None]

text to check code in

None
file_path Union[str, None]

filepath from where to read the text

None

Raises:

Type Description
ValueError

If both text and file_path are provided

ValueError

If none of text and file_path are provided

Source code in codesnipper/codesnipper.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@validate_call
def __init__(self, text: Union[str, None] = None, file_path: Union[str, None] = None):
    """Reads for code snippets within a text. A section is considered a code snippet if
    it's text is enclosed in triple backticks.

    Args:
        text: text to check code in
        file_path: filepath from where to read the text

    Raises:
        ValueError: If both `text` and `file_path` are provided
        ValueError: If none of `text` and `file_path` are provided
    """
    self.text = text
    self.file_path = file_path

    if self.text is None:
        if self.file_path is not None:
            with open(self.file_path) as file:
                self.text = file.read()
        else:
            raise ValueError("both `text` and `file_path` cannot be `None`. At least one is required")
    else:
        if self.file_path is not None:
            raise ValueError("both text and file_path are provided. Only one is required")