Pykov
Pykov copied to clipboard
Error when an item in pykov.Chain is None
Hey,
first of all this is my first time using github and also i'm very new to python so be gentle with me..
I have a pykov.Chain where one of the items is None and when i'm trying to use the function chain.move(None) i get the following error:
AttributeError: 'OrderedDict' object has no attribute 'choose'
to fix it i did the following changes in the succ function (i only use positive numbers in my project):
from:
def succ(self, key=None):
try:
if key is not None:
return self._succ[key]
else:
return self._succ
except AttributeError:
self._succ = OrderedDict([(state, Vector()) for state in self.states()])
for link, probability in six.iteritems(self):
self._succ[link[0]][link[1]] = probability
if key is not None:
return self._succ[key]
else:
return self._succ
to:
def succ(self, key=-1):
try:
if key is not -1:
return self._succ[key]
else:
return self._succ
except AttributeError:
self._succ = OrderedDict([(state, Vector()) for state in self.states()])
for link, probability in six.iteritems(self):
self._succ[link[0]][link[1]] = probability
if key is not -1:
return self._succ[key]
else:
return self._succ
My solution works for me, but is there a better way?
Hi Dvir, your solution works for you, but it is not suitable for a generic use case. Think about this case for example:
T = pykov.Chain({(1,-1): .3, (1,1): .7, (-1,1): 1.})
My suggestion is to replace None
with a defined value, maybe 'None'
. What do you think?