interactions_table.py
Go to the documentation of this file.
00001 #
00002 # License: BSD
00003 #   https://raw.github.com/robotics-in-concert/rocon_qt_gui/license/LICENSE
00004 #
00005 ##############################################################################
00006 # Description
00007 ##############################################################################
00008 
00009 """
00010 .. module:: interactions_table
00011    :platform: Unix
00012    :synopsis: A database of interactions.
00013 
00014 
00015 This module provides a class that acts as a database (dictionary style) of
00016 some set of interactions.
00017 
00018 ----
00019 
00020 """
00021 ##############################################################################
00022 # Imports
00023 ##############################################################################
00024 
00025 import rocon_console.console as console
00026 
00027 ##############################################################################
00028 # Classes
00029 ##############################################################################
00030 
00031 
00032 class InteractionsTable(object):
00033     '''
00034       The runtime populated interactions table along with methods to
00035       manipulate it.
00036 
00037       .. include:: weblinks.rst
00038     '''
00039     __slots__ = [
00040         'interactions',  # rocon_interactions.interactions.Interaction[]
00041     ]
00042 
00043     def __init__(self, filter_pairing_interactions=False):
00044         """
00045         Constructs an empty interactions table.
00046 
00047         :param bool filter_pairing_interactions: do not load any paired interactions
00048         """
00049         self.interactions = []
00050         """List of :class:`.Interaction` objects that will form the elements of the table."""
00051 
00052     def roles(self):
00053         '''
00054           List all roles for the currently stored interactions.
00055 
00056           :returns: a list of all roles
00057           :rtype: str[]
00058         '''
00059         # uniquify the list
00060         return list(set([i.role for i in self.interactions]))
00061 
00062     def __len__(self):
00063         return len(self.interactions)
00064 
00065     def __str__(self):
00066         """
00067         Convenient string representation of the table.
00068         """
00069         s = ''
00070         for interaction in self.interactions:
00071             s += "\n".join("  " + i for i in str(interaction).splitlines()) + '\n'
00072         return s
00073 
00074     def generate_role_view(self, role_name):
00075         '''
00076         Creates a temporary copy of interactions filtered by the specified role
00077         and sorts them into a dictionary view keyed by hash. This is a convenient
00078         object for use by the interactions chooser.
00079 
00080         :param str role_name: the filter for retrieving interactions
00081 
00082         :returns: A role based view of the interactions
00083         :rtype: dict { hash : :class:`.interactions.Interaction` }
00084         '''
00085         # there's got to be a faster way of doing this.
00086         role_view = {}
00087         for interaction in self.interactions:
00088             if interaction.role == role_name:
00089                 role_view[interaction.hash] = interaction
00090         return role_view
00091 
00092     def clear(self, role_name):
00093         """
00094         Clear all interactions belonging to this role.
00095 
00096         :param str role_name:
00097         """
00098         self.interactions[:] = [i for i in self.interactions if i.role != role_name]
00099 
00100     def append(self, interaction):
00101         """
00102         Append an interaction to the table.
00103 
00104         :param :class:`.Interaction` interaction:
00105         """
00106         matches = [i for i in self.interactions if i.hash == interaction.hash]
00107         if not matches:
00108             self.interactions.append(interaction)
00109         else:
00110             console.logwarn("Interactions Table : tried to append an already existing interaction [%s]" % interaction.hash)
00111 
00112     def find(self, interaction_hash):
00113         '''
00114         Find the specified interaction.
00115 
00116         :param str interaction_hash: in crc32 format
00117 
00118         :returns: interaction if found, None otherwise.
00119         :rtype: :class:`.Interaction` or None
00120         '''
00121         interaction = next((interaction for interaction in self.interactions
00122                             if interaction.hash == interaction_hash), None)
00123         return interaction


rocon_remocon
Author(s): Daniel Stonier, Donguk Lee
autogenerated on Fri Feb 12 2016 02:50:18