File: test_shim.py

package info (click to toggle)
python-thinc 8.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,804 kB
  • sloc: python: 15,818; javascript: 1,554; ansic: 342; makefile: 20; sh: 13
file content (33 lines) | stat: -rw-r--r-- 943 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from typing import List
from thinc.shims.shim import Shim
from ..util import make_tempdir


class MockShim(Shim):
    def __init__(self, data: List[int]):
        super().__init__(None, config=None, optimizer=None)
        self.data = data

    def to_bytes(self):
        return bytes(self.data)

    def from_bytes(self, data: bytes) -> "MockShim":
        return MockShim(data=list(data))


def test_shim_can_roundtrip_with_path():

    with make_tempdir() as path:
        shim_path = path / "cool_shim.data"
        shim = MockShim([1, 2, 3])
        shim.to_disk(shim_path)
        copy_shim = shim.from_disk(shim_path)
    assert copy_shim.to_bytes() == shim.to_bytes()


def test_shim_can_roundtrip_with_path_subclass(pathy_fixture):
    shim_path = pathy_fixture / "cool_shim.data"
    shim = MockShim([1, 2, 3])
    shim.to_disk(shim_path)
    copy_shim = shim.from_disk(shim_path)
    assert copy_shim.to_bytes() == shim.to_bytes()