-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVActions.py
61 lines (55 loc) · 2.15 KB
/
CSVActions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import CSVUtil
import os
from BaseElement import ComposedElement
from BaseElement import BaseElement
from Action import Action
import sys
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