File: test_shim.py

package info (click to toggle)
python-thinc 9.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,896 kB
  • sloc: python: 17,122; javascript: 1,559; ansic: 342; makefile: 15; sh: 13
file content (35 lines) | stat: -rw-r--r-- 945 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
34
35
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()