def test_output_parse_array(self): data = '\n'.join([ "Anchor", "10 20 30 40 50 60 70 80", "11 21 31 41 51 61 71 81", "Key a b c d e" ]) outfile = open(self.filename, 'w') outfile.write(data) outfile.close() gen = FileParser() gen.set_file(self.filename) gen.set_delimiters(' ') gen.mark_anchor('Anchor') val = gen.transfer_array(1, 1, 1, 8) self.assertEqual(val[0], 10) self.assertEqual(val[7], 80) val = gen.transfer_array(1, 5, 2, 6) self.assertEqual(val[0], 50) self.assertEqual(val[9], 61) gen.mark_anchor('Key') val = gen.transfer_array(0, 2, 0, 6) self.assertEqual(val[4], 'e') val = gen.transfer_array(0, 2, fieldend=6) self.assertEqual(val[4], 'e') # Now, let's try column delimiters gen.reset_anchor() gen.mark_anchor('Anchor') gen.set_delimiters('columns') val = gen.transfer_array(1, 7, 1, 15) self.assertEqual(val[0], 30) self.assertEqual(val[2], 50) val = gen.transfer_array(1, 10, 2, 18) self.assertEqual(val[0], 40) self.assertEqual(val[5], 61) val = gen.transfer_array(3, 5, 3, 10) self.assertEqual(val[0], 'a b c') try: gen.transfer_array(1, 7, 1) except ValueError as err: msg = "fieldend is missing, currently required" self.assertEqual(str(err), msg) else: self.fail('ValueError expected')
def test_output_parse_same_anchors(self): data = '\n'.join([ "CQUAD4 1 3.456", "CQUAD4 2 4.123", "CQUAD4 3 7.222", "CQUAD4 4" ]) outfile = open(self.filename, 'w') outfile.write(data) outfile.close() gen = FileParser() gen.set_file(self.filename) gen.set_delimiters(' ') gen.mark_anchor('CQUAD4') val = gen.transfer_var(0, 3) self.assertEqual(val, 3.456) gen.mark_anchor('CQUAD4') val = gen.transfer_var(0, 3) self.assertEqual(val, 4.123) gen.mark_anchor('CQUAD4', 2) val = gen.transfer_var(0, 2) self.assertEqual(val, 4) gen.reset_anchor() gen.mark_anchor('CQUAD4', -1) val = gen.transfer_var(0, 2) self.assertEqual(val, 4) gen.mark_anchor('CQUAD4', -1) val = gen.transfer_var(0, 3) self.assertEqual(val, 7.222) gen.mark_anchor('CQUAD4', -2) val = gen.transfer_var(0, 3) self.assertEqual(val, 4.123)
def test_output_parse(self): data = '\n'.join([ "Junk", "Anchor", " A 1, 2 34, Test 1e65", " B 4 Stuff", "Anchor", " C 77 False NaN 333.444", " 1,2,3,4,5", " Inf 1.#QNAN -1.#IND" ]) outfile = open(self.filename, 'w') outfile.write(data) outfile.close() gen = FileParser() gen.set_file(self.filename) gen.set_delimiters(' ') gen.mark_anchor('Anchor') val = gen.transfer_var(1, 1) self.assertEqual(val, 'A') gen.reset_anchor() val = gen.transfer_var(3, 2) self.assertEqual(val, 4) self.assertEqual(type(val), int) gen.mark_anchor('Anchor',2) val = gen.transfer_var(1, 4) self.assertEqual(isnan(val), True) val = gen.transfer_var(3, 1) self.assertEqual(isinf(val), True) val = gen.transfer_var(3, 2) self.assertEqual(isnan(val), True) val = gen.transfer_var(3, 3) self.assertEqual(isnan(val), True) val = gen.transfer_line(-1) self.assertEqual(val, ' B 4 Stuff') # Now, let's try column delimiters gen.set_delimiters('columns') gen.mark_anchor('Anchor',-1) val = gen.transfer_var(1, 8, 10) self.assertEqual(val, 'als') val = gen.transfer_var(1, 17) self.assertEqual(val, 333.444) # Test some errors try: gen.mark_anchor('C 77', 3.14) except ValueError as err: msg = "The value for occurrence must be an integer" self.assertEqual(str(err), msg) else: self.fail('ValueError expected') try: gen.mark_anchor('C 77', 0) except ValueError as err: msg = "0 is not valid for an anchor occurrence." self.assertEqual(str(err), msg) else: self.fail('ValueError expected') try: gen.mark_anchor('ZZZ') except RuntimeError as err: msg = "Could not find pattern ZZZ in output file filename.dat" self.assertEqual(str(err), msg) else: self.fail('RuntimeError expected')
def test_output_parse(self): data = '\n'.join([ "Junk", "Anchor", " A 1, 2 34, Test 1e65", " B 4 Stuff", "Anchor", " C 77 False NaN 333.444", " 1,2,3,4,5", " Inf 1.#QNAN -1.#IND" ]) outfile = open(self.filename, 'w') outfile.write(data) outfile.close() gen = FileParser() gen.set_file(self.filename) gen.set_delimiters(' ') gen.mark_anchor('Anchor') val = gen.transfer_var(1, 1) self.assertEqual(val, 'A') gen.reset_anchor() val = gen.transfer_var(3, 2) self.assertEqual(val, 4) self.assertEqual(type(val), int) gen.mark_anchor('Anchor', 2) val = gen.transfer_var(1, 4) self.assertEqual(isnan(val), True) val = gen.transfer_var(3, 1) self.assertEqual(isinf(val), True) val = gen.transfer_var(3, 2) self.assertEqual(isnan(val), True) val = gen.transfer_var(3, 3) self.assertEqual(isnan(val), True) val = gen.transfer_line(-1) self.assertEqual(val, ' B 4 Stuff') # Now, let's try column delimiters gen.set_delimiters('columns') gen.mark_anchor('Anchor', -1) val = gen.transfer_var(1, 8, 10) self.assertEqual(val, 'als') val = gen.transfer_var(1, 17) self.assertEqual(val, 333.444) # Test some errors try: gen.mark_anchor('C 77', 3.14) except ValueError as err: msg = "The value for occurrence must be an integer" self.assertEqual(str(err), msg) else: self.fail('ValueError expected') try: gen.mark_anchor('C 77', 0) except ValueError as err: msg = "0 is not valid for an anchor occurrence." self.assertEqual(str(err), msg) else: self.fail('ValueError expected') try: gen.mark_anchor('ZZZ') except RuntimeError as err: msg = "Could not find pattern ZZZ in output file filename.dat" self.assertEqual(str(err), msg) else: self.fail('RuntimeError expected')