Make getdotattr always return a flat list

This commit is contained in:
Konsta Vesterinen
2014-12-10 09:44:39 +02:00
parent fe456ff005
commit 54a713959f
2 changed files with 4 additions and 9 deletions

View File

@@ -644,13 +644,7 @@ def getdotattr(obj_or_class, dot_path):
for path in dot_path.split('.'): for path in dot_path.split('.'):
getter = attrgetter(path) getter = attrgetter(path)
if isinstance(last, list): if isinstance(last, list):
tmp = [] last = sum((getter(el) for el in last), [])
for el in last:
if isinstance(el, list):
tmp.extend(map(getter, el))
else:
tmp.append(getter(el))
last = tmp
elif isinstance(last, InstrumentedAttribute): elif isinstance(last, InstrumentedAttribute):
last = getter(last.property.mapper.class_) last = getter(last.property.mapper.class_)
elif last is None: elif last is None:

View File

@@ -67,11 +67,12 @@ class TestGetDotAttr(TestCase):
subsection = self.SubSection(section=section) subsection = self.SubSection(section=section)
subsubsection = self.SubSubSection(subsection=subsection) subsubsection = self.SubSubSection(subsection=subsection)
assert getdotattr(document, 'sections') == [section]
assert getdotattr(document, 'sections.subsections') == [ assert getdotattr(document, 'sections.subsections') == [
[subsection] subsection
] ]
assert getdotattr(document, 'sections.subsections.subsubsections') == [ assert getdotattr(document, 'sections.subsections.subsubsections') == [
[subsubsection] subsubsection
] ]
def test_class_paths(self): def test_class_paths(self):