Skip to content

[FIX] Removed header types and flags from .csv and .tab #3427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions Orange/data/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ def write_file(cls, filename, data):
# Priority when multiple formats support the same extension. Also
# the sort order in file open/save combo boxes. Lower is better.
PRIORITY = 10000
OPTIONAL_TYPE_ANNOTATIONS = False

def __init__(self, filename):
"""
Expand Down Expand Up @@ -431,8 +432,11 @@ def get_reader(cls, filename):
raise IOError('No readers for file "{}"'.format(filename))

@classmethod
def write(cls, filename, data):
return cls.write_file(filename, data)
def write(cls, filename, data, with_annotations=True):
if cls.OPTIONAL_TYPE_ANNOTATIONS:
return cls.write_file(filename, data, with_annotations)
else:
return cls.write_file(filename, data)

@classmethod
def write_table_metadata(cls, filename, data):
Expand Down Expand Up @@ -798,11 +802,12 @@ def header_flags(data):
zip(repeat('meta'), data.domain.metas)))))

@classmethod
def write_headers(cls, write, data):
def write_headers(cls, write, data, with_annotations=True):
"""`write` is a callback that accepts an iterable"""
write(cls.header_names(data))
write(cls.header_types(data))
write(cls.header_flags(data))
if with_annotations:
write(cls.header_types(data))
write(cls.header_flags(data))

@classmethod
def formatter(cls, var):
Expand Down Expand Up @@ -856,6 +861,7 @@ class CSVReader(FileFormat):
SUPPORT_COMPRESSED = True
SUPPORT_SPARSE_DATA = False
PRIORITY = 20
OPTIONAL_TYPE_ANNOTATIONS = True

def read(self):
for encoding in (lambda: ('us-ascii', None), # fast
Expand Down Expand Up @@ -915,12 +921,13 @@ def read(self):
raise ValueError('Cannot parse dataset {}: {}'.format(self.filename, error)) from error

@classmethod
def write_file(cls, filename, data):
def write_file(cls, filename, data, with_annotations=True):
with cls.open(filename, mode='wt', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=cls.DELIMITERS[0])
cls.write_headers(writer.writerow, data)
cls.write_headers(writer.writerow, data, with_annotations)
cls.write_data(writer.writerow, data)
cls.write_table_metadata(filename, data)
if with_annotations:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this if - saving the additional metadata file is not related to type annotations for variables.

cls.write_table_metadata(filename, data)


class TabReader(CSVReader):
Expand Down
11 changes: 10 additions & 1 deletion Orange/widgets/data/owsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Error(widget.OWWidget.Error):
filetype = Setting(FILE_TYPES[0][0])
compression = Setting(COMPRESSIONS[0][0])
compress = Setting(False)
add_type_annotations = Setting(True)

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -89,6 +90,10 @@ def __init__(self):

box.layout().addLayout(form)

self.annotations_cb = gui.checkBox(
self.controlArea, self, "add_type_annotations", label="Add type annotations",
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can fix the layout (checkbox not in the box, but outside and with a smaller indent) by changing this to:

        self.annotations_cb = gui.checkBox(
            None, self, "add_type_annotations", label="Add type annotations",
        )
        form.addRow(self.annotations_cb, None)

The layout of this widget is awful anyway, but this is another issue.

self.save = gui.auto_commit(
self.controlArea, self, "auto_save", "Save", box=False,
commit=self.save_file, callback=self.adjust_label,
Expand Down Expand Up @@ -175,14 +180,18 @@ def save_file(self):
os.path.join(
self.last_dir,
self.basename + self.type_ext + self.compress_ext),
self.data)
self.data, self.add_type_annotations)

except Exception as err_value:
self.error(str(err_value))
else:
self.error()

def update_extension(self):
self.type_ext = [ext for name, ext, _ in FILE_TYPES if name == self.filetype][0]
self.annotations_cb.setEnabled(False)
if self.get_writer_selected().OPTIONAL_TYPE_ANNOTATIONS:
self.annotations_cb.setEnabled(True)
self.compress_ext = dict(COMPRESSIONS)[self.compression] if self.compress else ''

def _update_text(self):
Expand Down