Пример #1
0
    def __init__(self,
                 *,
                 corner=None,
                 border=None,
                 hsep=None,
                 vsep=None,
                 tb_cross=None,
                 lb_cross=None,
                 rb_cross=None,
                 bb_cross=None,
                 hl_cross=None,
                 nl_cross=None,
                 cell_pad=[1, 1],
                 pad_char=" ",
                 template=None,
                 align=None,
                 data=None,
                 rstrip=True,
                 indent=""):
        """Initialise a Table formatting parameter set.
        
        Arguments:
        * corner[4]:     top left, top right, bottom left, bottom right corners
        * border[4]:     top, left, right, bottom border sans crossing or corner
        * hsep[2]:       horizontal separator, after first column and others
        * vsep[2]:       vertical separator, after first row and others
        * tb_cross[2]:   top border crossing, first and others
        * lb_cross[2]:   left border crossing, first and others
        * rb_cross[2]:   right border crossing, first and others
        * bb_cross[2]:   bottom border crossing, first and others
        * hl_cross[2]:   header separator line crossing, first and others
        * nl_cross[2]:   normal separator line crossing, first and others
        * cell_pad[2]:   minimum cell padding, left and right (integers)
        * pad_char:      padding character
        * template:      a template 7 x 7 drawing describing the table
        * align:         alignment descriptor string, 1 char per column, l/r/c;
                         may be sequence of 2 for first and following rows;
                         asterisk at the end means default for folloing columns
                         is the character in front of the asterisk
        * data:          the data to format, as a sequence of rows, which are
                         sequences of columns (the data items)
        * rstrip:        strip trailing blanks off output lines
        * intend:        a string to lead each output line

        Parameters are first taken from the template, if any, and
        can then tweaked by the other constructor arguments.

        """
        # (1) set default values
        self.__dict__.update(_default_params)
        # (2) override them from the template
        if template:
            self.__dict__.update(template_params(template))
        # (3) override with the constructor parameters
        self.__dict__.update(
            {k: v
             for k, v in locals().items() if v is not None})

        # Some need special checking and handling:
        # (1) cell_padding may be specified as list/tuple of 1 or 2, or as int
        if cell_pad is None:
            self.cell_pad = [0, 0]
        elif y.is_sequence(cell_pad):
            if not all(map(y.is_int, cell_pad)):
                raise ValueError(
                    "cell_pad is not a sequence of int, but {}".format(
                        repr(cell_pad)))
            if len(cell_pad) == 1:
                self.cell_pad = cell_pad * 2
            elif len(cell_pad) != 2:
                raise ValueError(
                    "cell_pad is not a sequence len 1 or 2, but {}".format(
                        len(cell_pad)))
        elif y.is_int(cell_pad):
            self.cell_pad = [cell_pad, cell_pad]
        else:
            raise ValueError(
                "cell_pad is not None or int or seq of 2 ints: {}".format(
                    repr(cell_pad)))

        # (2) Always have a separate alignment for the first and the following
        # lines. They need not be different, though. May be a list of 1 or 2
        # or a simple string or None.
        self.defaultalign = [None, None]
        if align is None:
            self.align = ["", ""]
        elif type(align) == str:
            split_align = align.split(",")
            if len(split_align) == 1:
                self.align = [align, align]
            elif len(split_align) == 2:
                self.align = split_align
            else:
                raise ValueError(
                    "more than 2 comma-separated align strings: {}".format(
                        repr(align)))
        else:
            raise ValueError("align must be a string of one or two " +
                             "comma-separated fields, " +
                             "but is {}".format(repr(align)))
        # Set default alignment if align ends with "*"
        for i in (0, 1):
            if self.align[i]:
                if self.align[i].endswith("*") and len(self.align[i]) > 1:
                    self.defaultalign[i] = self.align[i][-2]
                    self.align[i] = self.align[i][:-1]
        # Now incorporate the data, if specified here.
        if data:
            self._fill_table(data)
Пример #2
0
 def test_isseq_9(self):
     self.assertTrue(y.is_sequence(range(22)))
Пример #3
0
 def test_isseq_es(self):
     self.assertFalse(y.is_sequence(""))
Пример #4
0
 def test_isseq_7(self):
     self.assertFalse(y.is_sequence({}))
Пример #5
0
 def test_isseq_8(self):
     self.assertFalse(y.is_sequence(collections.UserString(5)))
Пример #6
0
 def test_isseq_5(self):
     self.assertTrue(y.is_sequence(collections.deque([3, 4, 5])))
Пример #7
0
 def test_isseq_6(self):
     self.assertTrue(y.is_sequence(collections.UserList([3, 4, 5])))
Пример #8
0
 def test_isseq_4(self):
     self.assertTrue(y.is_sequence((3, 4, 5)))
Пример #9
0
 def test_isseq_3(self):
     self.assertFalse(y.is_sequence("blabla"))
Пример #10
0
 def test_isseq_2(self):
     self.assertFalse(y.is_sequence(3))
Пример #11
0
 def test_isseq_1(self):
     self.assertFalse(y.is_sequence(set()))
Пример #12
0
 def test_isseq_0(self):
     self.assertTrue(y.is_sequence([]))