diff --git a/odml/base.py b/odml/base.py index abe7b5aa..a1109e81 100644 --- a/odml/base.py +++ b/odml/base.py @@ -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): """ @@ -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 @@ -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 @@ -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): @@ -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): """ @@ -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): """ @@ -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: diff --git a/odml/doc.py b/odml/doc.py index 02c3f8a3..f71217f6 100644 --- a/odml/doc.py +++ b/odml/doc.py @@ -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*. @@ -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 "" % (self._version, self._author, + len(self._sections)) + @property def id(self): """ @@ -107,11 +107,6 @@ def parent(self): """ The parent of a document is always None. """ return None - def __repr__(self): - return "" % (self._version, - self._author, - len(self._sections)) - def finalize(self): """ This needs to be called after the document is set up from parsing diff --git a/odml/property.py b/odml/property.py index a8ec7c56..2602dea2 100644 --- a/odml/property.py +++ b/odml/property.py @@ -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 @@ -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 @@ -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 diff --git a/odml/section.py b/odml/section.py index 8a9e9edc..95152238 100644 --- a/odml/section.py +++ b/odml/section.py @@ -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 @@ -59,8 +55,22 @@ def __init__(self, name, type=None, parent=None, self.parent = parent def __repr__(self): - return "
" % (self._name, self.type, - len(self._sections)) + return "
" % (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): @@ -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): @@ -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 diff --git a/odml/tools/odmlparser.py b/odml/tools/odmlparser.py index 641a52a9..c7aca64d 100644 --- a/odml/tools/odmlparser.py +++ b/odml/tools/odmlparser.py @@ -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':