Skip to content

Commit

Permalink
Merge pull request #6959 from markotoplak/1header-fix
Browse files Browse the repository at this point in the history
[FIX] 1 line headers: only allow single-letter types and flags
  • Loading branch information
markotoplak authored Dec 20, 2024
2 parents f70085b + f75a884 commit 9fb3969
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
32 changes: 21 additions & 11 deletions Orange/data/io_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,27 @@ def _header1(cls, headers: List[List[str]]) -> Tuple[List, List, List]:
e.g. d#sex,c#age,cC#IQ
"""

def is_flag(x):
return bool(cls._type_from_flag([x])[0] and
_RE_TYPES.match(cls._type_from_flag([x])[0]) or
Flags.RE_ALL.match(cls._flag_from_flag([x])[0]))

flags, names = zip(*[i.split(cls.HEADER1_FLAG_SEP, 1)
if cls.HEADER1_FLAG_SEP in i and
is_flag(i.split(cls.HEADER1_FLAG_SEP)[0])
else ('', i)
for i in headers[0]])
names = list(names)
roles = "".join([f for f in Flags.ALL.values() if len(f) == 1]) # cimw
types = "".join([t for t in flatten(getattr(vartype, 'TYPE_HEADERS')
for vartype in Variable.registry.values())
if len(t) == 1]).upper() # CNDST

res = ('^((?P<flags>'
f'[{roles}{types}]|'
f'([{roles}][{types}])|'
f'([{types}][{roles}])'
')#)?(?P<name>.*)')

header1_re = re.compile(res)

flags = []
names = []
for i in headers[0]:
m = header1_re.match(i)
f, n = m.group("flags", "name")
flags.append('' if f is None else f)
names.append(n)

return names, cls._type_from_flag(flags), cls._flag_from_flag(flags)

@classmethod
Expand Down
12 changes: 12 additions & 0 deletions Orange/data/tests/test_io_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def test_get_header_data_1_flags2(self):
self.assertListEqual(types, types_)
self.assertListEqual(flags, flags_)

def test_get_header_data_1_hashes(self):
names, types, flags = _TableHeader.create_header_data(
[["Some long text#and here", "vd#Invalid spec", "C#Valid spec",
"m#Meta", "cD#Discrete class", "Si#Ignored string"]])
names_ = ["Some long text#and here", "vd#Invalid spec", "Valid spec",
"Meta", "Discrete class", "Ignored string"]
types_ = ["", "", "c", "", "d", "s"]
flags_ = ["", "", "", "m", "c", "i"]
self.assertListEqual(names, names_)
self.assertListEqual(types, types_)
self.assertListEqual(flags, flags_)

def test_get_header_data_3(self):
names, types, flags = _TableHeader.create_header_data(self.header3[:3])
self.assertListEqual(names, ["a", "b", "c", "d", "w", "e", "f", "g"])
Expand Down
9 changes: 9 additions & 0 deletions i18n/si/msgs.jaml
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,15 @@ data/io_base.py:
^\s*( |{}|)*\s*$: false
class `_TableHeader`:
'#': false
def `_header1`:
TYPE_HEADERS: false
^((?P<flags>: false
[{roles}{types}]|: false
([{roles}][{types}])|: false
([{types}][{roles}]): false
)#)?(?P<name>.*): false
flags: false
name: false
class `_TableBuilder`:
def `__init__`:
'Feature ': false
Expand Down

0 comments on commit 9fb3969

Please sign in to comment.