Ejemplo n.º 1
0
    def __init__(self, field_name, is_allowed_to_be_empty, length_text, rule, data_format, empty_value=None):
        super(IntegerFieldFormat, self).__init__(
            field_name, is_allowed_to_be_empty, length_text, rule, data_format, empty_value)

        is_fixed_format = (data_format.format == data.FORMAT_FIXED)
        has_length = (length_text is not None) and (length_text.strip() != '')
        if has_length:
            length = self.length
            if is_fixed_format:
                # For fixed data format, use an implicit range starting from
                # 1 to take into account that leading and trailing blanks
                # might be missing from the rule parts.
                assert self.length.lower_limit == self.length.upper_limit
                length = ranges.Range('1...%d' % self.length.upper_limit)
            length_range = ranges.create_range_from_length(length)

        has_rule = (rule is not None) and (rule.strip() != '')
        if has_rule:
            rule_range = ranges.Range(rule)

        if has_length:
            if has_rule:
                # Both a length and a rule have been specified: check if all
                # non ``None`` parts of each item of the rule fit within the
                # range of the length. Then use the rule as valid range.
                for rule_item in rule_range.items:
                    partial_rule_limits = [
                        partial_rule_limit for partial_rule_limit in rule_item if partial_rule_limit is not None
                    ]
                    for partial_rule_limit in partial_rule_limits:
                        length_of_partial_rule_limit = _tools.length_of_int(partial_rule_limit)
                        try:
                            length.validate(
                                "length of partial rule limit '%d'" % partial_rule_limit, length_of_partial_rule_limit)
                        except errors.RangeValueError as error:
                            message = "length must be consistent with rule: %s" % error
                            raise errors.InterfaceError(message)
                self.valid_range = rule_range
            else:
                # A length but no rule has been specified: derive a valid
                # range from the length.
                self.valid_range = length_range
        else:
            if has_rule:
                # No length but a rule has been specified: use the rule as
                # valid range.
                self.valid_range = rule_range
            else:
                # No length and no rule has been specified: use a default
                # range of signed 32 bit integer. If the user wants a bigger
                # range, he has to specify it. Python's ``int`` scales to any
                # range as long as there is enough memory available to
                # represent it.
                self.valid_range = ranges.Range(ranges.DEFAULT_INTEGER_RANGE_TEXT)
Ejemplo n.º 2
0
    def __init__(self, field_name, is_allowed_to_be_empty, length_text, rule, data_format, empty_value=None):
        super(IntegerFieldFormat, self).__init__(
            field_name, is_allowed_to_be_empty, length_text, rule, data_format, empty_value)

        is_fixed_format = (data_format.format == data.FORMAT_FIXED)
        has_length = (length_text is not None) and (length_text.strip() != '')
        if has_length:
            length = self.length
            if is_fixed_format:
                # For fixed data format, use an implicit range starting from
                # 1 to take into account that leading and trailing blanks
                # might be missing from the rule parts.
                assert self.length.lower_limit == self.length.upper_limit
                length = ranges.Range('1...%d' % self.length.upper_limit)
            length_range = ranges.create_range_from_length(length)

        has_rule = (rule is not None) and (rule.strip() != '')
        if has_rule:
            rule_range = ranges.Range(rule)

        if has_length:
            if has_rule:
                # Both a length and a rule have been specified: check if all
                # non ``None`` parts of each item of the rule fit within the
                # range of the length. Then use the rule as valid range.
                for rule_item in rule_range.items:
                    partial_rule_limits = [
                        partial_rule_limit for partial_rule_limit in rule_item if partial_rule_limit is not None
                    ]
                    for partial_rule_limit in partial_rule_limits:
                        length_of_partial_rule_limit = _tools.length_of_int(partial_rule_limit)
                        try:
                            length.validate(
                                "length of partial rule limit '%d'" % partial_rule_limit, length_of_partial_rule_limit)
                        except errors.RangeValueError as error:
                            message = "length must be consistent with rule: %s" % error
                            raise errors.InterfaceError(message)
                self.valid_range = rule_range
            else:
                # A length but no rule has been specified: derive a valid
                # range from the length.
                self.valid_range = length_range
        else:
            if has_rule:
                # No length but a rule has been specified: use the rule as
                # valid range.
                self.valid_range = rule_range
            else:
                # No length and no rule has been specified: use a default
                # range of signed 32 bit integer. If the user wants a bigger
                # range, he has to specify it. Python's ``int`` scales to any
                # range as long as there is enough memory available to
                # represent it.
                self.valid_range = ranges.Range(ranges.DEFAULT_INTEGER_RANGE_TEXT)
