def _format_as_string(self, bracketed):
        if self.contents == []:
            return 'Unpopulated matrix of dimensions (%d, %d)' % (
                self.rows, self.cols)

        part_strs_list = []
        max_widths = [0, 0]
        for real in self.contents:
            # Fractional part is first, integer part second.
            fpart, ipart = math.modf(real)
            # Remove negative from fpart and strip the leading zero.
            fpart_str = str(abs(fpart))[1:] if fpart != 0 else ''
            # Cast to integer to strip off the fractional part of THIS number
            # (which is just 0), then format. Probably not the cleanest way,
            # but it works. This is done because RPython doesn't suport '%.0f'.
            sign_str = '-' if real < 0 else ''
            ipart_str = sign_str + str(int(abs(ipart)))
            part_strs = [fpart_str, ipart_str]
            max_widths = [
                max(len(part_strs[i]), max_widths[i]) for i in xrange(2)]
            part_strs_list.append(part_strs)

        # Tell RPython that these won't, in fact, be negative.
        rows = r_uint(self.rows)
        cols = r_uint(self.cols)
        lines = [
            ' '.join([
                # Fractional part is first, integer part second.
                rjust(part_strs[1], max_widths[1]) +
                ljust(part_strs[0], max_widths[0])
                for part_strs
                in part_strs_list[r * cols:(r + 1) * cols]])
            for r
            in xrange(rows)]

        if bracketed:
            lines = [' [ %s ]' % line for line in lines]
            lines[0] = '[' + lines[0][1:]
            lines[-1] = lines[-1] + ']'
        else:
            lines = [line.rstrip() for line in lines]
        return '\n'.join(lines)
 def test_width_larger_than_text_size(self):
     assert rjust('abcd', 10) == '      abcd'
 def test_width_equal_to_text_size(self):
     assert rjust('Just a test', 11) == 'Just a test'
 def test_width_smaller_than_text_size(self):
     assert rjust('hello there', 5) == 'hello there'