Skip to content

Commit 617580c

Browse files
authored
Merge pull request #278 from mpsonntag/slimFast
Flatten odml class hierarchy
2 parents 8de0721 + f6dd160 commit 617580c

5 files changed

Lines changed: 74 additions & 106 deletions

File tree

odml/base.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,14 @@
99
from .tools.doc_inherit import allow_inherit_docstring
1010

1111

12-
class _baseobj(object):
13-
pass
14-
15-
16-
class baseobject(_baseobj):
12+
class BaseObject(object):
1713
_format = None
1814

19-
def format(self):
20-
return self._format
21-
22-
@property
23-
def document(self):
24-
""" Returns the Document object in which this object is contained """
25-
if self.parent is None:
26-
return None
27-
return self.parent.document
28-
29-
def get_terminology_equivalent(self):
15+
def __hash__(self):
3016
"""
31-
Returns the equivalent object in an terminology (should there be one
32-
defined) or None
17+
Allow all odML objects to be hash-able.
3318
"""
34-
return None
19+
return id(self)
3520

3621
def __eq__(self, obj):
3722
"""
@@ -40,9 +25,6 @@ def __eq__(self, obj):
4025
unique within a document.
4126
"""
4227
# cannot compare totally different stuff
43-
if not isinstance(obj, _baseobj):
44-
return False
45-
4628
if not isinstance(self, obj.__class__):
4729
return False
4830

@@ -60,6 +42,23 @@ def __ne__(self, obj):
6042
"""
6143
return not self == obj
6244

45+
def format(self):
46+
return self._format
47+
48+
@property
49+
def document(self):
50+
""" Returns the Document object in which this object is contained """
51+
if self.parent is None:
52+
return None
53+
return self.parent.document
54+
55+
def get_terminology_equivalent(self):
56+
"""
57+
Returns the equivalent object in an terminology (should there be one
58+
defined) or None
59+
"""
60+
return None
61+
6362
def clean(self):
6463
"""
6564
Stub that doesn't do anything for this class
@@ -77,12 +76,6 @@ def clone(self, children=True):
7776
obj = copy.copy(self)
7877
return obj
7978

80-
def __hash__(self):
81-
"""
82-
Allow all odML objects to be hash-able.
83-
"""
84-
return id(self)
85-
8679

8780
class SmartList(list):
8881

@@ -167,12 +160,21 @@ def append(self, *obj_tuple):
167160

168161

169162
@allow_inherit_docstring
170-
class sectionable(baseobject):
163+
class Sectionable(BaseObject):
171164
def __init__(self):
172165
from odml.section import BaseSection
173166
self._sections = SmartList(BaseSection)
174167
self._repository = None
175168

