def _tdcat_py(full_path, tivo_mak): xml_data = {} tfile = open(full_path, 'rb') header = tfile.read(16) offset, chunks = struct.unpack('>LH', header[10:]) rawdata = tfile.read(offset - 16) tfile.close() count = 0 for i in xrange(chunks): chunk_size, data_size, id, enc = struct.unpack( '>LLHH', rawdata[count:count + 12]) count += 12 data = rawdata[count:count + data_size] xml_data[id] = {'enc': enc, 'data': data, 'start': count + 16} count += chunk_size - 12 chunk = xml_data[2] details = chunk['data'] if chunk['enc']: xml_key = xml_data[3]['data'] hexmak = hashlib.md5('tivo:TiVo DVR:' + tivo_mak).hexdigest() key = hashlib.sha1(hexmak + xml_key).digest()[:16] + '\0\0\0\0' turkey = hashlib.sha1(key[:17]).digest() turiv = hashlib.sha1(key).digest() details = turing.Turing(turkey, turiv).crypt(details, chunk['start']) return details
def test_part_two(self): "Test part two example of Turing object" # 1. Create Turing object from text myobj = turing.Turing(part2=True, text=aoc_25.from_text(PART_TWO_TEXT)) # 2. Check the part two self.assertEqual(myobj.part_two(verbose=False), PART_TWO_RESULT)
def test_part_one(self): "Test part one example of Turing object" # 1. Create Turing object from text myobj = turing.Turing(text=aoc_25.from_text(PART_ONE_TEXT)) # 2. Check the part one result self.assertEqual(myobj.part_one(verbose=False), PART_ONE_RESULT)
def test_text_init(self): "Test the Turing object creation from text" # 1. Create Turing object from text myobj = turing.Turing(text=aoc_25.from_text(EXAMPLE_TEXT)) # 2. Make sure it has the expected values self.assertEqual(myobj.part2, False) self.assertEqual(len(myobj.text), 20) self.assertEqual(len(myobj.tape), 0) self.assertEqual(myobj.position, 0) self.assertEqual(myobj.state, 'A') self.assertEqual(myobj.steps, 6) self.assertEqual(len(myobj.rules), 2) # 3. Check the rules self.assertTrue('A' in myobj.rules) self.assertTrue('B' in myobj.rules) self.assertEqual(len(myobj.rules['A']), 2) self.assertEqual(len(myobj.rules['B']), 2) self.assertEqual(myobj.rules['A'][0], turing.Rule('A', 0, 1, turing.RIGHT, 'B')) self.assertEqual(myobj.rules['A'][1], turing.Rule('A', 1, 0, turing.LEFT, 'B')) self.assertEqual(myobj.rules['B'][0], turing.Rule('B', 0, 1, turing.LEFT, 'A')) self.assertEqual(myobj.rules['B'][1], turing.Rule('B', 1, 1, turing.RIGHT, 'A')) # 3. Check the step and ones methods self.assertEqual(myobj.position, 0) self.assertEqual(myobj.state, 'A') self.assertEqual(myobj.ones(), 0) myobj.step() self.assertEqual(myobj.position, 1) self.assertEqual(myobj.state, 'B') self.assertEqual(myobj.ones(), 1) myobj.step() self.assertEqual(myobj.position, 0) self.assertEqual(myobj.state, 'A') self.assertEqual(myobj.ones(), 2) myobj.step() self.assertEqual(myobj.position, -1) self.assertEqual(myobj.state, 'B') self.assertEqual(myobj.ones(), 1) myobj.step() self.assertEqual(myobj.position, -2) self.assertEqual(myobj.state, 'A') self.assertEqual(myobj.ones(), 2) myobj.step() self.assertEqual(myobj.position, -1) self.assertEqual(myobj.state, 'B') self.assertEqual(myobj.ones(), 3) myobj.step() self.assertEqual(myobj.position, 0) self.assertEqual(myobj.state, 'A') self.assertEqual(myobj.ones(), 3)
def part_two(args, input_lines): "Process part two of the puzzle" # 1. Create the puzzle solver solver = turing.Turing(part2=True, text=input_lines) # 2. Determine the solution for part two solution = solver.part_two(verbose=args.verbose, limit=args.limit) if solution is None: print("There is no solution") else: print("The solution for part two is %s" % (solution)) # 3. Return result return solution is not None
def test_empty_init(self): "Test the default Turing creation" # 1. Create default Turing object myobj = turing.Turing() # 2. Make sure it has the default values self.assertEqual(myobj.part2, False) self.assertEqual(myobj.text, None) self.assertEqual(len(myobj.tape), 0) self.assertEqual(myobj.position, 0) self.assertEqual(myobj.state, None) self.assertEqual(myobj.steps, 0) self.assertEqual(len(myobj.rules), 0) # 3. Check methods self.assertEqual(myobj.ones(), 0)
# coding=utf-8 import turing import baiduApi if __name__ == "__main__": api_id = "9241847" api_key = "wxNGGTpSQTKYjROe5gpgBKDO" api_secert = "f4a44a0f4c5d12173c34e5b258ef1f98" bdr = baiduApi.BaiduRest(api_id, api_key, api_secert) while True: input("按下回车开始说话,自动停止") print('开始录音') bdr.recorder("output.wav") print("结束") ask = bdr.getText('output.wav') print('你:', ask) robot = turing.Turing() ans = robot.anser(ask) print('机器人:', ans) bdr.getVoice(ans, "output.mp3") bdr.speakMac("output.mp3")
6060< Similar to state 4, but applies to bit 2 6161< 6262< 6363< 6_82> 6x6x< 7070< Similar to state 4, but applies to bit 3 7171< 7272< 7373< 7_83> 7x7x< 8080> This state returns to the right 8181> 8282> 8383> 8x9x> in case of x it switches to state 9 9x9x> This state returns to the right over all x bits 900x< in case of a value other than x, it takes it and start moving to the 911x< left. It also switches to the state 0, 1, 2, or 3, depending of the bit. 922x< 933x< 9_x_< Halt """ print 'Output data: ' + str(turing.Turing(program, data).Run())
82a2< 83a3< 9003> 9103> 92a2< 93a3< a2a2< a3a3< a_b_> b2b0> b3b1> b_x_> """ # Create a machine tm = turing.Turing(program, data, printing=False) # Run the program output_data = tm.Run() # Test output data test_result = list(output_data) == list(data)[::-1] # Print summary print 'Output data: ' + output_data + '\n' print 'Test ' + ['failed', 'passed'][test_result] + '!'