Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_exists(self):
self.assertTrue(PathManager.exists(self._tmpfile))
fake_path = os.path.join(self._tmpdir, uuid.uuid4().hex)
self.assertFalse(PathManager.exists(fake_path))
def test_rm(self):
with open(os.path.join(self._tmpdir, "test_rm.txt"), "w") as f:
rm_file = f.name
f.write(self._tmpfile_contents)
f.flush()
self.assertTrue(PathManager.exists(rm_file))
self.assertTrue(PathManager.isfile(rm_file))
PathManager.rm(rm_file)
self.assertFalse(PathManager.exists(rm_file))
self.assertFalse(PathManager.isfile(rm_file))
def test_mkdirs(self):
new_dir_path = os.path.join(self._tmpdir, "new", "tmp", "dir")
self.assertFalse(PathManager.exists(new_dir_path))
PathManager.mkdirs(new_dir_path)
self.assertTrue(PathManager.exists(new_dir_path))
def test_exists(self):
self.assertTrue(PathManager.exists(self._tmpfile))
fake_path = os.path.join(self._tmpdir, uuid.uuid4().hex)
self.assertFalse(PathManager.exists(fake_path))
def test_mkdirs(self):
new_dir_path = os.path.join(self._tmpdir, "new", "tmp", "dir")
self.assertFalse(PathManager.exists(new_dir_path))
PathManager.mkdirs(new_dir_path)
self.assertTrue(PathManager.exists(new_dir_path))
def has_checkpoint(self):
"""
Returns:
bool: whether a checkpoint exists in the target directory.
"""
save_file = os.path.join(self.save_dir, "last_checkpoint")
return PathManager.exists(save_file)
def validate_corpus_exists(
corpus: pytorch_translate_data.ParallelCorpusConfig, split: str, is_npz: bool = True
):
"""
Makes sure that the files in the `corpus` are valid files. `split` is used
for logging.
"""
if is_npz:
if not PathManager.exists(corpus.source.data_file):
raise ValueError(f"{corpus.source.data_file} for {split} not found!")
if not PathManager.exists(corpus.target.data_file):
raise ValueError(f"{corpus.target.data_file} for {split} not found!")
else:
if not IndexedDataset.exists(corpus.source.data_file):
raise ValueError(f"{corpus.source.data_file} for {split} not found!")
if not IndexedDataset.exists(corpus.target.data_file):
raise ValueError(f"{corpus.target.data_file} for {split} not found!")