def test_save_download_text_stream_to_given_destination(self): exp = init(mode="debug", flush_period=0.5) data = "Some test content of the stream" exp["some/num/attr_name"] = FileVal.from_stream(StringIO(data)) self.assertIsInstance(exp.get_structure()["some"]["num"]["attr_name"], File) with create_file() as temp_filename: exp["some/num/attr_name"].download(temp_filename) with open(temp_filename, "rt") as file: self.assertEqual(file.read(), data)
def test_create_from_bytes_io(self): file = File.from_stream(BytesIO(b"aaabbbccc")) self.assertEqual(None, file.path) self.assertEqual(b"aaabbbccc", file.content) self.assertEqual("bin", file.extension) stream = BytesIO(b"aaabbbccc") stream.seek(3) file = File.from_stream(stream) self.assertEqual(None, file.path) self.assertEqual(b"aaabbbccc", file.content) self.assertEqual("bin", file.extension) file = File.from_stream(BytesIO(b"aaabbbccc"), extension="png") self.assertEqual(None, file.path) self.assertEqual(b"aaabbbccc", file.content) self.assertEqual("png", file.extension) file = File.from_stream(BytesIO(b"aaabbbccc"), seek=5) self.assertEqual(None, file.path) self.assertEqual(b"bccc", file.content) self.assertEqual("bin", file.extension)
def test_create_from_string_io(self): file = File.from_stream(StringIO("aaabbbccc")) self.assertEqual(None, file.path) self.assertEqual(b"aaabbbccc", file.content) self.assertEqual("txt", file.extension) stream = StringIO("aaabbbccc") stream.seek(3) file = File.from_stream(stream) self.assertEqual(None, file.path) self.assertEqual(b"aaabbbccc", file.content) self.assertEqual("txt", file.extension) file = File.from_stream(StringIO("aaabbbccc"), extension="png") self.assertEqual(None, file.path) self.assertEqual(b"aaabbbccc", file.content) self.assertEqual("png", file.extension) file = File.from_stream(StringIO("aaabbbccc"), seek=5) self.assertEqual(None, file.path) self.assertEqual(b"bccc", file.content) self.assertEqual("txt", file.extension)
def _get_base64_image_content(file: File) -> str: if file.path is not None: if not os.path.exists(file.path): raise FileNotFound(file.path) with open(file.path, "rb") as image_file: file = File.from_stream(image_file) ext = imghdr.what("", h=file.content) if not ext: raise OperationNotSupported( "FileSeries supports only image files for now. " "Other file types will be implemented in future." ) return base64_encode(file.content)
def test_save_download_binary_stream_to_default_destination(self): exp = init(mode="debug", flush_period=0.5) data = b"Some test content of the stream" exp["some/num/attr_name"] = FileVal.from_stream(BytesIO(data)) self.assertIsInstance(exp.get_structure()["some"]["num"]["attr_name"], File) with TemporaryDirectory() as temp_dir: with patch( "neptune.new.internal.backends.neptune_backend_mock.os.path.abspath" ) as abspath_mock: abspath_mock.side_effect = lambda path: os.path.normpath( temp_dir + "/" + path) exp["some/num/attr_name"].download() with open(temp_dir + "/attr_name.bin", "rb") as file: self.assertEqual(file.read(), data)
def test_fetch_dict(self): now = datetime.now() exp = init(mode="debug", flush_period=0.5) exp["params/int"] = 1 exp["params/float"] = 3.14 exp["params/bool"] = True exp["params/datetime"] = now exp["params/sub-namespace/int"] = 42 exp["params/sub-namespace/string"] = "Some text" # attributes to be ignored exp["params/sub-namespace/string_series"].log("Some text #1") exp["params/sub-namespace/int_series"].log(100) exp["some/num/attr_name"] = FileVal.from_stream( BytesIO(b"Some stream")) # pylint: disable=assignment-from-no-return # that's a false positive, pylint looks at Handler.fetch() params_dict = exp["params"].fetch() self.assertDictEqual( params_dict, { "int": 1, "float": 3.14, "bool": True, "datetime": now.replace(microsecond=1000 * int(now.microsecond / 1000)), "sub-namespace": { "int": 42, "string": "Some text", }, }, )