def check_blank(self, method):
     """common blank checks"""
     assert method(BDFCard(['   ']), 0, 'field') is None
     assert method(BDFCard([None]), 0, 'field') is None
     #assert method(BDFCard(['.']), 0, 'field') == 0.
     self.assertEqual('a', method(BDFCard(['']), 0, 'field', 'a'))
     self.assertEqual('b', method(BDFCard([None]), 0, 'field', 'b'))
Exemple #2
0
    def test_secset(self):
        """checks the SECSET/SECSET1 cards"""
        model = BDF(debug=False)
        seid = 171
        secset1a = SECSET1(seid, [1, 'THRU', 10], 4, comment='cset')
        secset1b = SECSET1.add_card(BDFCard(
            ['SECSET1', seid, 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]),
                                    comment='secset1')
        secset1a.write_card()
        secset1b.write_card()
        model._add_secset_object(secset1a)
        model._add_secset_object(secset1b)
        #| ASET1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        secseta = SECSET(seid, [1, 2, 3, 4, 5], [5, 4, 3, 2, 1],
                         comment='secset')
        secsetb = SECSET.add_card(BDFCard(
            ['SECSET', seid, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]),
                                  comment='secset')
        model._add_secset_object(secseta)
        model._add_secset_object(secsetb)
        model.add_secset(seid, [1, 2, 3], '42', comment='secset')
        model.add_secset1(seid, [1, 2, 3], [1, 2, 3], comment='secset1')

        nids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        for nid in nids:
            model.add_grid(nid, [float(nid), 0., 0.])
        secseta.validate()
        secsetb.validate()
        secseta.write_card()
        secsetb.write_card()
        save_load_deck(model)
    def test_integer_or_blank(self):
        """
        value = integer_or_blank(card, n, fieldname)
        """
        # integer
        self.check_integer(integer_or_blank)

        # float
        with self.assertRaises(SyntaxError):
            integer_or_blank(BDFCard([1.]), 0, 'field')
        with self.assertRaises(SyntaxError):
            integer_or_blank(BDFCard(['1.']), 0, 'field')
        #with self.assertRaises(SyntaxError):
        #integer(BDFCard(['cat']), 0, 'field')

        # string
        with self.assertRaises(SyntaxError):
            integer_or_blank(BDFCard(['a']), 0, 'field')
        with self.assertRaises(SyntaxError):
            integer_or_blank(BDFCard(['1b']), 0, 'field')

        self.check_blank(integer_or_blank)

        with self.assertRaises(SyntaxError):
            integer_or_blank(BDFCard(['1+2']), 0, 'field')

        card = [1, '2', '3.', 'C', None, '']
        exact = [1, 2, SyntaxError, SyntaxError, None, None]
        default = [None, None, None, None, None, None]
        self.run_function_default(integer_or_blank, card, exact, default)
