Skip to content

Commit 481d4a6

Browse files
authored
Merge pull request #346 from fschrader1992/master
Leaf Export and Property Handling
2 parents 2c79fb6 + 9095664 commit 481d4a6

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

odml/property.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,13 @@ def values(self, new_value):
328328
self._dtype = dtypes.infer_dtype(new_value[0])
329329

330330
if not self._validate_values(new_value):
331-
raise ValueError("odml.Property.values: passed values are not of "
332-
"consistent type!")
331+
if self._dtype in ("date", "time", "datetime"):
332+
raise ValueError("odml.Property.values: passed values are not of "
333+
"consistent type \'%s\'! Format should be \'%s\'." %
334+
(self._dtype, dtypes.default_values(self._dtype)))
335+
else:
336+
raise ValueError("odml.Property.values: passed values are not of "
337+
"consistent type!")
333338
self._values = [dtypes.get(v, self.dtype) for v in new_value]
334339

335340
@property
@@ -662,3 +667,27 @@ def pprint(self, indent=2, max_length=80, current_depth=-1):
662667
else:
663668
print(("{}{} {}: {}".format(property_spaces, prefix, self.name,
664669
value_string)))
670+
671+
def export_leaf(self):
672+
"""
673+
Export only the path from this property to the root.
674+
Include all properties of parent sections.
675+
"""
676+
curr = self.parent
677+
par = self.parent
678+
child = self.parent
679+
680+
while curr is not None:
681+
par = curr.clone(children=False, keep_id=True)
682+
if curr != self.parent:
683+
par.append(child)
684+
if hasattr(curr, 'properties'):
685+
if curr == self.parent:
686+
par.append(self.clone(keep_id=True))
687+
else:
688+
for prop in curr.properties:
689+
par.append(prop.clone(keep_id=True))
690+
child = par
691+
curr = curr.parent
692+
693+
return par

odml/section.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,24 @@ def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
638638
print("{} {} [{}]\n{}[...]".format(child_sec_indent,
639639
s.name, s.type,
640640
more_indent))
641+
642+
def export_leaf(self):
643+
"""
644+
Export only the path from this section to the root.
645+
Include all properties for all sections, but no other subsections.
646+
"""
647+
curr = self
648+
par = self
649+
child = self
650+
651+
while curr is not None:
652+
par = curr.clone(children=False, keep_id=True)
653+
if curr != self:
654+
par.append(child)
655+
if hasattr(curr, 'properties'):
656+
for prop in curr.properties:
657+
par.append(prop.clone(keep_id=True))
658+
child = par
659+
curr = curr.parent
660+
661+
return par

test/test_property.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import unittest
22

3+
import datetime
4+
35
from odml import Property, Section, Document, DType
46
from odml.property import BaseProperty
57
from odml.section import BaseSection
@@ -115,6 +117,19 @@ def test_value(self):
115117
self.assertEqual(p5.value, [0])
116118
self.assertEqual(p5.values, [0])
117119

120+
with self.assertRaises(ValueError):
121+
Property(name="dateprop", dtype=DType.date, value=['20190707'])
122+
123+
with self.assertRaises(ValueError):
124+
Property(name="timeprop", dtype=DType.time, value=['11.11.11'])
125+
126+
with self.assertRaises(ValueError):
127+
Property(name="datetimeprop", dtype=DType.datetime, value=['20190707'])
128+
129+
with self.assertRaises(ValueError):
130+
Property(name="intprop", dtype=DType.int, value=[2, "Hello!", 4])
131+
132+
118133
def test_value_append(self):
119134
# Test append w/o Property value or dtype
120135
prop = Property(name="append")
@@ -733,6 +748,42 @@ def test_comparison(self):
733748
prop_b.name = 'newPropertyName'
734749
self.assertNotEqual(prop_a, prop_b)
735750

751+
def test_export_leaf(self):
752+
doc = Document()
753+
first = doc.create_section("first")
754+
second = first.create_section("second")
755+
first.create_section("third")
756+
757+
name = "prop1"
758+
values = [1.3]
759+
first.create_property(name, value=values)
760+
761+
name = "prop2"
762+
values = ["words"]
763+
second.create_property(name, value=values)
764+
765+
name = "prop3"
766+
values = ["a", "b"]
767+
second.create_property(name, value=values)
768+
769+
name = "prop4"
770+
values = [3]
771+
second.create_property(name, value=values)
772+
773+
name = "prop5"
774+
values = ["abc"]
775+
first.create_property(name, value=values)
776+
777+
ex1 = first.properties["prop1"].export_leaf()
778+
self.assertEqual(len(ex1['first'].properties), 1)
779+
self.assertEqual(len(ex1['first'].sections), 0)
780+
781+
ex2 = second.properties["prop2"].export_leaf()
782+
self.assertEqual(len(ex2.sections), 1)
783+
self.assertEqual(len(ex2['first'].properties), 2)
784+
self.assertEqual(len(ex2['first'].sections), 1)
785+
self.assertEqual(len(ex2['first']['second'].properties), 1)
786+
736787

737788
if __name__ == "__main__":
738789
print("TestProperty")

test/test_section.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,41 @@ def test_create_property(self):
943943
prop = root.create_property(name, dtype=dtype)
944944
self.assertIsNone(prop.dtype)
945945

946+
def test_export_leaf(self):
947+
doc = Document()
948+
first = doc.create_section("first")
949+
second = first.create_section("second")
950+
third = first.create_section("third")
951+
952+
name = "prop1"
953+
values = [1.3]
954+
first.create_property(name, value=values)
955+
956+
name = "prop2"
957+
values = ["words"]
958+
first.create_property(name, value=values)
959+
960+
name = "prop3"
961+
values = ["a", "b"]
962+
second.create_property(name, value=values)
963+
964+
ex1 = first.export_leaf()
965+
self.assertEqual(len(ex1.sections), 1)
966+
self.assertEqual(len(ex1['first'].properties), 2)
967+
self.assertEqual(len(ex1['first'].sections), 0)
968+
969+
ex2 = second.export_leaf()
970+
self.assertEqual(len(ex2.sections), 1)
971+
self.assertEqual(len(ex2['first'].properties), 2)
972+
self.assertEqual(len(ex2['first'].sections), 1)
973+
self.assertEqual(len(ex2['first']['second'].properties), 1)
974+
975+
ex3 = third.export_leaf()
976+
self.assertEqual(len(ex3.sections), 1)
977+
self.assertEqual(len(ex3['first'].properties), 2)
978+
self.assertEqual(len(ex3['first'].sections), 1)
979+
self.assertEqual(len(ex3['first']['third']), 0)
980+
946981
def test_link(self):
947982
pass
948983

0 commit comments

Comments
 (0)