Skip to content

Commit 92e5781

Browse files
authored
Merge pull request #355 from mpsonntag/cleanup
General cleanup Awesome work!
2 parents a8941de + fb84454 commit 92e5781

25 files changed

+1262
-555
lines changed

.travis.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ dist: trusty
44
language: python
55

66
matrix:
7+
allow_failures:
8+
- os: linux
9+
python: "3.9-dev"
10+
dist: bionic
11+
712
include:
813
- os: linux
914
python: "2.7"
@@ -18,7 +23,14 @@ matrix:
1823
- os: linux
1924
python: "3.8"
2025
dist: xenial
26+
- os: linux
27+
python: "3.9-dev"
28+
dist: bionic
2129

30+
- os: osx
31+
language: generic
32+
env:
33+
- OSXENV=2.7.14
2234
- os: osx
2335
language: generic
2436
env:
@@ -30,7 +42,7 @@ matrix:
3042
- os: osx
3143
language: generic
3244
env:
33-
- OSXENV=2.7.14
45+
- OSXENV=3.8.0
3446

3547
install:
3648
- export PYVER=${TRAVIS_PYTHON_VERSION:0:1}

appveyor.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ environment:
1313
- PYTHON: "C:\\Python37"
1414
PYVER: 3
1515
BITS: 32
16+
- PYTHON: "C:\\Python38"
17+
PYVER: 3
18+
BITS: 32
1619
- PYTHON: "C:\\Python27-x64"
1720
PYVER: 2
1821
BITS: 64
@@ -22,6 +25,9 @@ environment:
2225
- PYTHON: "C:\\Python37-x64"
2326
PYVER: 3
2427
BITS: 64
28+
- PYTHON: "C:\\Python38-x64"
29+
PYVER: 3
30+
BITS: 64
2531

2632
init:
2733
- "ECHO %PYTHON% %vcvars% (%bits%)"

odml/base.py

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
# -*- coding: utf-8
22
"""
3-
Collects common base functionality
3+
This module provides base classes for functionality common to odML objects.
44
"""
55
import posixpath
66

7-
from . import terminology
8-
from .tools.doc_inherit import allow_inherit_docstring
9-
107
try:
118
from collections.abc import Iterable
129
except ImportError:
1310
from collections import Iterable
1411

12+
from . import terminology
13+
from .tools.doc_inherit import allow_inherit_docstring
14+
15+
1516
class BaseObject(object):
17+
"""
18+
Base class for all odML objects.
19+
"""
1620
_format = None
1721

1822
def __hash__(self):
@@ -41,16 +45,21 @@ def __eq__(self, obj):
4145

4246
def __ne__(self, obj):
4347
"""
44-
Use the __eq__ function to determine if both objects are equal
48+
Use the __eq__ function to determine if both objects are equal.
4549
"""
4650
return not self == obj
4751

4852
def format(self):
53+
"""
54+
Returns the format class of the current object.
55+
"""
4956
return self._format
5057

5158
@property
5259
def document(self):
53-
""" Returns the Document object in which this object is contained """
60+
"""
61+
Returns the Document object in which this object is contained.
62+
"""
5463
if self.parent is None:
5564
return None
5665
return self.parent.document
@@ -64,15 +73,18 @@ def get_terminology_equivalent(self):
6473

6574
def clean(self):
6675
"""
67-
Stub that doesn't do anything for this class
76+
Stub that doesn't do anything for this class.
6877
"""
6978
pass
7079

7180
def clone(self, children=True):
7281
"""
7382
Clone this object recursively (if children is True) allowing to copy it
7483
independently to another document. If children is False, this acts as
75-
a template cloner, creating a copy of the object without any children
84+
a template cloner, creating a copy of the object without any children.
85+
86+
:param children: True by default. Is used in the classes that inherit
87+
from this class.
7688
"""
7789
# TODO don't we need some recursion / deepcopy here?
7890
import copy
@@ -81,6 +93,9 @@ def clone(self, children=True):
8193

8294

8395
class SmartList(list):
96+
"""
97+
List class that can hold odml.Sections and odml.Properties.
98+
"""
8499

85100
def __init__(self, content_type):
86101
"""
@@ -108,7 +123,7 @@ def __getitem__(self, key):
108123
def __setitem__(self, key, value):
109124
"""
110125
Replaces item at list[*key*] with *value*.
111-
:param key: index position
126+
:param key: index position.
112127
:param value: object that replaces item at *key* position.
113128
value has to be of the same content type as the list.
114129
In this context usually a Section or a Property.
@@ -152,17 +167,17 @@ def __eq__(self, obj):
152167

153168
def __ne__(self, obj):
154169
"""
155-
Use the __eq__ function to determine if both objects are equal
170+
Use the __eq__ function to determine if both objects are equal.
156171
"""
157172
return not self == obj
158173

159174
def index(self, obj):
160175
"""
161-
Find obj in list
176+
Find obj in list.
162177
"""
163-
for i, e in enumerate(self):
164-
if e is obj:
165-
return i
178+
for idx, val in enumerate(self):
179+
if val is obj:
180+
return idx
166181
raise ValueError("remove: %s not in list" % repr(obj))
167182

168183
def remove(self, obj):
@@ -193,6 +208,9 @@ def sort(self, key=lambda x: x.name, reverse=False):
193208