Exemple #4
0
    def test_sebset(self):
        """checks the SEBSET/SEBSET1 cards"""
        model = BDF(debug=False)
        seid = 42
        bset1a = SEBSET1(seid, [1, 'THRU', 10], 4, comment='bset1')
        bset1b = SEBSET1.add_card(BDFCard(['SEBSET1', seid, 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]),
                                comment='sebset1')
        bset1a.write_card()
        bset1b.write_card()
        model._add_sebset_object(bset1a)
        model._add_sebset_object(bset1b)
        #| BSET1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        sebseta = SEBSET(seid, [1, 2, 3, 4, 5], [5, 4, 3, 2, 1], comment='sebset')
        sebsetb = SEBSET.add_card(BDFCard(['SEBSET', seid,
                                       1, 2, 3, 4, 5,
                                       5, 4, 3, 2, 1]), comment='sebset')
        assert len(sebseta.components) == 5, sebseta.components
        model._add_sebset_object(sebseta)
        model._add_sebset_object(sebsetb)

        nids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        for nid in nids:
            model.add_grid(nid, [float(nid), 0., 0.])
        sebseta.validate()
        sebsetb.validate()
        sebseta.write_card()
        sebsetb.write_card()
        save_load_deck(model, run_save_load_hdf5=True)
    def test_integer_double_or_blank(self):
        """
        value = double_or_blank(card, n, fieldname, default=None)
        """
        # integer
        self.check_integer(integer_double_or_blank)

        # float
        self.check_double(integer_double_or_blank)
        self.assertEqual(1.e-9,
                         integer_double_or_blank(BDFCard(['1-9']), 0, 'field'))
        self.assertEqual(1.e+9,
                         integer_double_or_blank(BDFCard(['1+9']), 0, 'field'))

        # error - string
        with self.assertRaises(SyntaxError):
            integer_double_or_blank(BDFCard(['C']), 0, 'field')
        with self.assertRaises(SyntaxError):
            integer_double_or_blank(BDFCard(['1C']), 0, 'field')
        with self.assertRaises(SyntaxError):
            integer_double_or_blank(BDFCard(['C1']), 0, 'field')

        # blank
        double_or_blank(BDFCard(['   ']), 0, 'field')
        double_or_blank(BDFCard([None]), 0, 'field')
        self.assertEqual(1.e-9, double_or_blank(BDFCard(['1-9']), 0, 'field'))
        self.assertEqual(1.e+9, double_or_blank(BDFCard(['1+9']), 0, 'field'))

        self.assertEqual(
            1000, integer_double_or_blank(BDFCard(['1000']), 0, 'field'))
    def test_integer_double_string_or_blank(self):
        """tests the integer_double_string_or_blank function"""
        # out of range
        self.assertEqual(
            None, integer_double_string_or_blank(BDFCard([1.]), 1, 'field'))
        #with self.assertRaises(SyntaxError):
        #print(integer_double_string_or_blank(BDFCard(['1b']), 0, 'field'))

        # integer
        self.check_integer(integer_double_string_or_blank)

        # float
        self.check_double(integer_double_string_or_blank)

        # string
        self.assertEqual(
            'LOAD',
            integer_double_string_or_blank(BDFCard(['load']), 0, 'field'))
        self.assertEqual(
            'MN-MM',
            integer_double_string_or_blank(BDFCard(['MN-MM']), 0, 'field'))
        #self.assertEqual(-1, integer_double_string_or_blank(BDFCard(['-1']), 0, 'field'))
        self.assertEqual(
            1000, integer_double_string_or_blank(BDFCard([1000]), 0, 'field'))
        self.assertEqual(
            'CAT',
            integer_double_string_or_blank(BDFCard([100]), 1, 'field', 'CAT'))
Exemple #7
0
    def test_qset(self):
        """checks the QSET/QSET1 cards"""
        model = BDF(debug=False)
        qset1a = QSET1([1, 'THRU', 10], 4, comment='qset')
        qset1b = QSET1.add_card(BDFCard(
            ['QSET1', 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]),
                                comment='qset1')
        model._add_qset_object(qset1a)
        model._add_qset_object(qset1b)
        qset1a.write_card()
        qset1b.write_card()
        #| ASET1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        qseta = QSET([1, 2, 3, 4, 5], [5, 4, 3, 2, 1], comment='qset')
        qsetb = QSET.add_card(BDFCard(['QSET', 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]),
                              comment='qset')
        model._add_qset_object(qseta)
        model._add_qset_object(qsetb)

        nids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        for nid in nids:
            model.add_grid(nid, [float(nid), 0., 0.])
        qseta.validate()
        qsetb.validate()
        qseta.write_card()
        qsetb.write_card()
        save_load_deck(model)
