Skip to content

Instantly share code, notes, and snippets.

@px-amaac
Last active December 31, 2015 07:49
Show Gist options
  • Save px-amaac/7956328 to your computer and use it in GitHub Desktop.
Save px-amaac/7956328 to your computer and use it in GitHub Desktop.
class Transition(object):
def __init__(self, state, given, prob):
self.state = state
self.given = given
self.prob = prob
def __str__(self):
return "P({}|{})={}".format(self.state, self.given, self.prob)
def disp(self):
return "P({}|{})={}".format(self.state, self.given, self.prob)
>>> from Transition import Transition
>>> t = Transition('b', 'b', 0.6)
>>> t
<Transition.Transition object at 0x00000000028DE6A0>
>>> print(t)
P(b|b)=0.6
>>> if t.state is 'b':
... print('working')
...
working
>>> if t.state is 'b' and t.given is 'b':
... print('working')
...
working
>>> file = open("out.txt", "r")
>>> lines = []
>>> for line in file:
... lines.append(line)
...
>>> print(lines)
['100\n', 'P(B|B)=0.6\n', 'P(B|L)=0.3\n', 'P(L|B)=0.4\n', 'P(L|L)=0.7\n', 'P(H|B
)=0.5\n', 'P(T|B)=0.5\n', 'P(H|L)=0.1\n', 'P(T|L)=0.9\n', '0\n', '0\n', '0\n', '
1\n', '0\n', '1\n', '1\n', '0\n', '1\n', '1\n', '1\n', '1\n', '0\n', '1\n', '0\n
', '1\n', '1\n', '0\n', '0\n', '1\n', '0\n', '0\n', '0\n', '0\n', '1\n', '0\n',
'1\n', '0\n', '0\n', '0\n', '1\n', '0\n', '0\n', '1\n', '1\n', '0\n', '0\n', '0\
n', '0\n', '0\n', '1\n', '0\n', '0\n', '1\n', '1\n', '0\n', '1\n', '1\n', '1\n',
'1\n', '0\n', '0\n', '0\n', '0\n', '1\n', '1\n', '1\n', '0\n', '1\n', '1\n', '0
\n', '1\n', '1\n', '1\n', '1\n', '0\n', '1\n', '0\n', '0\n', '1\n', '1\n', '1\n'
, '1\n', '1\n', '0\n', '0\n', '1\n', '1\n', '0\n', '1\n', '1\n', '0\n', '1\n', '
1\n', '1\n', '0\n', '1\n', '0\n', '0\n', '0\n', '1\n', '0\n', '0\n', '1\n', '1\n
', '0\n', '0\n', '0\n', '0\n', '1\n', 'B\n', 'B\n', 'B\n', 'B\n', 'L\n', 'B\n',
'L\n', 'B\n', 'L\n', 'L\n', 'B\n', 'B\n', 'B\n', 'L\n', 'L\n', 'B\n', 'B\n', 'B\
n', 'B\n', 'B\n', 'B\n', 'B\n', 'B\n', 'B\n', 'L\n', 'L\n', 'B\n', 'B\n', 'L\n',
'B\n', 'B\n', 'B\n', 'L\n', 'L\n', 'B\n', 'B\n', 'B\n', 'L\n', 'B\n', 'B\n', 'L
\n', 'B\n', 'B\n', 'B\n', 'B\n', 'B\n', 'B\n', 'B\n', 'B\n', 'L\n', 'L\n', 'L\n'
, 'B\n', 'L\n', 'L\n', 'L\n', 'L\n', 'B\n', 'B\n', 'L\n', 'B\n', 'B\n', 'L\n', '
B\n', 'B\n', 'B\n', 'B\n', 'B\n', 'L\n', 'B\n', 'L\n', 'L\n', 'B\n', 'B\n', 'B\n
', 'B\n', 'B\n', 'L\n', 'L\n', 'B\n', 'B\n', 'B\n', 'L\n', 'B\n', 'L\n', 'B\n',
'L\n', 'B\n', 'L\n', 'B\n', 'B\n', 'L\n', 'B\n', 'B\n', 'B\n', 'L\n', 'B\n', 'B\
n', 'B\n', 'L']
>>> first, second = lines[1].strip('P(\n').split(')')
>>> state, given = first.split('|')
>>> prob = second.strip('=')
>>> prob
'0.6'
>>> float(prob)
0.6
>>> t = Transition(state.lower(), given.lower(), float(prob))
>>> print(t)
P(b|b)=0.6
>>> if t.state is 'b' and t.given is 'b':
... print('working')
...
>>> print(t)
P(b|b)=0.6
>>> t.state
'b'
>>> t.prob
0.6
>>> if t.state is 'b':
... print('working')
...
>>> type(t.state)
<class 'str'>
>>> if t.state is "b":
... print('working')
...
>>> type("b")
<class 'str'>
>>> t.state
'b'
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment