def test_ints_16(self): self.assertEqual(print_field_16(1), ' 1', 'a') self.assertEqual(print_field_16(12345678), ' 12345678', 'b') self.assertEqual(print_field_16(1234567890123456), '1234567890123456', 'c') self.assertRaises(RuntimeError, print_field_16, 12345678901234567) #msg = print_field_16('12345678 ') #msg = '|%s| len(msg)=%s' %(msg, len(msg)) #self.assertEqual(print_field_16('12345678'), '12345678 ',msg) self.assertEqual(print_field_16('1 '), '1 ', '|%s|' % (print_field('1 ')))
def test_ints_16(self): self.assertEqual(print_field_16(1), ' 1', 'a') self.assertEqual(print_field_16(12345678), ' 12345678', 'b') self.assertEqual(print_field_16(1234567890123456), '1234567890123456', 'c') self.assertRaises(RuntimeError, print_field_16, 12345678901234567) #msg = print_field_16('12345678 ') #msg = '|%s| len(msg)=%s' %(msg, len(msg)) #self.assertEqual(print_field_16('12345678'), '12345678 ',msg) self.assertEqual(print_field_16('1 '), '1 ', '|%s|' % (print_field_8('1 ')))
def __init__(self, card, has_none=True): # type: (List[str], bool) -> None """ Parameters ---------- card : List[str] the split values for the card has_none : bool; default=True helps with a special case to speed up runtime # definitely bad card = ['GRID', '1', '', '1.0', '2.0', '3.0'] BDFCard(card, has_none=False) # definitely correct card = ['GRID', '1', None, '1.0', '2.0', '3.0'] BDFCard(card, has_none=True) # ??? card = ['GRID', '1', '', 1.0, 2.0, 3.0] BDFCard(card, has_none=True) """ if has_none: card = wipe_empty_fields( [print_field_16(field).strip() for field in card]) self.card = card self.nfields = len(self.card)
def __init__(self, card, has_none=True): """ Parameters ---------- card : List[str] the split values for the card has_none : bool; default=True helps with a special case to speed up runtime # definitely bad card = ['GRID', '1', '', '1.0', '2.0', '3.0'] BDFCard(card, has_none=False) # definitely correct card = ['GRID', '1', None, '1.0', '2.0', '3.0'] BDFCard(card, has_none=True) # ??? card = ['GRID', '1', '', 1.0, 2.0, 3.0] BDFCard(card, has_none=True) """ if has_none: card = wipe_empty_fields([print_field_16(field).strip() for field in card]) self.card = card self.nfields = len(self.card)
def assert_fields(card1, card2): try: fields1 = wipe_empty_fields(card1.repr_fields()) fields2 = wipe_empty_fields(card2.repr_fields()) except: print("card1 = \n%s" % (card1)) print("card2 = \n%s" % (card2)) raise if len(fields1) != len(fields2): msg = ('len(fields1)=%s len(fields2)=%s\n%r\n%r\n%s\n%s' % (len(fields1), len(fields2), fields1, fields2, print_card_8(fields1), print_card_8(fields2))) raise RuntimeError(msg) msg_end = '' max_int = 99999999 for (i, field1, field2) in zip(count(), fields1, fields2): if isinstance(field1, int) and field1 > max_int: value1a = print_field_16(field1) value2a = print_field_16(field2) else: value1a = print_field_8(field1) value2a = print_field_8(field2) msg_end += '%-2s: %-8s %-8s\n' % (i, field1, field2) if value1a != value2a: if isinstance(field1, int) and field1 > max_int: value1 = print_field_16(interpret_value(value1a)) value2 = print_field_16(interpret_value(value2a)) else: value1 = print_field_8(interpret_value(value1a)) value2 = print_field_8(interpret_value(value2a)) if value1 != value2: msg = 'value1 != value2\n' msg += ( 'card_name=%s ID=%s i=%s field1=%r field2=%r value1=%r ' 'value2=%r\n%r\n%r\n' % (fields1[0], fields1[1], i, field1, field2, value1, value2, fields1, fields2)) raise RuntimeError(msg + msg_end)
def assert_fields(card1, card2): try: fields1 = wipe_empty_fields_typed(card1.repr_fields()) fields2 = wipe_empty_fields_typed(card2.repr_fields()) except: print("card1 = \n%s" % (card1)) print("card2 = \n%s" % (card2)) raise if len(fields1) != len(fields2): msg = ('len(fields1)=%s len(fields2)=%s\n%r\n%r\n%s\n%s' % (len(fields1), len(fields2), fields1, fields2, print_card_8(fields1), print_card_8(fields2))) raise RuntimeError(msg) msg_end = '' max_int = 99999999 for (i, field1, field2) in zip(count(), fields1, fields2): if isinstance(field1, int) and field1 > max_int: value1a = print_field_16(field1) value2a = print_field_16(field2) else: value1a = print_field_8(field1) value2a = print_field_8(field2) msg_end += '%-2s: %-8s %-8s\n' % (i, field1, field2) if value1a != value2a: if isinstance(field1, int) and field1 > max_int: value1 = print_field_16(interpret_value(value1a)) value2 = print_field_16(interpret_value(value2a)) else: value1 = print_field_8(interpret_value(value1a)) value2 = print_field_8(interpret_value(value2a)) if value1 != value2: msg = 'value1 != value2\n' msg += ('card_name=%s ID=%s i=%s field1=%r field2=%r value1=%r ' 'value2=%r\n%r\n%r\n' % (fields1[0], fields1[1], i, field1, field2, value1, value2, fields1, fields2)) raise RuntimeError(msg + msg_end)
def test_strings_16(self): self.assertEqual(print_field_16(None), ' ', print_field_16(None)) self.assertEqual(print_field_16('asdf'), ' asdf', print_field_16('asdf')) self.assertEqual(print_field_16(' asdf '), ' asdf ', print_field_16(' asdf ')) self.assertRaises(RuntimeError, print_field_16, ' asdf ')
def write_field(value, fieldspan=1): """Convert `value` to field string """ width = fieldspan * 8 if isinstance(value, str): return value[:width].strip() elif width == 8: return print_field_8(value).strip() elif width == 16: return print_field_16(value).strip() else: raise ValueError("non-character field entries " "cannot span more than 2 fields, " "but fieldspan is {}".format(fieldspan))
def __init__(self, card, has_none=True): if has_none: card = wipe_empty_fields([print_field_16(field).strip() for field in card]) self.card = card self.nfields = len(self.card)