Skip to content
71 changes: 32 additions & 39 deletions odml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,14 @@
from .tools.doc_inherit import allow_inherit_docstring


class _baseobj(object):
pass


class baseobject(_baseobj):
class BaseObject(object):
_format = None

def format(self):
return self._format

@property
def document(self):
""" Returns the Document object in which this object is contained """
if self.parent is None:
return None
return self.parent.document

def get_terminology_equivalent(self):
def __hash__(self):
"""
Returns the equivalent object in an terminology (should there be one
defined) or None
Allow all odML objects to be hash-able.
"""
return None
return id(self)

def __eq__(self, obj):
"""
Expand All @@ -40,9 +25,6 @@ def __eq__(self, obj):
unique within a document.
"""
# cannot compare totally different stuff
if not isinstance(obj, _baseobj):
return False

if not isinstance(self, obj.__class__):
return False

Expand All @@ -60,6 +42,23 @@ def __ne__(self, obj):
"""
return not self == obj

def format(self):
return self._format

@property
def document(self):
""" Returns the Document object in which this object is contained """
if self.parent is None:
return None
return self.parent.document

def get_terminology_equivalent(self):
"""
Returns the equivalent object in an terminology (should there be one
defined) or None
"""
return None

def clean(self):
"""
Stub that doesn't do anything for this class
Expand All @@ -77,12 +76,6 @@ def clone(self, children=True):
obj = copy.copy(self)
return obj

def __hash__(self):
"""
Allow all odML objects to be hash-able.
"""
return id(self)


class SmartList(list):

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


@allow_inherit_docstring
class sectionable(baseobject):
class Sectionable(BaseObject):
def __init__(self):
from odml.section import BaseSection
self._sections = SmartList(BaseSection)
self._repository = None

def __getitem__(self, key):
return self._sections[key]

def __len__(self):
return len(self._sections)

def __iter__(self):
return self._sections.__iter__()

@property
def document(self):
"""
Expand Down Expand Up @@ -249,15 +251,6 @@ def remove(self, section):
self._sections.remove(section)
section._parent = None

def __getitem__(self, key):
return self._sections[key]

def __len__(self):
return len(self._sections)

def __iter__(self):
return self._sections.__iter__()

def itersections(self, recursive=True, yield_self=False,
filter_func=lambda x: True, max_depth=None):
"""
Expand Down Expand Up @@ -565,7 +558,7 @@ def clone(self, children=True):
to another document
"""
from odml.section import BaseSection
obj = super(sectionable, self).clone(children)
obj = super(Sectionable, self).clone(children)
obj._parent = None
obj._sections = SmartList(BaseSection)
if children:
Expand Down
15 changes: 5 additions & 10 deletions odml/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring


class Document(base._baseobj):
pass


@allow_inherit_docstring
class BaseDocument(base.sectionable, Document):
class BaseDocument(base.Sectionable):
"""
A representation of an odML document in memory.
Its odml attributes are: *author*, *date*, *version* and *repository*.
Expand Down Expand Up @@ -41,6 +37,10 @@ def __init__(self, author=None, date=None, version=None, repository=None, id=Non
self._date = None
self.date = date

def __repr__(self):
return "<Doc %s by %s (%d sections)>" % (self._version, self._author,
len(self._sections))

@property
def id(self):
"""
Expand Down Expand Up @@ -107,11 +107,6 @@ def parent(self):
""" The parent of a document is always None. """
return None

def __repr__(self):
return "<Doc %s by %s (%d sections)>" % (self._version,
self._author,
len(self._sections))

def finalize(self):
"""
This needs to be called after the document is set up from parsing
Expand Down
39 changes: 18 additions & 21 deletions odml/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring


class Property(base._baseobj):
pass


@allow_inherit_docstring
class BaseProperty(base.baseobject, Property):
class BaseProperty(base.BaseObject):
"""An odML Property"""
_format = frmt.Property

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

self.parent = parent

def __len__(self):
return len(self._value)

def __getitem__(self, key):
return self._value[key]

def __setitem__(self, key, item):
if int(key) < 0 or int(key) > self.__len__():
raise IndexError("odml.Property.__setitem__: key %i invalid for "
"array of length %i" % (int(key), self.__len__()))
try:
val = dtypes.get(item, self.dtype)
self._value[int(key)] = val
except Exception:
raise ValueError("odml.Property.__setitem__: passed value cannot be "
"converted to data type \'%s\'!" % self._dtype)

@property
def id(self):
return self._id
Expand Down Expand Up @@ -494,22 +507,6 @@ def get_terminology_equivalent(self):
except KeyError:
return None

def __len__(self):
return len(self._value)

def __getitem__(self, key):
return self._value[key]

def __setitem__(self, key, item):
if int(key) < 0 or int(key) > self.__len__():
raise IndexError("odml.Property.__setitem__: key %i invalid for array of length %i"
% (int(key), self.__len__()))
try:
val = dtypes.get(item, self.dtype)
self._value[int(key)] = val
except Exception:
raise ValueError("odml.Property.__setitem__: passed value cannot be converted to data type \'%s\'!" % self._dtype)

def extend(self, obj, strict=True):
"""
Extend the list of values stored in this property by the passed values. Method
Expand Down
43 changes: 19 additions & 24 deletions odml/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring


class Section(base._baseobj):
pass


@allow_inherit_docstring
class BaseSection(base.sectionable, Section):
class BaseSection(base.Sectionable):
""" An odML Section """
type = None
# id = None
Expand Down Expand Up @@ -59,8 +55,22 @@ def __init__(self, name, type=None, parent=None,
self.parent = parent

def __repr__(self):
return "<Section %s[%s] (%d)>" % (self._name, self.type,
len(self._sections))
return "<Section %s[%s] (%d)>" % (self._name, self.type, len(self._sections))

def __iter__(self):
"""
Iterate over each section and property contained in this section
"""
for section in self._sections:
yield section
for prop in self._props:
yield prop

def __len__(self):
"""
Number of children (sections AND properties)
"""
return len(self._sections) + len(self._props)

@property
def id(self):
Expand Down Expand Up @@ -243,9 +253,9 @@ def get_repository(self):
return self.parent.get_repository()
return super(BaseSection, self).repository

@base.sectionable.repository.setter
@base.Sectionable.repository.setter
def repository(self, url):
base.sectionable.repository.fset(self, url)
base.Sectionable.repository.fset(self, url)

@inherit_docstring
def get_terminology_equivalent(self):
Expand Down Expand Up @@ -354,21 +364,6 @@ def remove(self, obj):
else:
raise ValueError("Can only remove sections and properties")

def __iter__(self):
"""
Iterate over each section and property contained in this section
"""
for section in self._sections:
yield section
for prop in self._props:
yield prop

def __len__(self):
"""
Number of children (sections AND properties)
"""
return len(self._sections) + len(self._props)

def clone(self, children=True):
"""
Clone this object recursively allowing to copy it independently
Expand Down
12 changes: 0 additions & 12 deletions odml/tools/odmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ def __init__(self, parser='XML'):
self.parser = parser
self.warnings = []

def is_valid_attribute(self, attr, fmt):
if attr in fmt.arguments_keys:
return attr

if fmt.revmap(attr):
return attr

msg = "Invalid element <%s> inside <%s> tag" % (attr, fmt.__class__.__name__)
print(msg)
self.warnings.append(msg)
return None

def from_file(self, file, doc_format=None):

if self.parser == 'XML':
Expand Down