Exemple #8
0
    def test_cset(self):
        """checks the CSET/CSET1 cards"""
        model = BDF(debug=False)
        cset1a = CSET1([1, 'THRU', 10], 4, comment='cset')
        cset1b = CSET1.add_card(BDFCard(['CSET1', 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]),
                                comment='cset1')
        cset1a.write_card()
        cset1b.write_card()
        model._add_cset_object(cset1a)
        model._add_cset_object(cset1b)
        #| ASET1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        cseta = CSET([1, 2, 3, 4, 5], [5, 4, 3, 2, 1], comment='cset')
        csetb = CSET.add_card(BDFCard(['CSET',
                                       1, 2, 3, 4, 5,
                                       5, 4, 3, 2, 1]), comment='cset')
        model._add_cset_object(cseta)
        model._add_cset_object(csetb)
        model.add_cset([1, 2, 3], '42', comment='cset')
        model.add_cset1([1, 2, 3], [1, 2, 3], comment='cset1')

        nids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        for nid in nids:
            model.add_grid(nid, [float(nid), 0., 0.])
        cseta.validate()
        csetb.validate()
        cseta.write_card()
        csetb.write_card()
        save_load_deck(model, run_save_load_hdf5=True)
    def run_function(self, f, card, exact):
        """
        Helper function

        Parameters
        ----------
        f : function
           integer_or_blank
        card : List[varies]
            a series of values to add
        exacts : List[float]
            list of results
        """
        assert len(card) == len(
            exact), 'len(card)=%s len(exact)=%s' % (len(card), len(exact))
        i = 0
        fieldname = 'f'
        bdf_card = BDFCard(card)
        for i, exacti in enumerate(exact):
            if exacti == SyntaxError:
                with self.assertRaises(SyntaxError):
                    msg = 'field=%r exact=%r' % (bdf_card.field(i), exacti)
                    #print msg
                    f(bdf_card, i, fieldname)
            else:

                value = f(bdf_card, i, fieldname)
                self.assertEqual(value, exacti)
            i += 1
 def test_integer_or_double(self):
     # out of range
     with self.assertRaises(SyntaxError):
         integer_or_double(BDFCard([1.]), 1, 'field')
     self.assertEqual(1.e-9, integer_or_double(BDFCard(['1-9']), 0,
                                               'field'))
     self.assertEqual(1.e+9, integer_or_double(BDFCard(['1+9']), 0,
                                               'field'))
    def test_double_or_string(self):
        """tests the double_or_string function"""
        # out of range
        with self.assertRaises(SyntaxError):
            double_or_string(BDFCard([1.]), 1, 'field')
        self.assertEqual(1.e-9, double_or_string(BDFCard(['1-9']), 0, 'field'))
        self.assertEqual(1.e+9, double_or_string(BDFCard(['1+9']), 0, 'field'))

        self.check_double(double_or_string)
        self.check_string(double_or_string, check_dash=False)
    def test_blank(self):
        """
        value = integer(card, n, fieldname)
        """
        # integer
        with self.assertRaises(SyntaxError):
            blank(BDFCard([1]), 0, 'field')
        with self.assertRaises(SyntaxError):
            blank(BDFCard(['1']), 0, 'field')

        # float
        with self.assertRaises(SyntaxError):
            blank(BDFCard([1.]), 0, 'field')
        with self.assertRaises(SyntaxError):
            blank(BDFCard(['1.']), 0, 'field')

        # string
        with self.assertRaises(SyntaxError):
            blank(BDFCard(['a']), 0, 'field')
        with self.assertRaises(SyntaxError):
            blank(BDFCard(['1b']), 0, 'field')

        # blank
        val = blank(BDFCard(['']), 0, 'field')
        self.assertEqual(val, None)
        val = blank(BDFCard([None]), 0, 'field')
        self.assertEqual(val, None)
        val = blank(BDFCard([None]), 0, 'field', 'default')
        self.assertEqual(val, 'default')
    def test_double(self):
        """
        value = double(card, n, fieldname)
        """
        # out of range
        with self.assertRaises(SyntaxError):
            double(BDFCard([1.]), 1, 'field')

        # integer
        with self.assertRaises(SyntaxError):
            double(BDFCard([1]), 0, 'field')
        with self.assertRaises(SyntaxError):
            double(BDFCard(['1']), 0, 'field')

        self.assertEqual(1.e-9, double(BDFCard(['1-9']), 0, 'field'))
        self.assertEqual(1.e+9, double(BDFCard(['1+9']), 0, 'field'))

        # float
        self.check_double(double)

        # string
        with self.assertRaises(SyntaxError):
            double(BDFCard(['a']), 0, 'field')
        with self.assertRaises(SyntaxError):
            double(BDFCard(['1b']), 0, 'field')

        # blank
        with self.assertRaises(SyntaxError):
            double(BDFCard(['']), 0, 'field')
        with self.assertRaises(SyntaxError):
            double(BDFCard([None]), 0, 'field')

        card = [1.0, '2.0', '3.', 'C', None, '']
        exact = [1.0, 2.0, 3.0, SyntaxError, SyntaxError, SyntaxError]
        self.run_function(double, card, exact)
    def check_integer(self, method):
        """common integer checks"""
        self.assertEqual(1, method(BDFCard([1]), 0, 'field'))
        self.assertEqual(2, method(BDFCard(['2']), 0, 'field'))
        self.assertEqual(-1, method(BDFCard(['-1']), 0, 'field'))

        #if check_space:
        with self.assertRaises(SyntaxError):
            method(BDFCard(['1 3']), 0, 'field')
        with self.assertRaises(SyntaxError):
            method(BDFCard(['-1 3']), 0, 'field')
