def load(self, input):
     """ deserialize from a file object """
     try:
         self.clear()
         line = input.readline()
         # line --> open|moves|close
         delim = line.index("|"), line.index("|", line.index("|") + 1)
         open = line[0:delim[0]]
         close = line[(delim[1] + 1):]
         moves = line[(delim[0] + 1):delim[1]]
         # open --> flag@time
         delim = open.index("@")
         self.ep_open = open[0:delim], int(open[(delim + 1):])
         # close --> flag@time
         delim = close.index("@")
         self.ep_close = close[0:delim], int(close[(delim + 1):])
         # moves --> action[reward](time)...
         minput = io.StringIO(moves)
         while True:
             # check if EOF
             ipt = minput.tell()
             if minput.read(1) == "":
                 break
             minput.seek(ipt)
             # ?? --> action
             a = action.parse(minput)
             self.ep_score += a.apply(self.ep_state)
             # [?] --> reward
             r = self.load_optional_value(minput, "[]")
             # (?) --> time
             t = self.load_optional_value(minput, "()")
             # (action, reward, time)
             self.ep_moves += [(a, r, t)]
         return True
     except (RuntimeError, ValueError, IndexError):
         pass
     return False
Exemple #2
0
 print(sio.read(1))
 print(sio.read(1))
 print(sio.read(1) == "")
 
 line = "".join([str(move[0]) + ("[" + str(move[1]) + "]" if move[1] else "") + ("(" + str(move[2]) + ")" if move[2] else "") for move in moves])
 print(line)
 minput = io.StringIO(line)
 state = board()
 while True:
     # check if EOF
     ipt = minput.tell()
     if minput.read(1) == "":
         break
     minput.seek(ipt)
     # ?? --> action
     a = action.parse(minput)
     u = a.apply(state)
     print("a ", str(a), str(u))
     # [?] --> reward
     ipt = minput.tell()
     if minput.read(1) == "[":
         buf = minput.read()
         r = buf[0:buf.index("]")]
         minput.seek(ipt + len(r) + 2)
         r = int(r)
     else:
         minput.seek(ipt)
         r = 0
     # (?) --> time
     ipt = minput.tell()
     if minput.read(1) == "(":