194209
@allow_inherit_docstring
195210
class Sectionable(BaseObject):
211+
"""
212+
Base class for all odML objects that can store odml.Sections.
213+
"""
196214
def __init__(self):
197215
from odml.section import BaseSection
198216
self._sections = SmartList(BaseSection)
@@ -210,18 +228,20 @@ def __iter__(self):
210228
@property
211229
def document(self):
212230
"""
213-
Returns the parent-most node (if its a document instance) or None
231+
Returns the parent-most node (if its a document instance) or None.
214232
"""
215233
from odml.doc import BaseDocument
216-
p = self
217-
while p.parent:
218-
p = p.parent
219-
if isinstance(p, BaseDocument):
220-
return p
234+
par = self
235+
while par.parent:
236+
par = par.parent
237+
if isinstance(par, BaseDocument):
238+
return par
221239

222240
@property
223241
def sections(self):
224-
""" The list of sections contained in this section/document """
242+
"""
243+
The list of sections contained in this section/document.
244+
"""
225245
return self._sections
226246

227247
def insert(self, position, section):
@@ -301,6 +321,7 @@ def itersections(self, recursive=True, yield_self=False,
301321
:param filter_func: accepts a function that will be applied to each
302322
iterable. Yields iterable if function returns True
303323
:type filter_func: function
324+
:param max_depth: number of layers in the document tree to include in the search.
304325
"""
305326
stack = []
306327
# Below: never yield self if self is a Document
@@ -422,6 +443,10 @@ def _match_iterable(self, iterable, key):
422443
"""
423444
Searches for a key match within a given iterable.
424445
Raises ValueError if not found.
446+
447+
:param iterable: list of odML objects.
448+
:param key: string to search an objects name against.
449+
:returns: odML object that matched the key.
425450
"""
426451
for obj in iterable:
427452
if self._matches(obj, key):
@@ -458,17 +483,25 @@ def _get_section_by_path(self, path):
458483
return self._match_iterable(self.sections, pathlist[0])
459484

460485
def find(self, key=None, type=None, findAll=False, include_subtype=False):
461-
""" Return the first subsection named *key* of type *type* """
486+
"""
487+
Returns the first subsection named *key* of type *type*.
488+
489+
:param key: string to search against an odML objects name.
490+
:param type: type of an odML object.
491+
:param findAll: include further matches after the first one in the result.
492+
:param include_subtype: splits an objects type at '/' and matches the parts
493+
against the provided type.
494+
"""
462495
ret = []
463496
if type:
464497
type = type.lower()
465498

466-
for s in self._sections:
467-
if self._matches(s, key, type, include_subtype=include_subtype):
499+
for sec in self._sections:
500+
if self._matches(sec, key, type, include_subtype=include_subtype):
468501
if findAll:
469-
ret.append(s)
502+
ret.append(sec)
470503
else:
471-
return s
504+
return sec
472505
if ret:
473506
return ret
474507

@@ -533,7 +566,7 @@ def find_related(self, key=None, type=None, children=True, siblings=True,
533566

534567
def get_path(self):
535568
"""
536-
Returns the absolute path of this section
569+
Returns the absolute path of this section.
537570
"""
538571
node = self
539572
path = []
@@ -543,36 +576,36 @@ def get_path(self):
543576
return "/" + "/".join(path)
544577

545578
@staticmethod
546-
def _get_relative_path(a, b):
579+
def _get_relative_path(path_a, path_b):
547580
"""
548-
Returns a relative path for navigation from dir *a* to dir *b*
581+
Returns a relative path for navigation from *path_a* to *path_b*.
549582
550-
If the common parent of both is "/", return an absolute path
583+
If the common parent of both is "/", return an absolute path.
551584
"""
552-
a += "/"
553-
b += "/"
554-
parent = posixpath.dirname(posixpath.commonprefix([a, b]))
585+
path_a += "/"
586+
path_b += "/"
587+
parent = posixpath.dirname(posixpath.commonprefix([path_a, path_b]))
555588
if parent == "/":
556-
return b[:-1]
589+
return path_b[:-1]
557590

558-
a = posixpath.relpath(a, parent)
559-
b = posixpath.relpath(b, parent)
560-
if a == ".":
561-
return b
591+
path_a = posixpath.relpath(path_a, parent)
592+
path_b = posixpath.relpath(path_b, parent)
593+
if path_a == ".":
594+
return path_b
562595

563-
return posixpath.normpath("../" * (a.count("/") + 1) + b)
596+
return posixpath.normpath("../" * (path_a.count("/") + 1) + path_b)
564597

565598
def get_relative_path(self, section):
566599
"""
567600
Returns a relative (file)path to point to section
568601
like (e.g. ../other_section)
569602
570603
If the common parent of both sections is the document (i.e. /),
571-
return an absolute path
604+
return an absolute path.
572605
"""
573-
a = self.get_path()
574-
b = section.get_path()
575-
return self._get_relative_path(a, b)
606+
path_a = self.get_path()
607+
path_b = section.get_path()
608+
return self._get_relative_path(path_a, path_b)
576609

577610
def clean(self):
578611
"""
@@ -586,22 +619,24 @@ def clean(self):
586619

587620
def clone(self, children=True, keep_id=False):
588621
"""
589-
Clone this object recursively allowing to copy it independently
590-
to another document
622+
Clones this object recursively allowing to copy it independently
623+
to another document.
591624
"""
592625
from odml.section import BaseSection
593626
obj = super(Sectionable, self).clone(children)
594627
obj._parent = None
595628
obj._sections = SmartList(BaseSection)
596629
if children:
597-
for s in self._sections:
598-
obj.append(s.clone(keep_id=keep_id))
630+
for sec in self._sections:
631+
obj.append(sec.clone(keep_id=keep_id))
599632

600633
return obj
601634

602635
@property
603636
def repository(self):
604-
""" A URL to a terminology. """
637+
"""
638+
A URL to a terminology.
639+
"""
605640
return self._repository
606641

607642
@repository.setter

0 commit comments

Comments
 (0)