Ticket #720 (new bug)

Opened 3 years ago

ExampleTable.save() for .txt files doesn't preserve attribute metadata

Reported by: yang Owned by:
Milestone: Future Component: canvas
Severity: minor Keywords:
Cc: Blocking:
Blocked By:

Description

The save() method doesn't preserve the special characters describing the variable type when writing new-style tab-delimited files (.txt).

The special characters are decribed here:

 http://orange.biolab.si/doc/reference/tabdelimited.htm

"Prefixed attributes contain one- or two-lettered prefix, followed by "#" and the name. The first letter of the prefix can be either "m" for meta-attributes, "i" to ignore the attribute, and "c" to define the class attribute. As always, only one attribute can be a class attribue. The second letter denotes the attribute type, "D" for discrete, "C" for continuous, "S" for string attributes and "B" for baskets."

I know that at least the attribute type character is not being written, so saving then loading a .txt will not produce the same ExampleTable.

Here is some code I've written to get around this (from  http://orange.biolab.si/forum/viewtopic.php?f=3&t=1231):

def savePar(path, table):
    with open(path, 'w') as f:
        # The order happens to match the order of the attributes in saveTxt
        attrs = ('%s%s#%s' % (
            'c' if a is table.domain.classVar else '',
            'D' if a.varType == orange.VarTypes.Discrete else 'C',
            a.name)
            for a in table.domain)
        metas = ('mD#%s' % m.name for m in table.domain.getmetas().values())
        print >> f, '\t'.join(itertools.chain(attrs, metas))
    table.save(path + '.txt')
    with open(path, 'a') as f:
        with open(path + '.txt') as g:
            g.readline()
            f.write(g.read())
Note: See TracTickets for help on using tickets.