Skip to content

Store

ExampleStore

Source code in recon/store.py
10
11
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
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
76
77
78
79
80
81
class ExampleStore:
    def __init__(self, examples: List[Example] = []):
        self._map: Dict[int, Example] = {}
        for e in examples:
            self.add(e.model_copy(deep=True))

    def __getitem__(self, example_hash: int) -> Example:
        return self._map[example_hash]

    def __len__(self) -> int:
        """The number of strings in the store.

        Returns:
            Number of examples in store
        """
        return len(self._map)

    def __contains__(self, example: Union[int, Example]) -> bool:
        """Check whether a string is in the store.

        Args:
            example (Union[int, Example]): The example to check

        Returns:
            Whether the store contains the example.
        """
        example_hash = hash(example) if isinstance(example, Example) else example
        return example_hash in self._map

    def add(self, example: Example) -> None:
        """Add an Example to the store

        Args:
            example (Example): example to add
        """
        example_hash = hash(example)
        if example_hash not in self:
            self._map[example_hash] = example.model_copy(deep=True)

    def from_disk(self, path: Union[str, Path]) -> "ExampleStore":
        """Load store from disk

        Args:
            path (Path): Path to file to load from

        Returns:
            ExampleStore: Initialized ExampleStore
        """
        path = ensure_path(path)
        examples = srsly.read_jsonl(path)
        for e in examples:
            e = cast(Dict[str, Any], e)
            raw_example = e["example"]
            example = Example(**raw_example)
            self.add(example)

        return self

    def to_disk(self, path: Union[str, Path]) -> None:
        """Save store to disk

        Args:
            path (Path): Path to save store to
        """
        path = ensure_path(path)
        examples = []
        for example_hash, example in self._map.items():
            examples.append(
                {"example_hash": example_hash, "example": example.model_dump()}
            )

        srsly.write_jsonl(path, examples)

__contains__(example)

Check whether a string is in the store.

Parameters:

Name Type Description Default
example Union[int, Example]

The example to check

required

Returns:

Type Description
bool

Whether the store contains the example.

Source code in recon/store.py
27
28
29
30
31
32
33
34
35
36
37
def __contains__(self, example: Union[int, Example]) -> bool:
    """Check whether a string is in the store.

    Args:
        example (Union[int, Example]): The example to check

    Returns:
        Whether the store contains the example.
    """
    example_hash = hash(example) if isinstance(example, Example) else example
    return example_hash in self._map

__len__()

The number of strings in the store.

Returns:

Type Description
int

Number of examples in store

Source code in recon/store.py
19
20
21
22
23
24
25
def __len__(self) -> int:
    """The number of strings in the store.

    Returns:
        Number of examples in store
    """
    return len(self._map)

add(example)

Add an Example to the store

Parameters:

Name Type Description Default
example Example

example to add

required
Source code in recon/store.py
39
40
41
42
43
44
45
46
47
def add(self, example: Example) -> None:
    """Add an Example to the store

    Args:
        example (Example): example to add
    """
    example_hash = hash(example)
    if example_hash not in self:
        self._map[example_hash] = example.model_copy(deep=True)

from_disk(path)

Load store from disk

Parameters:

Name Type Description Default
path Path

Path to file to load from

required

Returns:

Name Type Description
ExampleStore ExampleStore

Initialized ExampleStore

Source code in recon/store.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def from_disk(self, path: Union[str, Path]) -> "ExampleStore":
    """Load store from disk

    Args:
        path (Path): Path to file to load from

    Returns:
        ExampleStore: Initialized ExampleStore
    """
    path = ensure_path(path)
    examples = srsly.read_jsonl(path)
    for e in examples:
        e = cast(Dict[str, Any], e)
        raw_example = e["example"]
        example = Example(**raw_example)
        self.add(example)

    return self

to_disk(path)

Save store to disk

Parameters:

Name Type Description Default
path Path

Path to save store to

required
Source code in recon/store.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def to_disk(self, path: Union[str, Path]) -> None:
    """Save store to disk

    Args:
        path (Path): Path to save store to
    """
    path = ensure_path(path)
    examples = []
    for example_hash, example in self._map.items():
        examples.append(
            {"example_hash": example_hash, "example": example.model_dump()}
        )

    srsly.write_jsonl(path, examples)