Skip to content

Commit 1dc53af

Browse files
committed
[FIX] data/io.py Metadata file not saved anymore when it is empty
Issue Do not save the metadata file (when saving the data) if this is empty (0 Bytes) Description of changes Change if statement and add third condition. It implies that attribute should not be empty.
1 parent 4830c26 commit 1dc53af

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Orange/data/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def write(cls, filename, data):
373373

374374
@classmethod
375375
def write_table_metadata(cls, filename, data):
376-
if isinstance(filename, str) and hasattr(data, 'attributes'):
376+
if isinstance(filename, str) and getattr(data, 'attributes', None):
377377
if all(isinstance(key, str) and isinstance(value, str)
378378
for key, value in data.attributes.items()):
379379
with open(filename + '.metadata', 'w') as f:

Orange/tests/test_tab_reader.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,23 @@ def test_data_name(self):
206206
table2 = TabReader(table1.__file__).read()
207207
self.assertEqual(table1.name, 'iris')
208208
self.assertEqual(table2.name, 'iris')
209+
210+
def test_metadata(self):
211+
tempdir = tempfile.mkdtemp()
212+
table = Table("titanic")
213+
table.attributes = OrderedDict()
214+
table.attributes["a"] = "aa"
215+
table.attributes["b"] = "bb"
216+
fname = path.join(tempdir, "out.tab")
217+
TabReader.write_table_metadata(fname, table)
218+
self.assertTrue(path.isfile(fname + ".metadata"))
219+
shutil.rmtree(tempdir)
220+
221+
def test_no_metadata(self):
222+
tempdir = tempfile.mkdtemp()
223+
table = Table("titanic")
224+
table.attributes = OrderedDict()
225+
fname = path.join(tempdir, "out.tab")
226+
TabReader.write_table_metadata(fname, table)
227+
self.assertFalse(path.isfile(fname + ".metadata"))
228+
shutil.rmtree(tempdir)

0 commit comments

Comments
 (0)