Skip to content

Storage Corpus

lingo.storage.corpus

Storage implementations for Corpus types.

EmptyCorpus

Bases: Corpus[bytes]

A corpus with no content (for placeholder/mock usage).

Source code in lingo/storage/corpus.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class EmptyCorpus(Corpus[bytes]):
    """A corpus with no content (for placeholder/mock usage)."""

    @classmethod
    def from_object(cls, obj: Any) -> EmptyCorpus:
        """Create an empty corpus."""
        return cls(content_type="application/octet-stream", bytes_data=b"")

    def materialize_to_object(self) -> bytes:
        """Returns empty bytes."""
        return b""

    def materialize_content(self) -> bytes:
        """Returns empty bytes."""
        return b""

    def materialize_to_file(self, path: Any) -> Path:
        """Create an empty file."""
        path = Path(path)
        path.write_bytes(b"")
        return path

from_object(obj: Any) -> EmptyCorpus classmethod

Create an empty corpus.

Source code in lingo/storage/corpus.py
46
47
48
49
@classmethod
def from_object(cls, obj: Any) -> EmptyCorpus:
    """Create an empty corpus."""
    return cls(content_type="application/octet-stream", bytes_data=b"")

materialize_content() -> bytes

Returns empty bytes.

Source code in lingo/storage/corpus.py
55
56
57
def materialize_content(self) -> bytes:
    """Returns empty bytes."""
    return b""

materialize_to_file(path: Any) -> Path

Create an empty file.

Source code in lingo/storage/corpus.py
59
60
61
62
63
def materialize_to_file(self, path: Any) -> Path:
    """Create an empty file."""
    path = Path(path)
    path.write_bytes(b"")
    return path

materialize_to_object() -> bytes

Returns empty bytes.

Source code in lingo/storage/corpus.py
51
52
53
def materialize_to_object(self) -> bytes:
    """Returns empty bytes."""
    return b""

JSONCorpus

Bases: Corpus[dict]

Corpus implementation for JSON serialization.

Source code in lingo/storage/corpus.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class JSONCorpus(Corpus[dict]):
    """Corpus implementation for JSON serialization."""

    @classmethod
    def from_object(cls, obj: Any) -> JSONCorpus:
        """Create a JSON corpus from a Python object."""
        payload = obj if isinstance(obj, dict) else {"data": obj}
        return cls(content_type="application/json", object_data=payload)

    def materialize_to_object(self) -> dict:
        """Materialize the corpus to a Python dictionary."""
        if self.object_data is not None:
            return self.object_data
        if self.bytes_data:
            return json.loads(self.bytes_data.decode())
        return {}

    def materialize_content(self) -> bytes:
        """Materialize the corpus to JSON bytes."""
        if self.bytes_data is not None:
            return self.bytes_data
        return json.dumps(self.materialize_to_object()).encode()

    def materialize_to_file(self, path: Any) -> Path:
        """Materialize the corpus to a JSON file."""
        path = Path(path)
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(json.dumps(self.materialize_to_object()))
        return path

from_object(obj: Any) -> JSONCorpus classmethod

Create a JSON corpus from a Python object.

Source code in lingo/storage/corpus.py
15
16
17
18
19
@classmethod
def from_object(cls, obj: Any) -> JSONCorpus:
    """Create a JSON corpus from a Python object."""
    payload = obj if isinstance(obj, dict) else {"data": obj}
    return cls(content_type="application/json", object_data=payload)

materialize_content() -> bytes

Materialize the corpus to JSON bytes.

Source code in lingo/storage/corpus.py
29
30
31
32
33
def materialize_content(self) -> bytes:
    """Materialize the corpus to JSON bytes."""
    if self.bytes_data is not None:
        return self.bytes_data
    return json.dumps(self.materialize_to_object()).encode()

materialize_to_file(path: Any) -> Path

Materialize the corpus to a JSON file.

Source code in lingo/storage/corpus.py
35
36
37
38
39
40
def materialize_to_file(self, path: Any) -> Path:
    """Materialize the corpus to a JSON file."""
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(self.materialize_to_object()))
    return path

materialize_to_object() -> dict

Materialize the corpus to a Python dictionary.

Source code in lingo/storage/corpus.py
21
22
23
24
25
26
27
def materialize_to_object(self) -> dict:
    """Materialize the corpus to a Python dictionary."""
    if self.object_data is not None:
        return self.object_data
    if self.bytes_data:
        return json.loads(self.bytes_data.decode())
    return {}