Skip to content

Commit

Permalink
Clean __str__ and __repr__ methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Kolbe committed Jan 9, 2014
1 parent d1f1bcb commit dd5763b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion goap/src/goap/common_ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, state_name, topic, topic_class, field):
self._value = None

def __repr__(self):
return '<ROSTopicCondition topic=%s field=%s>' % (self._topic, self._field)
return '<%s topic=%s field=%s>' % (self.__class__.__name__, self._topic, self._field)

def _callback(self, msg):
self._value = self._msgeval(msg)
Expand Down
31 changes: 28 additions & 3 deletions goap/src/goap/goap.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def __init__(self, worldstate=None):
if worldstate is not None:
self._condition_values.update(worldstate._condition_values)

def __str__(self):
return '%s %s' % (self.__class__.__name__, self._condition_values)

def __repr__(self):
return '<WorldState %X values=%s>' % (id(self), self._condition_values)

Expand Down Expand Up @@ -95,7 +98,10 @@ def __init__(self, state_name):
self._state_name = state_name

def __str__(self):
return "Condition:%s" % self._state_name
return '%s:%s' % (self.__class__.__name__, self._state_name)

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

def get_value(self):
"""Returns the current value, hopefully not blocking."""
Expand Down Expand Up @@ -139,8 +145,11 @@ def __init__(self, condition, value, deviation=None):
self._value = value
self._deviation = deviation

def __str__(self):
return '%s:%s=%s~%s' % (self.__class__.__name__, self._condition._state_name, self._value, self._deviation)

def __repr__(self):
return '<Precondition cond=%s value=%s dev=%s>' % (self._condition, self._value, self._deviation)
return '<%s cond=%s value=%r dev=%s>' % (self.__class__.__name__, self._condition, self._value, self._deviation)

def is_valid(self, worldstate):
cond_value = worldstate.get_condition_value(self._condition)
Expand All @@ -163,6 +172,12 @@ def __init__(self, condition, new_value):
self._condition = condition
self._new_value = new_value

def __str__(self):
return '%s:%s=%s' % (self.__class__.__name__, self._condition._state_name, self._new_value)

def __repr__(self):
return '<%s cond=%s new_val=%s>' % (self.__class__.__name__, self._condition, self._new_value)

def apply_to(self, worldstate):
# TODO: remove me as I'm only for forward planning?
worldstate.set_condition_value(self._condition, self._new_value)
Expand All @@ -178,6 +193,12 @@ def __init__(self, condition):
# Effect.__init__(self, condition, None)
self._condition = condition

def __str__(self):
return '%s:%s' % (self.__class__.__name__, self._condition._state_name)

def __repr__(self):
return '<%s cond=%s>' % (self.__class__.__name__, self._condition)

# def apply_to(self, worldstate):
# worldstate.memory.set_value(self._condition, self._new_value)

Expand Down Expand Up @@ -214,8 +235,12 @@ def __init__(self, preconditions, effects):
self._preconditions = preconditions
self._effects = effects


def __str__(self):
return self.__class__.__name__

def __repr__(self):
return '<Action type=%s>' % self.__class__.__name__
return '<%s preconditions=%s effects=%s>' % (self.__class__.__name__, self._preconditions, self._effects)

def cost(self):
return 1
Expand Down
20 changes: 15 additions & 5 deletions goap/src/goap/inheriting.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def __init__(self, memory, variable, new_value, preconditions, effects):
self._state_name = 'memory.' + self._variable
self._memory.declare_variable(self._variable)

def __repr__(self):
return '<%s var=%s new=%s>' % (self.__class__.__name__, self._variable, self._new_value)

def run(self, next_worldstate):
self._memory.set_value(self._variable, self._new_value)

Expand All @@ -93,8 +96,13 @@ def __init__(self, memory, variable, old_value, new_value):
)
self._old_value = old_value

def __str__(self):
return '%s:%s=%s->%s' % (self.__class__.__name__,
self._variable, self._old_value, self._new_value)

def __repr__(self):
return '<MemoryChangeVarAction var=%s old=%s new=%s>' % (self._variable, self._old_value, self._new_value)
return '<%s var=%s old=%s new=%s>' % (self.__class__.__name__,
self._variable, self._old_value, self._new_value)


class MemoryIncrementerAction(Action):
Expand All @@ -114,8 +122,13 @@ def __init__(self, memory, variable, increment=1):
self._increment = increment
self._memory.declare_variable(self._variable)

def __str__(self):
return '%s:%s+=%s' % (self.__class__.__name__,
self._variable, self._increment)

def __repr__(self):
return '<MemoryIncrementerAction var=%s incr=%s>' % (self._variable, self._increment)
return '<%s var=%s incr=%s>' % (self.__class__.__name__,
self._variable, self._increment)

def cost(self):
return abs(self._increment)
Expand Down Expand Up @@ -143,9 +156,6 @@ def __init__(self, memory, variable):
self._variable = variable
memory.declare_variable(self._state_name)

def __repr__(self):
return '<MemoryCondition var=%s>' % self._variable

def get_value(self):
return self._memory.get_value(self._state_name)

6 changes: 4 additions & 2 deletions goap/src/goap/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def __init__(self, worldstate, action, parent_nodes_path_list, parent_actions_pa
self.parent_actions_path_list = parent_actions_path_list

def __repr__(self):
return '<Node %X cost=%s action=%s worldstate=%s>' % \
(id(self), self.cost(), self.action, self.worldstate)
# return '<Node %X cost=%s action=%s worldstate=%s>' % \
# (id(self), self.cost(), self.action, self.worldstate)
return '<Node %X cost=%s action=%s>' % \
(id(self), self.cost(), self.action)

def cost(self):
return len(self.parent_nodes_path_list) + \
Expand Down

0 comments on commit dd5763b

Please sign in to comment.