Exemple #15
0
    def test_set1_01(self):
        bdf = BDF(debug=False)
        lines = ['SET1,    1100,    100,     101']
        card = bdf.process_card(lines)
        card = BDFCard(card)

        size = 8
        card = SET1.add_card(card)
        card.write_card(size, 'dummy')
        card.raw_fields()

        card2 = SET1(1100, [100, 101], is_skin=False, comment='')
        card2.write_card(size, 'dummy')
    def test_string(self):
        """
        value = string(card, n, fieldname)
        """
        # integer
        with self.assertRaises(SyntaxError):
            string(BDFCard([1]), 0, 'field')
        with self.assertRaises(SyntaxError):
            string(BDFCard(['1']), 0, 'field')

        # float
        with self.assertRaises(SyntaxError):
            string(BDFCard([1.]), 0, 'field')
        with self.assertRaises(SyntaxError):
            string(BDFCard(['1.']), 0, 'field')

        # string
        self.assertEqual('A', string(BDFCard(['a']), 0, 'field'))
        self.assertEqual('1B', string(BDFCard(['1b']), 0, 'field'))

        # blank
        with self.assertRaises(SyntaxError):
            string(BDFCard(['']), 0, 'field')
        with self.assertRaises(SyntaxError):
            string(BDFCard([None]), 0, 'field')

        card = ['a', 'b1', '3.', 'C', None, '', 'frog']
        exact = ['A', 'B1', SyntaxError, 'C', SyntaxError, SyntaxError, 'FROG']
        self.run_function(string, card, exact)
Exemple #17
0
    def test_bset(self):
        """checks the BSET/BSET1 cards"""
        bset1a = BSET1(4, [1, 'THRU', 10])
        bset1b = BSET1.add_card(
            BDFCard(['BSET1', 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]))
        bset1a.write_card()
        bset1b.write_card()
        #| ASET1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        bseta = BSET([1, 2, 3, 4, 5], [5, 4, 3, 2, 1])
        bsetb = BSET.add_card(BDFCard(['BSET', 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]))
        bseta.validate()
        bsetb.validate()
        bseta.write_card()
        bsetb.write_card()
Exemple #18
0
    def _test_cset(self):
        """checks the CSET/CSET1 cards"""
        cset1a = CSET1(4, [1, 'THRU', 10])
        cset1b = CSET1.add_card(
            BDFCard(['CSET1', 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]))
        cset1a.write_card()
        cset1b.write_card()
        #| ASET1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        cseta = CSET([1, 2, 3, 4, 5], [5, 4, 3, 2, 1])
        csetb = CSET.add_card(BDFCard(['CSET', 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]))
        cseta.validate()
        csetb.validate()
        cseta.write_card()
        csetb.write_card()
Exemple #19
0
    def test_qset(self):
        """checks the QSET/QSET1 cards"""
        qset1a = QSET1(4, [1, 'THRU', 10])
        qset1b = QSET1.add_card(
            BDFCard(['QSET1', 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]))
        qset1a.write_card()
        qset1b.write_card()
        #| ASET1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        qseta = QSET([1, 2, 3, 4, 5], [5, 4, 3, 2, 1])
        qsetb = QSET.add_card(BDFCard(['QSET', 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]))
        qseta.validate()
        qsetb.validate()
        qseta.write_card()
        qsetb.write_card()
    def run_function(self, f, card, exact):
        assert len(card) == len(exact), 'len(card)=%s len(exact)=%s' % (len(card), len(exact))
        i = 0
        fieldname = 'f'
        card = BDFCard(card)
        for i, exacti in enumerate(exact):
            if exacti == SyntaxError:
                with self.assertRaises(SyntaxError):
                    msg = 'field=%r exact=%r' % (card.field(i), exacti)
                    #print msg
                    f(card, i, fieldname)
            else:

                value = f(card, i, fieldname)
                self.assertEqual(value, exacti)
            i += 1
    def run_function_default(self, f, card, exact, default):
        """
        Helper function

        Parameters
        ----------
        f : function
           integer_or_blank
        card : List[varies]
            a series of values to add
        exacts : List[float]
            list of results
        default : List[float]
            list of default values
        """
        fieldname = 'f'
        assert len(card) == len(
            exact), 'len(card)=%s len(exact)=%s' % (len(card), len(exact))
        assert len(card) == len(default), 'len(card)=%s len(default)=%s' % (
            len(card), len(default))
        i = 0
        bdf_card = BDFCard(card)
        for i, exacti in enumerate(exact):
            defaulti = default[i]
            if exacti == SyntaxError:
                with self.assertRaises(exacti):
                    #msg = 'field=%r exact=%r default=%r' % (bdf_card.field(i), exacti, defaulti)
                    #print(msg)
                    f(bdf_card, i, fieldname, defaulti)
            else:
                value = f(bdf_card, i, fieldname, defaulti)
                self.assertEqual(value, exacti)
            i += 1
