def test_FSCache_404(): c = cache.FSCache("/tmp/labm8-cache-404") with pytest.raises(KeyError): c['foobar'] with pytest.raises(KeyError): del c['foobar'] assert not c.get("foobar") assert c.get("foobar", 5) == 5 c.clear()
def __init__(self, path: pathlib.Path): self.path = path.absolute() self.cache = cache.FSCache(self.path) self.corpus = NullCorpus() self.config = pbutil.FromFile( self.path / 'META.pbtxt', internal_pb2.ModelMeta()).config self.atomizer = atomizers.AtomizerBase.FromFile(self.path / 'atomizer') self.backend = { model_pb2.NetworkArchitecture.TENSORFLOW: tensorflow_backend.TensorFlowBackend, model_pb2.NetworkArchitecture.KERAS: keras_backend.KerasBackend, }[self.config.architecture.backend](self.config, self.cache, self.atomizer)
def test_FSCache_dict_key(): c = cache.FSCache("/tmp/labm8-cache-dict") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check assert fs.read("/tmp/labm8.test.remove.txt") == ["Hello, world!"] # insert file into cache key = {'a': 5, "c": [1, 2, 3]} c[key] = "/tmp/labm8.test.remove.txt" # check file contents assert fs.read(c[key]) == ["Hello, world!"] c.clear()
def mkcache(*relative_path_components: str) -> cache.FSCache: """Instantiate a file system cache. If the cache does not exist, one is created. Args: *relative_path_components: Relative path of cache. Returns: A filesystem cache instance. """ return cache.FSCache(cachepath(*relative_path_components), escape_key=cache.escape_path)
def test_FSCache_iter_len(): c = cache.FSCache("/tmp/labm8-fscache-iter", escape_key=cache.escape_path) c.clear() system.echo("Hello, world!", "/tmp/labm8.testfile.txt") c["foo"] = "/tmp/labm8.testfile.txt" for path in c: assert path == c.keypath("foo") system.echo("Hello, world!", "/tmp/labm8.testfile.txt") c["bar"] = "/tmp/labm8.testfile.txt" assert len(c) == 2 assert len(c.ls()) == 2 assert "bar" in c.ls() assert "foo" in c.ls() c.clear()
def test_FSCache_remove(): c = cache.FSCache("/tmp/labm8-cache-remove") # create file system.echo("Hello, world!", "/tmp/labm8.test.remove.txt") # sanity check assert fs.read("/tmp/labm8.test.remove.txt") == ["Hello, world!"] # insert file into cache c['foobar'] = "/tmp/labm8.test.remove.txt" # sanity check assert fs.read(c['foobar']) == ["Hello, world!"] # remove from cache del c['foobar'] with pytest.raises(KeyError): c['foobar'] assert not c.get("foobar") c.clear()
def test_set_and_get(): fs.rm("/tmp/labm8-cache-set-and-get") c = cache.FSCache("/tmp/labm8-cache-set-and-get") # create file system.echo("Hello, world!", "/tmp/labm8.testfile.txt") # sanity check assert fs.read("/tmp/labm8.testfile.txt") == ["Hello, world!"] # insert file into cache c['foobar'] = "/tmp/labm8.testfile.txt" # file must be in cache assert fs.isfile(c.keypath("foobar")) # file must have been moved assert not fs.isfile("/tmp/labm8.testfile.txt") # check file contents assert fs.read(c['foobar']) == ["Hello, world!"] assert fs.read(c['foobar']) == fs.read(c.get('foobar')) c.clear()
def test_FSCache_missing_key(): c = cache.FSCache("/tmp/labm8-missing-key") with pytest.raises(ValueError): c['foo'] = '/not/a/real/path' c.clear()
def test_FSCache_init_and_empty(): c = cache.FSCache("/tmp/labm8-cache-init-and-empty") assert fs.isdir("/tmp/labm8-cache-init-and-empty") c.clear() assert not fs.isdir("/tmp/labm8-cache-init-and-empty")