169+
def __getitem__(self, key):
170+
return self._sections[key]
171+
172+
def __len__(self):
173+
return len(self._sections)
174+
175+
def __iter__(self):
176+
return self._sections.__iter__()
177+
176178
@property
177179
def document(self):
178180
"""
@@ -249,15 +251,6 @@ def remove(self, section):
249251
self._sections.remove(section)
250252
section._parent = None
251253

252-
def __getitem__(self, key):
253-
return self._sections[key]
254-
255-
def __len__(self):
256-
return len(self._sections)
257-
258-
def __iter__(self):
259-
return self._sections.__iter__()
260-
261254
def itersections(self, recursive=True, yield_self=False,
262255
filter_func=lambda x: True, max_depth=None):
263256
"""
@@ -565,7 +558,7 @@ def clone(self, children=True):
565558
to another document
566559
"""
567560
from odml.section import BaseSection
568-
obj = super(sectionable, self).clone(children)
561+
obj = super(Sectionable, self).clone(children)
569562
obj._parent = None
570563
obj._sections = SmartList(BaseSection)
571564
if children:

odml/doc.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring
99

1010

11-
class Document(base._baseobj):
12-
pass
13-
14-
1511
@allow_inherit_docstring
16-
class BaseDocument(base.sectionable, Document):
12+
class BaseDocument(base.Sectionable):
1713
"""
1814
A representation of an odML document in memory.
1915
Its odml attributes are: *author*, *date*, *version* and *repository*.
@@ -41,6 +37,10 @@ def __init__(self, author=None, date=None, version=None, repository=None, id=Non
4137
self._date = None
4238
self.date = date
4339

40+
def __repr__(self):
41+
return "<Doc %s by %s (%d sections)>" % (self._version, self._author,
42+
len(self._sections))
43+
4444
@property
4545
def id(self):
4646
"""
@@ -107,11 +107,6 @@ def parent(self):
107107
""" The parent of a document is always None. """
108108
return None
109109

110-
def __repr__(self):
111-
return "<Doc %s by %s (%d sections)>" % (self._version,
112-
self._author,
113-
len(self._sections))
114-
115110
def finalize(self):
116111
"""
117112
This needs to be called after the document is set up from parsing

odml/property.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring
99

1010

11-
class Property(base._baseobj):
12-
pass
13-
14-
1511
@allow_inherit_docstring
16-
class BaseProperty(base.baseobject, Property):
12+
class BaseProperty(base.BaseObject):
1713
"""An odML Property"""
1814
_format = frmt.Property
1915

@@ -83,6 +79,23 @@ def __init__(self, name, value=None, parent=None, unit=None,
8379

8480
self.parent = parent
8581

82+
def __len__(self):
83+
return len(self._value)
84+
85+
def __getitem__(self, key):
86+
return self._value[key]
87+
88+
def __setitem__(self, key, item):
89+
if int(key) < 0 or int(key) > self.__len__():
90+
raise IndexError("odml.Property.__setitem__: key %i invalid for "
91+
"array of length %i" % (int(key), self.__len__()))
92+
try:
93+
val = dtypes.get(item, self.dtype)
94+
self._value[int(key)] = val
95+
except Exception:
96+
raise ValueError("odml.Property.__setitem__: passed value cannot be "
97+
"converted to data type \'%s\'!" % self._dtype)
98+
8699
@property
87100
def id(self):
88101
return self._id
@@ -494,22 +507,6 @@ def get_terminology_equivalent(self):
494507
except KeyError:
495508
return None
496509

497-
def __len__(self):
498-
return len(self._value)
499-
500-
def __getitem__(self, key):
501-
return self._value[key]
502-
503-
def __setitem__(self, key, item):
504-
if int(key) < 0 or int(key) > self.__len__():
505-
raise IndexError("odml.Property.__setitem__: key %i invalid for array of length %i"
506-
% (int(key), self.__len__()))
507-
try:
508-
val = dtypes.get(item, self.dtype)
509-
self._value[int(key)] = val
510-
except Exception:
511-
raise ValueError("odml.Property.__setitem__: passed value cannot be converted to data type \'%s\'!" % self._dtype)
512-
513510
def extend(self, obj, strict=True):
514511
"""
515512
Extend the list of values stored in this property by the passed values. Method

odml/section.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@
1212
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring
1313

1414

15-
class Section(base._baseobj):
16-
pass
17-
18-
1915
@allow_inherit_docstring
20-
class BaseSection(base.sectionable, Section):
16+
class BaseSection(base.Sectionable):
2117
""" An odML Section """
2218
type = None
2319
# id = None
@@ -59,8 +55,22 @@ def __init__(self, name, type=None, parent=None,
5955
self.parent = parent
6056

6157
def __repr__(self):
62-
return "<Section %s[%s] (%d)>" % (self._name, self.type,
63-
len(self._sections))
58+
return "<Section %s[%s] (%d)>" % (self._name, self.type, len(self._sections))
59+
60+
def __iter__(self):
61+
"""
62+
Iterate over each section and property contained in this section
63+
"""
64+
for section in self._sections:
65+
yield section
66+
for prop in self._props:
67+
yield prop
68+
69+
def __len__(self):
70+
"""
71+
Number of children (sections AND properties)
72+
"""
73+
return len(self._sections) + len(self._props)
6474

6575
@property
6676
def id(self):
@@ -243,9 +253,9 @@ def get_repository(self):
243253
return self.parent.get_repository()
244254
return super(BaseSection, self).repository
245255

246-
@base.sectionable.repository.setter
256+
@base.Sectionable.repository.setter
247257
def repository(self, url):
248-
base.sectionable.repository.fset(self, url)
258+
base.Sectionable.repository.fset(self, url)
249259

250260
@inherit_docstring
251261
def get_terminology_equivalent(self):
@@ -354,21 +364,6 @@ def remove(self, obj):
354364
else:
355365
raise ValueError("Can only remove sections and properties")
356366

357-
def __iter__(self):
358-
"""
359-
Iterate over each section and property contained in this section
360-
"""
361-
for section in self._sections:
362-
yield section
363-
for prop in self._props:
364-
yield prop
365-
366-
def __len__(self):
367-
"""
368-
Number of children (sections AND properties)
369-
"""
370-
return len(self._sections) + len(self._props)
371-
372367
def clone(self, children=True):
373368
"""
374369
Clone this object recursively allowing to copy it independently

odml/tools/odmlparser.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ def __init__(self, parser='XML'):
102102
self.parser = parser
103103
self.warnings = []
104104

105-
def is_valid_attribute(self, attr, fmt):
106-
if attr in fmt.arguments_keys:
107-
return attr
108-
109-
if fmt.revmap(attr):
110-
return attr
111-
112-
msg = "Invalid element <%s> inside <%s> tag" % (attr, fmt.__class__.__name__)
113-
print(msg)
114-
self.warnings.append(msg)
115-
return None
116-
117105
def from_file(self, file, doc_format=None):
118106

119107
if self.parser == 'XML':

0 commit comments

Comments
 (0)