getattr() checks for attribute membership, returns the value if not None and raises AttributeError when is not present. fixes #19
parent
ad46474663
commit
5dc049d65a
|
@ -78,7 +78,7 @@ class _STIXBase(collections.Mapping):
|
|||
raise DependentPropertiesError(self.__class__, failed_dependency_pairs)
|
||||
|
||||
def _check_object_constraints(self):
|
||||
if self.granular_markings:
|
||||
if hasattr(self, "granular_markings") and self.granular_markings:
|
||||
for m in self.granular_markings:
|
||||
# TODO: check selectors
|
||||
pass
|
||||
|
@ -124,7 +124,16 @@ class _STIXBase(collections.Mapping):
|
|||
|
||||
# Handle attribute access just like key access
|
||||
def __getattr__(self, name):
|
||||
return self.get(name)
|
||||
# If the requested attribute is not None return its value
|
||||
if self.get(name) is not None:
|
||||
return self.get(name)
|
||||
# Otherwise check the attribute exists inside object. If it exists
|
||||
# return None. If not raise the AttributeError.
|
||||
elif name in self._properties:
|
||||
return None
|
||||
|
||||
raise AttributeError("'%s' object has no attribute '%s'" %
|
||||
(self.__class__.__name__, name))
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name != '_inner' and not name.startswith("_STIXBase__"):
|
||||
|
|
Loading…
Reference in New Issue