class agent:
'''Class for agents who have friends and goals.'''
count = 0
agents = []
def __init__(self, name):
''' Constructor for agent with name.'''
self.name = name
self.friends = []
self.goals = []
self.adoptedgoals = []
self.count = agent.count
agent.count += 1
agent.agents.append(self)
def __repr__(self):
''' Print out agent so that it can evaluate to itself.'''
return "agent('{}')".format(self.name)
def __str__(self):
'''Return agent as a string.'''
return "<agent. name: {} ({})>".format(self.name, self.count)
def add_friend(self, friend):
'''Add a friend without duplicates. Also add reciprocal friend.'''
if not friend in self.friends:
self.friends.append(friend)
friend.add_friend(self)
def add_goal(self, goal):
'''Add goals (stances) without duplicates.'''
if not goal in self.goals:
self.goals.append(goal)
def pp(self):
'''Pretty print agent information.'''
result = "Name:\t" + self.name
if self.friends:
result += "\nFriends:\t" + str(self.friends)
if self.goals:
result += "\nGoals:\t"+ str(self.goals)
if self.adoptedgoals:
result += "\nAdopted: \t" + str(self.adoptedgoals)
return result
def adopt_goals(self):
''' Go through each friend and add that friend's goals to the adoptedgoals list, without duplication.'''
for f in self.friends:
for g in f.goals:
if not g in self.adoptedgoals:
self.adoptedgoals.append(g)
def __eq__(self, other):
''' Overload == operator. Are two agents equal by name, goals, and friends? '''
return self.name == other.name and self.goals == other.goals and self.friends == other.friends
def copy(self):
''' Clone the agent, including name, goals, and friends. '''
newagent = agent(self.name)
newagent.goals = self.goals[:]
newagent.friends = self.friends[:]
return newagent
def agreewith(self, other):
''' Return sorted list of stances on which the agents agree, except for importance.
No Duplicates. '''
result = []
selfgoals = list(set(self.goals + self.adoptedgoals))
othergoals = list(set(other.goals + other.adoptedgoals))
for g in selfgoals:
for og in othergoals:
if g == og:
result.append(g)
return sorted(result)
def disagreewith(self, other):
''' Return sorted list of stances on which agents disagree, except for importance.
No Duplicates. '''
result = []
selfgoals = list(set(self.goals + self.adoptedgoals))
othergoals = list(set(other.goals + other.adoptedgoals))
for g in selfgoals:
for og in othergoals:
if g != og:
result.append(g)
return sorted(result)