Beispiel #1
0
    def to_strings(self, *, quote=True, pad=False, truncate_width=inf):
        """
        Return vector as strings formatted for display.

        >>> vector = di.Vector([1/2, 1/3, 1/4])
        >>> vector.to_strings()
        """
        if self.length == 0:
            return self.__class__.fast([], str)
        identity = lambda x, *args, **kwargs: x
        quote = util.quote if quote else identity
        pad = util.pad if pad else identity
        if self.is_float():
            strings = util.format_floats(self)
            return self.__class__.fast(pad(strings), str)
        if self.is_integer():
            strings = ["{:d}".format(x) for x in self]
            return self.__class__.fast(pad(strings), str)
        if self.is_object():
            strings = [str(x) for x in self]
            for i in range(len(strings)):
                if len(strings[i]) > truncate_width:
                    strings[i] = strings[i][:(truncate_width - 1)] + "…"
            return self.__class__.fast(pad(strings), str)
        if self.is_string():
            strings = [quote(x) for x in self]
            for i in range(len(strings)):
                if len(strings[i]) > truncate_width:
                    strings[i] = strings[i][:(truncate_width - 1)] + "…"
            return self.__class__.fast(pad(strings), str)
        strings = [str(x) for x in self]
        return self.__class__.fast(pad(strings), str)
Beispiel #2
0
 def test_format_floats_inf(self):
     a = [-math.inf, 0, math.inf, np.nan]
     b = ["-inf", "0e+00", "inf", "nan"]
     assert util.format_floats(a) == b
Beispiel #3
0
 def test_format_floats_integer(self):
     a = [1.0, 2.0, 3.0, np.nan]
     b = ["1", "2", "3", "nan"]
     assert util.format_floats(a) == b
Beispiel #4
0
 def test_format_floats_5(self):
     a = [12345678, 1234567812345678, 123456781234567812345678, np.nan]
     b = ["1.234568e+07", "1.234568e+15", "1.234568e+23", "nan"]
     assert util.format_floats(a) == b
Beispiel #5
0
 def test_format_floats_4(self):
     a = [123.456789, 123456.789, 123456789, np.nan]
     b = ["123", "123457", "123456789", "nan"]
     assert util.format_floats(a) == b
Beispiel #6
0
 def test_format_floats_3(self):
     a = [0.123456, 1, 123.456, np.nan]
     b = ["0.123", "1.000", "123.456", "nan"]
     assert util.format_floats(a) == b
Beispiel #7
0
 def test_format_floats_2(self):
     a = [0.000123456, 0.123456, 0, np.nan]
     b = ["0.000123", "0.123456", "0.000000", "nan"]
     assert util.format_floats(a) == b
Beispiel #8
0
 def test_format_floats_1(self):
     a = [1 / 1000000000, 1 / 1000000, 1 / 1000, np.nan]
     b = ["1e-09", "1e-06", "1e-03", "nan"]
     assert util.format_floats(a) == b