Beispiel #1
0
    def selectRandom(self, key):
        """Method for getting a random selection from within a sub-category of the Selector.

        This method will attempt to randomly choose an entry for the specified sub-category in the Selector.

        It will fail if there are no ``true`` enabled entries in the sub-category or if the randomised function repeatedly
        fails to find a ``true`` enabled entry because they are too sparse.

        The number of attempts is limited by the size of the sub-category array.

        Args:
            key (str) : the name of a sub-category within the Selector's structure.

        Raises:
            RDJParameterErr : if unable to select a return value.
        """
        retval = None
        if key is not None:
            # How many entries are there in this category?
            try:
                sellen = len(self._data[key])
            except KeyError:
                print("KeyError in Selector for key: {0}".format(key))
                print(jStr(self._data))
                raise KeyError
            # Let's not try selecting on an empty set
            if sellen > 0:
                # If I can't select after that number of tries, there's a probably an issue
                ctr = sellen
                # Set a limit
                while ctr > 0:
                    src, incl = random.choice(list(self._data[key].items()))
                    if incl is True:
                        # We have a selection
                        retval = src
                        # Skip to the end.
                        ctr = 0
                    else:
                        # Marked as excluded
                        retval = None
                    # Need to avoid an infinite loop if ALL the entries got
                    # accidentally excluded.
                    ctr = ctr - 1
        # Houston, we have a problem
        if retval is None:
            raise robox.RDJParameterErr(
                "Unable to select a valid source for {0}".format(key))
        # Here's a result.
        print("{0} : {1}".format(key, retval))
        return retval
Beispiel #2
0
 def __str__(self):
     return jStr(self._data)
Beispiel #3
0
 def __str__(self):
     """Returns a string representation of the object variables.
     """
     return jStr(vars(self))