Exemple #22
0
def components_or_blank(card: BDFCard,
                        ifield: int,
                        fieldname: str,
                        default: Optional[str] = None) -> Optional[str]:
    """
	
		:param card: BDF card as a list
		:type card: BDFCard()
		:param ifield: field number
		:type ifield: int
		:param fieldname: name of field
		:type fieldname: int
		:param default: the default value for the field (default=None)
		:type default: str, None
		:return components: a string of the dofs '0' or '123456' (not all are required)
    """
    #assert isinstance(card, BDFCard), type(card)
    assert isinstance(ifield, int), type(ifield)
    assert isinstance(fieldname, str), type(fieldname)
    svalue = card.field(ifield)
    if svalue is None:
        return default
    elif isinstance(svalue, integer_types):
        svalue = str(svalue)
    else:
        svalue = svalue.strip()

    if svalue:
        return parse_components(card, ifield, fieldname)
    return default
Exemple #23
0
def integer_double_or_blank(card: BDFCard,
                            ifield: int,
                            fieldname: str,
                            default=None):
    """
    Parameters
    ----------
    card : BDFCard()
        BDF card as a list
    ifield : int
        field number
    fieldname : str
        name of field
    default : int / float / None
        the default value for the field (default=None)

    """
    svalue = card.field(ifield)

    if isinstance(svalue, integer_float_types):
        return svalue
    elif svalue is None:
        return default

    if svalue:
        # integer/float
        try:
            return integer_or_double(card, ifield, fieldname)
        except:
            dtype = _get_dtype(svalue)
            raise SyntaxError(
                '%s = %r (field #%s) on card must be an integer, float, or '
                'blank (not %s).\n'
                'card=%s' % (fieldname, svalue, ifield, dtype, card))
    return default
Exemple #24
0
def blank(card: BDFCard, ifield: int, fieldname: str, default=None) -> None:
    """
	
		:param card: BDF card as a list
		:type card: BDFCard()
		:param ifield: field number
		:type ifield: int
		:param fieldname: name of field
		:type fieldname: str
		:param default: the default value for the field (default=None)
		:type default: None
    """
    assert isinstance(card, BDFCard), type(card)
    assert isinstance(ifield, int), type(ifield)
    assert isinstance(fieldname, str), type(fieldname)
    svalue = card.field(ifield)
    if svalue is None:
        return default

    if isinstance(svalue, str):
        svalue = svalue.strip().upper()
        if len(svalue) == 0:
            return default
    dtype = _get_dtype(svalue)
    raise SyntaxError('%s = %r (field #%s) on card must be blank (not %s).\n'
                      'card=%s' % (fieldname, svalue, ifield, dtype, card))
Exemple #25
0
def string_choice_or_blank(card: BDFCard,
                           ifield: int,
                           fieldname: str,
                           choices: Tuple[str],
                           default=None):
    """
   		:param card: BDF card as a list
		:type card: BDFCard()
		:param ifield: field number
		:type ifield: int
		:param fieldname: name of field
		:type fieldname: str
        :param default: the default value for the field (default=None)
		:type default: int, float, None
		:return value: the value from the desired field
    """
    svalue = card.field(ifield)
    if svalue is None:
        return default
    elif isinstance(svalue, str):
        svalue = svalue.strip().upper()
    else:
        dtype = _get_dtype(svalue)
        raise SyntaxError(
            '%s = %r (field #%s) on card must be a string (not %s).\n'
            'card=%s' % (fieldname, svalue, ifield, dtype, card))

    if svalue:  # string
        svalue = svalue.upper()
        if svalue not in choices:
            raise RuntimeError(
                f'{fieldname} = {svalue} (field #{ifield}) on card is a string, but must be {choices}.\n'
                f'card={card}')
        return svalue
    return default
