def test_multislot_dump_load(self): with tempfile.TemporaryFile("w+b") as tf: dump(self.obj1k, tf, slot_key='1k') dump(self.obj2k, tf, slot_key='2k') recovered_obj1k = load(tf, slot_key='1k') recovered_obj2k = load(tf, slot_key='2k') self.assertEqualObj1k(recovered_obj1k) self.assertEqualObj2k(recovered_obj2k)
def test_sameslot_dump_load(self): with tempfile.TemporaryFile("w+b") as tf: dump(self.obj1k, tf, slot_key='same-slot') recovered_obj1k = load(tf, slot_key='same-slot') # Try to override dump(self.obj2k, tf, slot_key='same-slot') recovered_obj2k = load(tf, slot_key='same-slot') self.assertEqualObj1k(recovered_obj1k) self.assertEqualObj2k(recovered_obj2k)
def test_multi_dump_object(self): with TemporaryDirectory() as tmp_dir: opener = partial(open, Path(tmp_dir) / 'test.mlpack') # Perform two writes in different slots. r = MLIOResource('theid', 'something', slot_key='the-list') r._dump_object_impl(self.the_list, opener) r = MLIOResource('theid', 'something', slot_key='the-text') r._dump_object_impl(self.the_text, opener) # The MLPack shouldn't be truncated with opener(mode='rb') as f: recovered_text = mlio.load(f, slot_key='the-text') recovered_list = mlio.load(f, slot_key='the-list') self.assertListEqual(self.the_list, recovered_list) self.assertEqual(self.the_text, recovered_text)
def test_simple_dump_load(self): with tempfile.TemporaryFile("w+b") as tf: dump(self.obj2k, tf) recovered_obj2k = load(tf) self.assertEqualObj2k(recovered_obj2k)
def test_default_dump_object(self): with TemporaryDirectory() as tmp_dir: opener = partial(open, Path(tmp_dir) / 'test.mlpack') r = MLIOResource('theid', 'something') obj = {'oNe', 'Two', "Νιαις œ•³≥≠÷’…ß÷≠ γιουνικοτ"} r._dump_object_impl(obj, opener) with opener(mode='rb') as f: recovered_obj = mlio.load(f) self.assertSetEqual(obj, recovered_obj)
def _load_object_impl(self, opener): with opener(mode='rb') as f: if self._slot_key is None: return mlio.load(f) else: return mlio.load(f, slot_key=self._slot_key)