Skip to content

Commit

Permalink
Add __repr__ methods to core objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Stimberg committed May 21, 2013
1 parent 15d0627 commit 5d9e454
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
8 changes: 8 additions & 0 deletions brian2/core/base.py
Expand Up @@ -150,6 +150,14 @@ def _set_active(self, val):
it for all `contained_objects`.
''')

def __repr__(self):
description = ('{classname}(when={scheduler}, name={name})')
return description.format(classname=self.__class__.__name__,
scheduler=repr(Scheduler(when=self.when,
clock=self.clock,
order=self.order)),
name=repr(self.name))

# This is a repeat from Nameable.name, but we want to get the documentation
# here again
name = Nameable.name
Expand Down
6 changes: 3 additions & 3 deletions brian2/core/clocks.py
Expand Up @@ -43,7 +43,7 @@ class Clock(Nameable):
basename = 'clock'
name = Nameable.name

@check_units(dt=second, t=second)
@check_units(dt=second)
def __init__(self, dt=None, name=None):
self._dt_spec = dt
self.i = 0 #: The time step of the simulation as an integer.
Expand All @@ -58,10 +58,10 @@ def reinit(self):
self.i = 0

def __str__(self):
return 'Clock: t = ' + str(self.t) + ', dt = ' + str(self.dt)
return 'Clock ' + self.name + ': t = ' + str(self.t) + ', dt = ' + str(self.dt)

def __repr__(self):
return 'Clock(dt=%s)' % (repr(self.dt),)
return 'Clock(dt=%r, name=%r)' % (self.dt, self.name)

def tick(self):
'''
Expand Down
4 changes: 4 additions & 0 deletions brian2/core/namespace.py
Expand Up @@ -195,6 +195,10 @@ def __contains__(self, key):
return True

return False

def __repr__(self):
return '<%s containing namespaces: %s>' % (self.__class__.__name__,
', '.join(self.namespaces.iterkeys()))

def get_default_numpy_namespace(N):
'''
Expand Down
5 changes: 5 additions & 0 deletions brian2/core/network.py
Expand Up @@ -331,3 +331,8 @@ def stop(self):
Stops the network from running, this is reset the next time `Network.run` is called.
'''
self._stopped = True

def __repr__(self):
return '<%s at time t=%s, containing objects: %s>' % (self.__class__.__name__,
str(self.t),
', '.join((obj.__repr__() for obj in self.objects)))
7 changes: 7 additions & 0 deletions brian2/core/scheduler.py
Expand Up @@ -105,3 +105,10 @@ def __init__(self, *args, **kwds):

#: Whether or not the user explicitly specified an order
self.defined_order = defined_order

def __repr__(self):
description = '{classname}(clock={clock}, when={when}, order={order})'
return description.format(classname=self.__class__.__name__,
clock=repr(self.clock),
when=repr(self.when),
order=repr(self.order))
67 changes: 67 additions & 0 deletions brian2/core/specifiers.py
Expand Up @@ -42,6 +42,9 @@ def __init__(self, name):
#: The name of the thing being specified (e.g. the model variable)
self.name = name

def __repr__(self):
return '%s(name=%r)' % (self.__class__.__name__, self.name)


class VariableSpecifier(Specifier):
'''
Expand Down Expand Up @@ -76,6 +79,16 @@ def __init__(self, name, unit, scalar=True, constant=False):
#: Whether the value is constant during a run
self.constant = constant


def __repr__(self):
description = ('{classname}(name={name}, unit={unit}, scalar={scalar}, '
'constant={constant})')
return description.format(classname=self.__class__.__name__,
name=repr(self.name),
unit=repr(self.unit),
scalar=repr(self.scalar),
constant=repr(self.constant))

class Value(VariableSpecifier):
'''
An object providing information about model variables that have an
Expand Down Expand Up @@ -117,6 +130,16 @@ def set_value(self):
'''
raise NotImplementedError()

def __repr__(self):
description = ('{classname}(name={name}, unit={unit}, dtype={dtype}, '
'scalar={scalar}, constant={constant})')
return description.format(classname=self.__class__.__name__,
name=repr(self.name),
unit=repr(self.unit),
dtype=repr(self.dtype),
scalar=repr(self.scalar),
constant=repr(self.constant))

###############################################################################
# Concrete classes that are used as specifiers in practice.
###############################################################################
Expand Down Expand Up @@ -157,6 +180,15 @@ def get_value(self):
def set_value(self):
raise TypeError('The value "%s" is read-only' % self.name)

def __repr__(self):
description = ('{classname}(name={name}, unit={unit}, dtype={dtype}, '
'value={value}')
return description.format(classname=self.__class__.__name__,
name=repr(self.name),
unit=repr(self.unit),
dtype=repr(self.dtype),
value=repr(self.value))


class StochasticVariable(VariableSpecifier):
'''
Expand Down Expand Up @@ -222,6 +254,17 @@ def __init__(self, name, unit, dtype, obj, attribute, constant=False):
def get_value(self):
return getattr(self.obj, self.attribute)

def __repr__(self):
description = ('{classname}(name={name}, unit={unit}, dtype={dtype}, '
'obj={obj}, attribute={attribute}, constant={constant})')
return description.format(classname=self.__class__.__name__,
name=repr(self.name),
unit=repr(self.unit),
dtype=repr(self.dtype),
obj=repr(self.obj),
attribute=repr(self.attribute),
constant=repr(self.constant))


class ArrayVariable(Value):
'''
Expand Down Expand Up @@ -270,6 +313,16 @@ def get_value(self):
def set_value(self, value):
self.array[:] = value

def __repr__(self):
description = ('<{classname}(name={name}, unit={unit}, dtype={dtype}, '
'array=<...>, index={index}, constant={constant})>')
return description.format(classname=self.__class__.__name__,
name=repr(self.name),
unit=repr(self.unit),
dtype=repr(self.dtype),
index=repr(self.index),
constant=self.constant)


class Subexpression(Value):
'''
Expand Down Expand Up @@ -327,6 +380,15 @@ def get_value(self):
def __contains__(self, var):
return var in self.identifiers

def __repr__(self):
description = ('<{classname}(name={name}, unit={unit}, dtype={dtype}, '
'expr={expr}, specifiers=<...>, namespace=<....>)>')
return description.format(classname=self.__class__.__name__,
name=repr(self.name),
unit=repr(self.unit),
dtype=repr(self.dtype),
expr=repr(self.expr))


class Index(Specifier):
'''
Expand All @@ -352,3 +414,8 @@ def __init__(self, name, iterate_all=True):
'is type %s instead' % type(all)))
#: Whether the index varies over the whole of an input vector
self.iterate_all = iterate_all

def __repr__(self):
return '%s(name=%r, iterate_all=%r)' % (self.__class__.__name__,
self.name,
self.iterate_all)

0 comments on commit 5d9e454

Please sign in to comment.