Exemple #26
0
def integer_double_or_string(card: BDFCard, ifield: int,
                             fieldname: str) -> Union[int, float, str]:
    """
    Casts a value to an integer/double/string

    Parameters
    ----------
    card : BDFCard()
        BDF card as a list
    ifield : int
        field number
    fieldname : str
        name of field

    Returns
    -------
    value : varies
        the value of the field

    """
    svalue = card.field(ifield)
    if isinstance(svalue, integer_float_types):
        return svalue
    elif svalue is None:
        dtype = _get_dtype(svalue)
        raise SyntaxError(
            '%s = %r (field #%s) on card must be an integer or float (not %s).\n'
            'card=%s' % (fieldname, svalue, ifield, dtype, card))

    svalue = str(svalue.strip())
    if svalue:  # integer/float/string
        if '.' in svalue or '-' in svalue or '+' in svalue:
            # float
            value = double(card, ifield, fieldname)
        elif svalue.isdigit():  # 1, not +1, or -1
            # int
            try:
                value = int(svalue)
            except (ValueError, TypeError):
                raise SyntaxError(
                    '%s = %r (field #%s) on card must be an integer, float, '
                    'or string (not blank).\n'
                    'card=%s' % (fieldname, svalue, ifield, card))
        elif ' ' in svalue:
            raise SyntaxError(
                '%s = %r (field #%s) on card must be an integer, float, or string '
                '(not a string with a blank).\n'
                'card=%s' % (fieldname, svalue, ifield, card))
        elif svalue[0].isdigit():
            raise SyntaxError(
                '%s = %r (field #%s) on card must be an integer, float, or string '
                '(not a string with a leading integer).\n'
                'card=%s' % (fieldname, svalue, ifield, card))
        else:
            value = interpret_value(svalue, card)
        return value
    dtype = _get_dtype(svalue)
    raise SyntaxError(
        '%s = %r (field #%s) on card must be an integer, float, or string (not %s).\n'
        'card=%s' % (fieldname, svalue, ifield, dtype, card))
Exemple #27
0
def integer_double_or_blank(card: BDFCard,
                            ifield: int,
                            fieldname: str,
                            default=None):
    """Casts a value to an integer/double/blank

		:param card: BDF card as a list
		:type card: BDFCard()
		:param ifield: field number
		:type ifield: int
		:param fieldname: name of field
		:type fieldname: str
		:return value: the value from the desired field
        :param default: the default value for the field (default=None)
		:type default: int, float, None
    """
    svalue = card.field(ifield)

    if isinstance(svalue, integer_float_types):
        return svalue
    elif svalue is None:
        return default

    if svalue:
        # integer/float
        try:
            return integer_or_double(card, ifield, fieldname)
        except:
            dtype = _get_dtype(svalue)
            raise SyntaxError(
                '%s = %r (field #%s) on card must be an integer, float, or '
                'blank (not %s).\n'
                'card=%s' % (fieldname, svalue, ifield, dtype, card))
    return default
Exemple #28
0
def exact_string_or_blank(card: BDFCard,
                          ifield: int,
                          fieldname: str,
                          default=None):
    """
    Parameters
    ----------
    card : BDFCard()
        BDF card as a list
    ifield : int
        field number
    fieldname : str
        name of field
    default : str, None
        the default value for the field (default=None)

    Returns
    -------
    value : varies
        the value of the field

    """
    svalue = card.field(ifield)
    if svalue is None:
        return default
    svalue = '%-8s' % svalue
    if svalue == '':
        return default
    return svalue
