Exemple #1
0
    def test_format_strings(self):
        pv = PrettyValue("s")
        results = pv.format("test")
        self.assertEquals(results, "test")

        pv = PrettyValue("10s")
        results = pv.format("test")
        self.assertEquals(results, "test      ")

        pv = PrettyValue(">10s")
        results = pv.format("test")
        self.assertEquals(results, "      test")

        pv = PrettyValue("^10s")
        results = pv.format("test")
        self.assertEquals(results, "   test   ")

        pv = PrettyValue("10s")
        results = pv.format(5.25)
        self.assertEquals(results, "5.25      ")

        self.assertRaises(ValueError, PrettyValue, "=10s")

        self.assertRaises(ValueError, PrettyValue, "=10s")

        self.assertRaises(ValueError, PrettyValue, "-10s")

        self.assertRaises(ValueError, PrettyValue, "10.2s")
Exemple #2
0
    def test_format_percents(self):
        pv = PrettyValue(".2%")
        results = pv.format(".0525")
        self.assertEquals(results, "5.25%")

        pv = PrettyValue("%")
        results = pv.format("1")
        self.assertEquals(results, "100.0000%")
Exemple #3
0
    def test_parse_rawtype_date(self):
        pv = PrettyValue()

        results = pv.parse_rawtype("%Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)
Exemple #4
0
    def test_format_unknowns(self):
        d = datetime.datetime(2010, 7, 4, 12, 15, 58)

        pv = PrettyValue("%Y-%m-%d")
        results = pv.format(d)
        self.assertEquals(results, "2010-07-04")

        self.assertRaises(ValueError, PrettyValue, " %Y-%m-%d")

        pv = PrettyValue(">10%Y")
        results = pv.format(d)
        self.assertEquals(results, "      2010")
Exemple #5
0
    def test_parse_rawtype_align(self):
        pv = PrettyValue()

        results = pv.parse_rawtype(">")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], ">")
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("<")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], "<")
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("^")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], "^")
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype(">i")
        self.assertEquals(results["atype"], "i")
        self.assertEquals(results["align"], ">")
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("^.2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], "^")
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("^-.2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], "^")
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("=+5.2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], "=")
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("> %Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], ">")
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], " ")
        self.assertEquals(results["precision"], None)
Exemple #6
0
    def test_maxwidth(self):
        pv = PrettyValue("10s")
        results = pv.format("test")
        self.assertEquals(pv.maxwidth, 10)

        pv = PrettyValue("10s")
        results = pv.format("testing is lots of fun")
        self.assertEquals(pv.maxwidth, 22)

        pv = PrettyValue("i")
        results = pv.format(5123)
        self.assertEquals(pv.maxwidth, 4)

        pv = PrettyValue("5.1f")
        results = pv.format(5123.23456)
        self.assertEquals(pv.maxwidth, 6)
Exemple #7
0
    def test_parse_rawtype_width(self):
        pv = PrettyValue()

        results = pv.parse_rawtype("5")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("5s")
        self.assertEquals(results["atype"], "s")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("5i")
        self.assertEquals(results["atype"], "i")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("5f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("5%Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)
Exemple #8
0
    def test_format_fill(self):
        pv = PrettyValue("10s", fill="*")
        results = pv.format("test")
        self.assertEquals(results, "test******")

        pv = PrettyValue("^10s", fill="-")
        results = pv.format("test")
        self.assertEquals(results, "---test---")

        pv = PrettyValue("^10i", fill="-")
        results = pv.format("5")
        self.assertEquals(results, "----5-----")

        pv = PrettyValue("^+10.2f", fill="-")
        results = pv.format(5.23456)
        self.assertEquals(results, "--+5.23---")

        pv = PrettyValue("10i", fill=0)
        results = pv.format(5)
        self.assertEquals(results, "0000000005")

        pv = PrettyValue("-10i", fill=0)
        results = pv.format(-5)
        self.assertEquals(results, "-000000005")

        pv = PrettyValue(">10i", fill=0)
        results = pv.format(-5)
        self.assertEquals(results, "00000000-5")

        pv = PrettyValue("=10i", fill=0)
        results = pv.format(-5)
        self.assertEquals(results, "-000000005")

        pv = PrettyValue(" 10i", fill=0)
        results = pv.format(5)
        self.assertEquals(results, " 000000005")
Exemple #9
0
    def test_format_floats(self):
        # default precision is used of 4 and it does round up.
        pv = PrettyValue("f")
        results = pv.format("5.256678")
        self.assertEquals(results, "5.2567")

        # no sign just size
        pv = PrettyValue("10f")
        results = pv.format("5.256678")
        self.assertEquals(results, "    5.2567")

        # same size but small precision
        pv = PrettyValue("10.2f")
        results = pv.format("5.256678")
        self.assertEquals(results, "      5.26")

        # same size but bigger precision
        pv = PrettyValue("10.8f")
        results = pv.format(5.256678)
        self.assertEquals(results, "5.25667800")

        pv = PrettyValue("<10.2f")
        results = pv.format(5.256678)
        self.assertEquals(results, "5.26      ")

        pv = PrettyValue("^10.1f")
        results = pv.format(5)
        self.assertEquals(results, "   5.0    ")

        pv = PrettyValue("< 10f")
        results = pv.format(5)
        self.assertEquals(results, " 5.0000   ")

        # positive sign and size
        pv = PrettyValue("+10f")
        results = pv.format(5)
        self.assertEquals(results, "+   5.0000")

        pv = PrettyValue(">+10.1f")
        results = pv.format(5)
        self.assertEquals(results, "      +5.0")

        pv = PrettyValue("<+10.1f")
        results = pv.format(5.256)
        self.assertEquals(results, "+5.3      ")

        pv = PrettyValue("^+10.1f")
        results = pv.format(5.233)
        self.assertEquals(results, "   +5.2   ")

        # negative number and size
        pv = PrettyValue("10f")
        results = pv.format("-5.25634")
        self.assertEquals(results, "-   5.2563")

        pv = PrettyValue(">10.2f")
        results = pv.format("-5.33333")
        self.assertEquals(results, "     -5.33")

        pv = PrettyValue("< 10f")
        results = pv.format("-5.3333")
        self.assertEquals(results, "-5.3333   ")

        pv = PrettyValue("<10.2f")
        results = pv.format(-5.256)
        self.assertEquals(results, "-5.26     ")

        pv = PrettyValue("^10.2f")
        results = pv.format("-5")
        self.assertEquals(results, "  -5.00   ")

        pv = PrettyValue(" f")
        results = pv.format(5)
        self.assertEquals(results, " 5.0000")

        pv = PrettyValue(" f")
        results = pv.format(-5)
        self.assertEquals(results, "-5.0000")

        pv = PrettyValue("f")
        self.assertRaises(ValueError, pv.format, "test")
Exemple #10
0
    def test_format_ints(self):
        pv = PrettyValue("i")
        results = pv.format("5")
        self.assertEquals(results, "5")

        # no sign just size
        pv = PrettyValue("10i")
        results = pv.format("5")
        self.assertEquals(results, "         5")

        pv = PrettyValue("<10i")
        results = pv.format("5")
        self.assertEquals(results, "5         ")

        pv = PrettyValue("^10i")
        results = pv.format("5")
        self.assertEquals(results, "    5     ")

        pv = PrettyValue("< 10i")
        results = pv.format("5")
        self.assertEquals(results, " 5        ")

        # positive sign and size
        pv = PrettyValue("+10i")
        results = pv.format("5")
        self.assertEquals(results, "+        5")

        pv = PrettyValue(">+10i")
        results = pv.format("5")
        self.assertEquals(results, "        +5")

        pv = PrettyValue("<+10i")
        results = pv.format("5")
        self.assertEquals(results, "+5        ")

        pv = PrettyValue("^+10i")
        results = pv.format("5")
        self.assertEquals(results, "    +5    ")

        # negative number and size
        pv = PrettyValue("10i")
        results = pv.format("-5")
        self.assertEquals(results, "-        5")

        pv = PrettyValue(">10i")
        results = pv.format("-5")
        self.assertEquals(results, "        -5")

        pv = PrettyValue("< 10i")
        results = pv.format("-5")
        self.assertEquals(results, "-5        ")

        pv = PrettyValue("<10i")
        results = pv.format("-5")
        self.assertEquals(results, "-5        ")

        pv = PrettyValue("^10i")
        results = pv.format("-5")
        self.assertEquals(results, "    -5    ")

        # test float conversion to int formatting
        pv = PrettyValue("i")
        results = pv.format(5.25)
        self.assertEquals(results, "5")

        pv = PrettyValue(" i")
        results = pv.format(5)
        self.assertEquals(results, " 5")

        pv = PrettyValue(" i")
        results = pv.format(-5)
        self.assertEquals(results, "-5")

        pv = PrettyValue("+10i")
        results = pv.format(5)
        self.assertEquals(results, "+        5")

        self.assertRaises(ValueError, PrettyValue, "10.2i")

        pv = PrettyValue("i")
        self.assertRaises(ValueError, pv.format, "test")
Exemple #11
0
    def test_parse_rawtype_precision(self):
        pv = PrettyValue()

        results = pv.parse_rawtype(".")
        self.assertEquals(results["atype"], ".")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype(".f")
        self.assertEquals(results["atype"], ".f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype(".2s")
        self.assertEquals(results["atype"], "s")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype(".2i")
        self.assertEquals(results["atype"], "i")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("5.2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype(".2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype(".2 f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("-.2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("- .2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("5.2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("+5.2f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], 2)

        results = pv.parse_rawtype("%H:%M.%S")
        self.assertEquals(results["atype"], "%H:%M.%S")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], None)
        self.assertEquals(results["precision"], None)
Exemple #12
0
    def test_parse_rawtype_sign(self):
        pv = PrettyValue()

        results = pv.parse_rawtype("-")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("-5")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+5")
        self.assertEquals(results["atype"], None)
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], 5)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("-s")
        self.assertEquals(results["atype"], "s")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+s")
        self.assertEquals(results["atype"], "s")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype(" -i")
        self.assertEquals(results["atype"], "-i")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], " ")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("-i")
        self.assertEquals(results["atype"], "i")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("- i")
        self.assertEquals(results["atype"], "i")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+i")
        self.assertEquals(results["atype"], "i")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+ i")
        self.assertEquals(results["atype"], "i")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("-f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("- f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+ f")
        self.assertEquals(results["atype"], "f")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype(" %Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], " ")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("-%Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("- %Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "-")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+%Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)

        results = pv.parse_rawtype("+ %Y-%m-%d")
        self.assertEquals(results["atype"], "%Y-%m-%d")
        self.assertEquals(results["align"], None)
        self.assertEquals(results["width"], None)
        self.assertEquals(results["sign"], "+")
        self.assertEquals(results["precision"], None)