Example #1
0
 def _readObservationFile(self):
     """Docstring for readActionFile """
     csvObj = CSVUtil.readCSV(self.observationFile, self.delimiter)
     rows = csvObj['listRows']
     self.actionElements = ComposedElement()
     # Use for loop to print csv row by row
     for row in rows:
         be = BaseElement(row[0], row[1], row[2].lower())
         self.actionElements.addElement(be)
Example #2
0
class CSVActions(object):
    DELIMITER_ACTION = "action_"
    ACTION_ID = "reward"
    def _readActionFile(self):
        """Docstring for readActionFile """
        csvObj = CSVUtil.readCSV(self.actionfile, self.delimiter)
        rows = csvObj['listRows']
        self.actionElements = ComposedElement()
        # Use for loop to print csv row by row
        for row in rows:
            be = BaseElement(row[0], row[1], row[2].lower())
            self.actionElements.addElement(be)

    def __init__(self, basedata):
        """Docstring for CSVActions. """
        self.basedata = basedata
        self.delimiter = ","
        self.actionfile = os.path.join(basedata, "actions.csv")
        self._readActionFile()

    def getCategories(self):
        """Returns the  list of categories"""
        return self.actionElements.getCategories()

    def createAction(self, header, row):
        """Create an action from a row
        :row: the row
        :returns: the parsed action
        """
        rank = 1
        idRow = row[0]
        actionElements = {}
        for i in range(len(header)):
            if CSVActions.ACTION_ID == header[i]:
                rank = row[i]
            elif CSVActions.DELIMITER_ACTION in header[i]:
                # Take all the actions of the category
                category = header[i]
                idAction = row[i]
                # Get all elements of the category

                categoryElements = self.actionElements.getElementsByCategory(category)
                if idAction != "":
                    # Get the currect value for that action
                    actionInstance = categoryElements.getElement(idAction)
                    if actionInstance is None:
                        print "WARN: no %s idAction" % (idAction)
                    actionElements[category] = actionInstance
                else:
                    actionInstance = BaseElement.empty(category)
                    actionElements[category] = actionInstance

        return Action(actionElements, rank, idRow)
        pass
def test_remove():
    ce = ComposedElement()
    be = BaseElement("Test", "TestDesc", "category")
    ce.addElement(be)

    assert be == ce.getElement("Test")
    ce.removeElement("Test")
    assert (ce.getElement("Test") is None)
def test_category():
    ce = ComposedElement()
    be = BaseElement("Test", "TestDesc", "category")
    be2 = BaseElement("Test2 ", "TestDesc2", "category")
    be3 = BaseElement("Test3", "TestDesc3", "category2")

    ce.addElement(be)
    ce.addElement(be2)
    ce.addElement(be3)

    eg = ce.getElementsByCategory("category")
    assert ([be, be2] == eg or [be2, be] == eg)
def test_addElement():
    ce = ComposedElement()
    be = BaseElement("Test", "TestDesc", "category")
    ce.addElement(be)
    assert be == ce.getElement("Test")