Exemple #29
0
    def test_omit(self):
        """checks the OMIT/OMIT1 cards"""
        model = BDF(debug=False)
        omit1a = OMIT1([1, 'THRU', 10], 4, comment='omit1')
        self.assertEqual(omit1a.components, 4)
        omit1b = OMIT1.add_card(BDFCard(
            ['OMIT1', 5, 1, 2, 3, 4, 5, 6, 7, 8, 10, 9]),
                                comment='omit1')
        model._add_omit_object(omit1a)
        model._add_omit_object(omit1b)

        omit1a.validate()
        omit1b.validate()
        omit1a.write_card()
        omit1b.write_card()
        #| OMIT1 |  C  | ID1 | THRU | ID2 |     |     |     |     |

        #omita = OMIT([1, 2, 3, 4, 5], [5, 4, 3, 2, 1])
        #omitb = OMIT.add_card(BDFCard(['OMIT',
        #1, 2, 3, 4, 5,
        #5, 4, 3, 2, 1]))
        #omita.validate()
        #omitb.validate()
        #omita.write_card()
        #omitb.write_card()
        save_load_deck(model)
Exemple #30
0
def integer_double_string_or_blank(card: BDFCard,
                                   ifield: int,
                                   fieldname: str,
                                   default=None):
    # type (BDFCard, int, str, Union[int, float, str]) -> Optional[Union[int, float, str]]
    """
    Parameters
    ----------
    card : BDFCard()
        BDF card as a list
    ifield : int
        field number
    fieldname : str
        name of field
    default : int, float, str, None (default=None)
        the default value for the field

    Returns
    -------
    value : int, float, str, None
        the field value

    """
    svalue = card.field(ifield)

    if isinstance(svalue, integer_float_types):
        return svalue
    elif svalue is None:
        return default

    svalue = svalue.strip().upper()
    if svalue:
        # integer/float/string
        if '.' in svalue or '-' in svalue[1:] or '+' in svalue[1:]:
            # float
            try:
                value = double(card, ifield, fieldname)
            except SyntaxError:
                value = interpret_value(card[ifield], card)
        elif RE_INT.match(
                svalue):  # svalue[0].isdigit() or svalue[1:].isdigit():
            # int
            try:
                value = int(svalue)
            except ValueError:
                dtype = _get_dtype(svalue)
                msg = (
                    '%s = %r (field #%s) on card must be an integer, float, '
                    'or string (not %s).\n'
                    'card=%s' % (fieldname, svalue, ifield, dtype, card))
                raise SyntaxError(msg)
        elif ' ' in svalue:
            raise SyntaxError(
                '%s = %r (field #%s) on card must be an integer, float or string '
                '(without a blank space).\n'
                'card=%s' % (fieldname, svalue, ifield, card))
        else:
            value = svalue
        return value
    return default
Exemple #31
0
 def test_set1_03(self):
     """checks the SET1 card"""
     sid = 10
     ids = [1, 2, 3, 4, 5]
     set1a = SET1(sid, ids, is_skin=False, comment='set1')
     set1b = SET1.add_card(BDFCard(['SET1', sid] + ids))
     set1a.write_card()
Exemple #32
0
def integer(card: BDFCard, ifield: int, fieldname: str) -> int:
    """
    Casts a value to an integer

    Parameters
    ----------
    card : BDFCard()
        BDF card as a list
    ifield : int
        field number
    fieldname : str
        name of field

    """
    svalue = card.field(ifield)
    if isinstance(svalue, float_types):
        dtype = _get_dtype(svalue)
        raise SyntaxError(
            '%s = %r (field #%s) on card must be an integer (not %s).\n'
            'card=%s' % (fieldname, svalue, ifield, dtype, card))

    try:
        return int(svalue)
    except (ValueError, TypeError):
        dtype = _get_dtype(svalue)
        raise SyntaxError(
            '%s = %r (field #%s) on card must be an integer (not %s).\n'
            'card=%s' % (fieldname, svalue, ifield, dtype, card))
 def run_function_default(self, f, card, exact, default):
     fieldname = 'f'
     assert len(card) == len(exact), 'len(card)=%s len(exact)=%s' % (len(card), len(exact))
     assert len(card) == len(default), 'len(card)=%s len(default)=%s' % (len(card), len(default))
     i = 0
     card = BDFCard(card)
     for i, exacti in enumerate(exact):
         defaulti = default[i]
         if exacti == SyntaxError:
             with self.assertRaises(exacti):
                 msg = 'field=%r exact=%r default=%r' % (card.field(i), exacti, defaulti)
                 #print msg
                 f(card, i, fieldname, defaulti)
         else:
             value = f(card, i, fieldname, defaulti)
             self.assertEqual(value, exacti)
         i += 1