Ejemplo n.º 3
0
    def __init__(self, field_name, is_allowed_to_be_empty, length_text, rule, data_format, empty_value=None):
        super(IntegerFieldFormat, self).__init__(field_name, is_allowed_to_be_empty, length_text, rule, data_format,
                                                 empty_value)
        # The default range is 32 bit. If the user wants a bigger range, he
        # has to specify it. Python's ``int`` scales to any range as long
        # there is enough memory available to represent it.
        has_length = (length_text is not None) and (length_text.strip() != '')
        has_rule = (rule is not None) and (rule.strip() != '')

        if has_length:
            length = self._length
            if data_format.format == data.FORMAT_FIXED and self._length.lower_limit == self._length.upper_limit:
                length = ranges.Range('1...%d' % self._length.upper_limit)

            length_range = ranges.create_range_from_length(length)

            if length_range.lower_limit == 1:
                self._is_allowed_to_be_empty = False

            if has_rule:
                rule_range = ranges.Range(rule)

                if length_range.upper_limit is not None and rule_range.upper_limit is not None \
                        and length_range.upper_limit < rule_range.upper_limit:
                    raise errors.FieldValueError('length upper limit must be greater than the rule upper limit')

                if length_range.lower_limit is not None and rule_range.lower_limit is not None \
                        and length_range.lower_limit > rule_range.lower_limit:
                    raise errors.FieldValueError('rule lower limit must be less than the length lower limit')

                self.valid_range = rule_range
            else:
                self.valid_range = length_range
        else:
            if has_rule:
                self.valid_range = ranges.Range(rule)
            else:
                self.valid_range = ranges.Range(ranges.DEFAULT_INTEGER_RANGE_TEXT)
Ejemplo n.º 4
0
 def test_can_create_range_from_length(self):
     self.assertEqual(ranges.create_range_from_length(ranges.Range("1...")).items, None)
     self.assertEqual(ranges.create_range_from_length(ranges.Range("1...1")).items, [(0, 9)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("1...3")).items, [(-99, 999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("0...")).items, None)
     self.assertEqual(ranges.create_range_from_length(ranges.Range("0...1")).items, [(0, 9)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("0...3")).items, [(-99, 999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("")).items, None)
     self.assertEqual(ranges.create_range_from_length(ranges.Range("...1")).items, [(0, 9)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("...3")).items, [(-99, 999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("2...2")).items, [(-9, -1), (10, 99)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("2...5")).items, [(-9999, -1), (10, 99999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("3...8")).items, [(-9999999, -10), (100, 99999999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("2...")).items, [(None, -1), (10, None)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("3...")).items, [(None, -10), (100, None)])
     self.assertEqual(
         ranges.create_range_from_length(
             ranges.Range("3...4, 10...")).items, [(-999, -10), (100, 9999), (None, -100000000), (1000000000, None)])
Ejemplo n.º 5
0
 def test_can_create_range_from_length(self):
     self.assertEqual(ranges.create_range_from_length(ranges.Range("1...")).items, None)
     self.assertEqual(ranges.create_range_from_length(ranges.Range("1...1")).items, [(0, 9)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("1...3")).items, [(-99, 999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("0...")).items, None)
     self.assertEqual(ranges.create_range_from_length(ranges.Range("0...1")).items, [(0, 9)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("0...3")).items, [(-99, 999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("")).items, None)
     self.assertEqual(ranges.create_range_from_length(ranges.Range("...1")).items, [(0, 9)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("...3")).items, [(-99, 999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("2...2")).items, [(-9, -1), (10, 99)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("2...5")).items, [(-9999, -1), (10, 99999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("3...8")).items, [(-9999999, -10), (100, 99999999)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("2...")).items, [(None, -1), (10, None)])
     self.assertEqual(ranges.create_range_from_length(ranges.Range("3...")).items, [(None, -10), (100, None)])
     self.assertEqual(
         ranges.create_range_from_length(
             ranges.Range("3...4, 10...")).items, [(-999, -10), (100, 9999), (None, -100000000), (1000000000, None)])