Skip to content

Commit

Permalink
Make the __getattr__ mechanism in StateMonitor Python3-compatible (we…
Browse files Browse the repository at this point in the history
… should document/implement a standard mechanism for this, we are using it in several places)
  • Loading branch information
mstimberg committed Jul 23, 2013
1 parent 2ad5792 commit b0f3183
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions brian2/monitors/statemonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def __init__(self, source, variables, record=None, when=None,
scheduler.clock = source.clock
if not scheduler.defined_when:
scheduler.when = 'end'

BrianObject.__init__(self, when=scheduler, name=name)

# variables should always be a list of strings
if variables is True:
variables = source.equations.names
Expand Down Expand Up @@ -120,6 +121,8 @@ def __init__(self, source, variables, record=None, when=None,
index='', group=None,
constant=True)

self._group_attribute_access_active = True

def reinit(self):
self._values = dict((v, DynamicArray((0, len(self.indices)),
use_numpy_resize=True,
Expand Down Expand Up @@ -150,6 +153,17 @@ def update(self):
self.codeobj()

def __getattr__(self, item):
# We do this because __setattr__ and __getattr__ are not active until
# _group_attribute_access_active attribute is set, and if it is set,
# then __getattr__ will not be called. Therefore, if getattr is called
# with this name, it is because it hasn't been set yet and so this
# method should raise an AttributeError to agree that it hasn't been
# called yet.
if item == '_group_attribute_access_active':
raise AttributeError
if not hasattr(self, '_group_attribute_access_active'):
raise AttributeError

# TODO: Decide about the interface
if item == 't':
return Quantity(self._t.data.copy(), dim=second.dim)
Expand All @@ -165,7 +179,7 @@ def __getattr__(self, item):
elif item.endswith('_') and item[:-1] in self.variables:
return self._values[item[:-1]].data.copy()
else:
getattr(super(StateMonitor, self), item)
raise AttributeError('Unknown attribute %s' % item)

def __repr__(self):
description = '<{classname}, recording {variables} from {source}>'
Expand Down

0 comments on commit b0f3183

Please sign in to comment.