redlines.document

 1import os
 2from abc import ABC, abstractmethod
 3
 4__all__: tuple[str, ...] = ("Document", "PlainTextFile")
 5
 6
 7class Document(ABC):
 8    """
 9    An abstract base class used as a common data interchange with the redlines with formats other than python text strings.
10    To see a basic implementation, you can look at the `PlainTextFile` class for text files
11
12    |Supported File Formats | Class |
13    |---| --- |
14    |Plain Text files | `PlainTextFile` |
15    |PDF files | `PDFFile` (requires redlines[pdf]) |
16    """
17
18    @property
19    @abstractmethod
20    def text(self) -> str:
21        """
22        This property is used by the redlines library to obtain the text to compare. Implement this method to return
23        the text to be compared by redlines.
24        """
25        pass
26
27
28class PlainTextFile(Document):
29    """
30    Use this class so that Redlines can read plain text files.
31
32    ```python
33    from redlines import PlainTextFile
34
35    source = PlainTextFile("tests/documents/PlainTextFile/source.txt")
36    test = PlainTextFile("tests/documents/PlainTextFile/test.txt")
37
38    redline = Redlines(source, test)
39    assert (
40        redline.output_markdown
41        == "The quick brown fox <span style='color:red;font-weight:700;text-decoration:line-through;'>jumps over </span><span style='color:green;font-weight:700;'>walks past </span>the lazy dog."
42    )
43    ```
44
45    """
46
47    @property
48    def text(self) -> str:
49        """
50        :return: The text of the file referred to when the Document was created.
51        """
52        return self._text
53
54    def __init__(self, file_path: str | bytes | os.PathLike[str]) -> None:
55        """
56        Use this class so that Redlines can read plain text files.
57
58        :param file_path: Path to the text file.
59        :type file_path: str | bytes | os.PathLike[str]
60        """
61        with open(file_path) as f:
62            self._text = f.read()
class Document(abc.ABC):
 8class Document(ABC):
 9    """
10    An abstract base class used as a common data interchange with the redlines with formats other than python text strings.
11    To see a basic implementation, you can look at the `PlainTextFile` class for text files
12
13    |Supported File Formats | Class |
14    |---| --- |
15    |Plain Text files | `PlainTextFile` |
16    |PDF files | `PDFFile` (requires redlines[pdf]) |
17    """
18
19    @property
20    @abstractmethod
21    def text(self) -> str:
22        """
23        This property is used by the redlines library to obtain the text to compare. Implement this method to return
24        the text to be compared by redlines.
25        """
26        pass

An abstract base class used as a common data interchange with the redlines with formats other than python text strings. To see a basic implementation, you can look at the PlainTextFile class for text files

Supported File Formats Class
Plain Text files PlainTextFile
PDF files PDFFile (requires redlines[pdf])
text: str
19    @property
20    @abstractmethod
21    def text(self) -> str:
22        """
23        This property is used by the redlines library to obtain the text to compare. Implement this method to return
24        the text to be compared by redlines.
25        """
26        pass

This property is used by the redlines library to obtain the text to compare. Implement this method to return the text to be compared by redlines.

class PlainTextFile(Document):
29class PlainTextFile(Document):
30    """
31    Use this class so that Redlines can read plain text files.
32
33    ```python
34    from redlines import PlainTextFile
35
36    source = PlainTextFile("tests/documents/PlainTextFile/source.txt")
37    test = PlainTextFile("tests/documents/PlainTextFile/test.txt")
38
39    redline = Redlines(source, test)
40    assert (
41        redline.output_markdown
42        == "The quick brown fox <span style='color:red;font-weight:700;text-decoration:line-through;'>jumps over </span><span style='color:green;font-weight:700;'>walks past </span>the lazy dog."
43    )
44    ```
45
46    """
47
48    @property
49    def text(self) -> str:
50        """
51        :return: The text of the file referred to when the Document was created.
52        """
53        return self._text
54
55    def __init__(self, file_path: str | bytes | os.PathLike[str]) -> None:
56        """
57        Use this class so that Redlines can read plain text files.
58
59        :param file_path: Path to the text file.
60        :type file_path: str | bytes | os.PathLike[str]
61        """
62        with open(file_path) as f:
63            self._text = f.read()

Use this class so that Redlines can read plain text files.

from redlines import PlainTextFile

source = PlainTextFile("tests/documents/PlainTextFile/source.txt")
test = PlainTextFile("tests/documents/PlainTextFile/test.txt")

redline = Redlines(source, test)
assert (
    redline.output_markdown
    == "The quick brown fox <span style='color:red;font-weight:700;text-decoration:line-through;'>jumps over </span><span style='color:green;font-weight:700;'>walks past </span>the lazy dog."
)
PlainTextFile(file_path: str | bytes | os.PathLike[str])
55    def __init__(self, file_path: str | bytes | os.PathLike[str]) -> None:
56        """
57        Use this class so that Redlines can read plain text files.
58
59        :param file_path: Path to the text file.
60        :type file_path: str | bytes | os.PathLike[str]
61        """
62        with open(file_path) as f:
63            self._text = f.read()

Use this class so that Redlines can read plain text files.

Parameters
  • file_path: Path to the text file.
text: str
48    @property
49    def text(self) -> str:
50        """
51        :return: The text of the file referred to when the Document was created.
52        """
53        return self._text
Returns

The text of the file referred to when the Document was created.