Skip to content

Commit 6c86f9a

Browse files
authored
Merge pull request #307 from mpsonntag/funcharm
Harmonize attribute naming with nixpy
2 parents 4ce2e1b + 7ac9973 commit 6c86f9a

6 files changed

Lines changed: 151 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
dist
77
build
88
eggs
9+
.eggs
910
parts
1011

1112
# odml files

odml/base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,19 @@ def get_repository(self):
615615
parent) or None
616616
"""
617617
return self._repository
618+
619+
def create_section(self, name, type="undefined", oid=None):
620+
"""
621+
Creates a new subsection that is a child of this section.
622+
623+
:param name: The name of the section to create.
624+
:param type: The type of the section.
625+
:param oid: object id, UUID string as specified in RFC 4122. If no id
626+
is provided, an id will be generated and assigned.
627+
:return: The new section.
628+
"""
629+
from odml.section import BaseSection
630+
sec = BaseSection(name=name, type=type, oid=oid)
631+
sec.parent = self
632+
633+
return sec

odml/property.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def __init__(self, name=None, value=None, parent=None, unit=None,
6262
if not name:
6363
name = self._id
6464

65-
self._name = name
6665
self._parent = None
6766
self._name = name
6867
self._value_origin = value_origin

odml/section.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ def properties(self):
236236
""" The list of all properties contained in this section """
237237
return self._props
238238

239+
@property
240+
def props(self):
241+
""" The list of all properties contained in this section;
242+
NIXpy format style alias for 'properties'."""
243+
return self._props
244+
239245
@property
240246
def sections(self):
241247
""" The list of all child-sections of this section """
@@ -582,3 +588,22 @@ def reorder(self, new_index):
582588
"Section has no parent, cannot reorder in parent list.")
583589

584590
return self._reorder(self.parent.sections, new_index)
591+
592+
def create_property(self, name, value=None, dtype=None, oid=None):
593+
"""
594+
Create a new property that is a child of this section.
595+
596+
:param name: The name of the property.
597+
:param value: Some data value, it can be a single value or
598+
a list of homogeneous values.
599+
:param dtype: The data type of the values stored in the property,
600+
if dtype is not given, the type is deduced from the values.
601+
Check odml.DType for supported data types.
602+
:param oid: object id, UUID string as specified in RFC 4122. If no id
603+
is provided, an id will be generated and assigned.
604+
:return: The new property.
605+
"""
606+
prop = BaseProperty(name=name, value=value, dtype=dtype, oid=oid)
607+
prop.parent = self
608+
609+
return prop

test/test_doc.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,32 @@ def test_comparison(self):
291291

292292
doc_b.sections["subsecA"].properties[0].name = "newPropB"
293293
self.assertNotEqual(doc_a, doc_b)
294+
295+
def test_create_section(self):
296+
root = Document()
297+
self.assertEqual(len(root.sections), 0)
298+
299+
name = "subsec"
300+
type = "subtype"
301+
oid = "79b613eb-a256-46bf-84f6-207df465b8f7"
302+
subsec = root.create_section(name, type, oid)
303+
304+
self.assertEqual(len(root.sections), 1)
305+
self.assertEqual(subsec.parent, root)
306+
self.assertEqual(root.sections[name], subsec)
307+
self.assertEqual(root.sections[name].type, type)
308+
self.assertEqual(root.sections[name].oid, oid)
309+
310+
name = "othersec"
311+
subsec = root.create_section(name)
312+
self.assertEqual(len(root.sections), 2)
313+
self.assertEqual(subsec.parent, root)
314+
self.assertEqual(root.sections[name], subsec)
315+
self.assertEqual(root.sections[name].type, "undefined")
316+
317+
name = "subsubsec"
318+
subsec = root.sections[0].create_section(name)
319+
self.assertEqual(len(root.sections), 2)
320+
self.assertEqual(subsec.parent, root.sections[0])
321+
self.assertEqual(len(root.sections[0].sections), 1)
322+
self.assertEqual(root.sections[0].sections[0].name, name)

test/test_section.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,29 @@ def test_children(self):
203203
with self.assertRaises(ValueError):
204204
sec.properties[0] = "prop"
205205

206+
# same tests with props alias
207+
prop = Property(name="prop2", parent=sec)
208+
newprop = Property(name="newprop2")
209+
210+
self.assertEqual(prop.parent, sec)
211+
self.assertEqual(sec.props[1], prop)
212+
self.assertEqual(len(sec.props), 2)
213+
self.assertIsNone(newprop.parent)
214+
215+
sec.props[1] = newprop
216+
self.assertEqual(newprop.parent, sec)
217+
self.assertEqual(sec.props[1], newprop)
218+
self.assertEqual(len(sec.props), 2)
219+
self.assertIsNone(prop.parent)
220+
221+
# Test set property fails
222+
with self.assertRaises(ValueError):
223+
sec.props[1] = Document()
224+
with self.assertRaises(ValueError):
225+
sec.props[1] = newsec
226+
with self.assertRaises(ValueError):
227+
sec.props[1] = "prop2"
228+
206229
def test_id(self):
207230
s = Section(name="S")
208231
self.assertIsNotNone(s.id)
@@ -863,6 +886,63 @@ def test_comparison(self):
863886
self.assertNotEqual(sec_a, sec_b)
864887
self.assertNotEqual(sec_a.properties, sec_b.properties)
865888

889+
def test_create_section(self):
890+
root = Section("root")
891+
self.assertEqual(len(root.sections), 0)
892+
893+
name = "subsec"
894+
type = "subtype"
895+
oid = "79b613eb-a256-46bf-84f6-207df465b8f7"
896+
subsec = root.create_section(name, type, oid)
897+
898+
self.assertEqual(len(root.sections), 1)
899+
self.assertEqual(subsec.parent, root)
900+
self.assertEqual(root.sections[name], subsec)
901+
self.assertEqual(root.sections[name].type, type)
902+
self.assertEqual(root.sections[name].oid, oid)
903+
904+
name = "othersec"
905+
subsec = root.create_section(name)
906+
self.assertEqual(len(root.sections), 2)
907+
self.assertEqual(subsec.parent, root)
908+
self.assertEqual(root.sections[name], subsec)
909+
self.assertEqual(root.sections[name].type, "undefined")
910+
911+
name = "subsubsec"
912+
subsec = root.sections[0].create_section(name)
913+
self.assertEqual(len(root.sections), 2)
914+
self.assertEqual(subsec.parent, root.sections[0])
915+
self.assertEqual(len(root.sections[0].sections), 1)
916+
self.assertEqual(root.sections[0].sections[0].name, name)
917+
918+
def test_create_property(self):
919+
root = Section("root")
920+
self.assertEqual(len(root.properties), 0)
921+
922+
name = "prop"
923+
oid = "79b613eb-a256-46bf-84f6-207df465b8f7"
924+
prop = root.create_property(name, oid=oid)
925+
self.assertEqual(len(root.properties), 1)
926+
self.assertEqual(prop.parent, root)
927+
self.assertEqual(root.properties[name].oid, oid)
928+
929+
name = "test_values"
930+
values = ["a", "b"]
931+
prop = root.create_property(name, value=values)
932+
self.assertEqual(len(root.properties), 2)
933+
self.assertEqual(root.properties[name].value, values)
934+
935+
name = "test_dtype"
936+
dtype = "str"
937+
prop = root.create_property(name, dtype=dtype)
938+
self.assertEqual(len(root.properties), 3)
939+
self.assertEqual(root.properties[name].dtype, dtype)
940+
941+
name = "test_dtype_fail"
942+
dtype = "I do not exist"
943+
prop = root.create_property(name, dtype=dtype)
944+
self.assertIsNone(prop.dtype)
945+
866946
def test_link(self):
867947
pass
868948

0 commit comments

Comments
 (0)