2
2
"""
3
3
This module provides base classes for functionality common to odML objects.
4
4
"""
5
+ import copy
5
6
import posixpath
6
7
7
8
try :
@@ -36,9 +37,10 @@ def __eq__(self, obj):
36
37
return False
37
38
38
39
for key in self ._format :
39
- if key == "id" or key == "oid" :
40
+ if key in [ "id" , "oid" ] :
40
41
continue
41
- elif getattr (self , key ) != getattr (obj , key ):
42
+
43
+ if getattr (self , key ) != getattr (obj , key ):
42
44
return False
43
45
44
46
return True
@@ -87,7 +89,6 @@ def clone(self, children=True):
87
89
from this class.
88
90
"""
89
91
# TODO don't we need some recursion / deepcopy here?
90
- import copy
91
92
obj = copy .copy (self )
92
93
return obj
93
94
@@ -149,6 +150,8 @@ def __contains__(self, key):
149
150
if (hasattr (obj , "name" ) and obj .name == key ) or key == obj :
150
151
return True
151
152
153
+ return False
154
+
152
155
def __eq__ (self , obj ):
153
156
"""
154
157
SmartList attributes of 'sections' and 'properties' are
@@ -292,7 +295,7 @@ def extend(self, sec_list):
292
295
if not isinstance (sec , BaseSection ):
293
296
raise ValueError ("Can only extend objects of type Section." )
294
297
295
- elif isinstance (sec , BaseSection ) and sec .name in self ._sections :
298
+ if isinstance (sec , BaseSection ) and sec .name in self ._sections :
296
299
raise KeyError ("Section with name '%s' already exists." % sec .name )
297
300
298
301
for sec in sec_list :
@@ -356,9 +359,9 @@ def iterproperties(self, max_depth=None, filter_func=lambda x: True):
356
359
iterable. Yields iterable if function returns True
357
360
:type filter_func: function
358
361
"""
359
- for sec in [ s for s in self .itersections (max_depth = max_depth ,
360
- yield_self = True )]:
361
- if hasattr (sec , "properties" ): # not to fail if odml.Document
362
+ for sec in list ( self .itersections (max_depth = max_depth , yield_self = True )):
363
+ # Avoid fail with an odml.Document
364
+ if hasattr (sec , "properties" ):
362
365
for i in sec .properties :
363
366
if filter_func (i ):
364
367
yield i
@@ -380,7 +383,7 @@ def itervalues(self, max_depth=None, filter_func=lambda x: True):
380
383
iterable. Yields iterable if function returns True
381
384
:type filter_func: function
382
385
"""
383
- for prop in [ p for p in self .iterproperties (max_depth = max_depth )] :
386
+ for prop in list ( self .iterproperties (max_depth = max_depth )) :
384
387
if filter_func (prop .values ):
385
388
yield prop .values
386
389
@@ -478,9 +481,10 @@ def _get_section_by_path(self, path):
478
481
479
482
if found :
480
483
return found ._get_section_by_path ("/" .join (pathlist [1 :]))
484
+
481
485
raise ValueError ("Section named '%s' does not exist" % pathlist [0 ])
482
- else :
483
- return self ._match_iterable (self .sections , pathlist [0 ])
486
+
487
+ return self ._match_iterable (self .sections , pathlist [0 ])
484
488
485
489
def find (self , key = None , type = None , findAll = False , include_subtype = False ):
486
490
"""
0 commit comments