Exemple #1
0
    def test_default_formats(self):
        B = float("-1,250,500,880.76".replace(",", ""))
        M = B / 1000.0
        k = M / 1000.0
        p = k / 1000000.0
        tests = [
            (B, "$(1.3B)", fmt.BillionDollarsFormatter),
            (B, "(1.3B)", fmt.BillionsFormatter),
            (M, "$(1.3M)", fmt.MillionDollarsFormatter),
            (M, "(1.3M)", fmt.MillionsFormatter),
            (k, "$(1.3k)", fmt.ThousandDollarsFormatter),
            (k, "(1.3k)", fmt.ThousandsFormatter),
            (k, "(1,250.50)", fmt.FloatFormatter),
            (k, "(1,251)", fmt.IntFormatter),
            # Floats
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            (str(k), "-1,251",
             fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            # Percents
            (0.12433, "12.4%", fmt.new_percent_formatter(commas=1,
                                                         precision=1)),
            (0.12433, "12.433%",
             fmt.new_percent_formatter(commas=1, precision=3)),
            (
                -0.12433,
                "-12.4%",
                fmt.new_percent_formatter(commas=1, parens=0, precision=1),
            ),
            (
                -0.12433,
                "(12.4%)",
                fmt.new_percent_formatter(commas=1, parens=1, precision=1),
            ),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEqual(expected, actual)
            # Test if it were a list
            actual = fct([val] * 5)
            self.assertEqual([expected] * 5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val] * 5))
            pt.assert_series_equal(pd.Series([expected] * 5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({"a": [val] * 5, "b": [val] * 5}))
            pt.assert_frame_equal(
                pd.DataFrame({
                    "a": [expected] * 5,
                    "b": [expected] * 5
                }), actual)
Exemple #2
0
    def test_default_formats(self):
        B = float('-1,250,500,880.76'.replace(',', ''))
        M = B / 1000.
        k = M / 1000.
        p = k / 1000000.
        tests = [
            (B, '$(1.3B)', fmt.BillionDollarsFormatter),
            (B, '(1.3B)', fmt.BillionsFormatter),
            (M, '$(1.3M)', fmt.MillionDollarsFormatter),
            (M, '(1.3M)', fmt.MillionsFormatter),
            (k, '$(1.3k)', fmt.ThousandDollarsFormatter),
            (k, '(1.3k)', fmt.ThousandsFormatter),
            (k, '(1,250.50)', fmt.FloatFormatter),
            (k, '(1,251)', fmt.IntFormatter),
            # Floats
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            (str(k), '-1,251',
             fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            # Percents
            (.12433, '12.4%', fmt.new_percent_formatter(commas=1,
                                                        precision=1)),
            (.12433, '12.433%', fmt.new_percent_formatter(commas=1,
                                                          precision=3)),
            (-.12433, '-12.4%',
             fmt.new_percent_formatter(commas=1, parens=0, precision=1)),
            (-.12433, '(12.4%)',
             fmt.new_percent_formatter(commas=1, parens=1, precision=1)),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEquals(expected, actual)
            # Test if it were a list
            actual = fct([val] * 5)
            self.assertEquals([expected] * 5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val] * 5))
            pt.assert_series_equal(pd.Series([expected] * 5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({'a': [val] * 5, 'b': [val] * 5}))
            pt.assert_frame_equal(
                pd.DataFrame({
                    'a': [expected] * 5,
                    'b': [expected] * 5
                }), actual)
Exemple #3
0
    def test_default_formats(self):
        B = float("-1,250,500,880.76".replace(",", ""))
        M = B / 1000.0
        k = M / 1000.0
        p = k / 1000000.0
        tests = [
            (B, "$(1.3B)", fmt.BillionDollarsFormatter),
            (B, "(1.3B)", fmt.BillionsFormatter),
            (M, "$(1.3M)", fmt.MillionDollarsFormatter),
            (M, "(1.3M)", fmt.MillionsFormatter),
            (k, "$(1.3k)", fmt.ThousandDollarsFormatter),
            (k, "(1.3k)", fmt.ThousandsFormatter),
            (k, "(1,250.50)", fmt.FloatFormatter),
            (k, "(1,251)", fmt.IntFormatter),
            # Floats
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            (str(k), "-1,251", fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            # Percents
            (0.12433, "12.4%", fmt.new_percent_formatter(commas=1, precision=1)),
            (0.12433, "12.433%", fmt.new_percent_formatter(commas=1, precision=3)),
            (-0.12433, "-12.4%", fmt.new_percent_formatter(commas=1, parens=0, precision=1)),
            (-0.12433, "(12.4%)", fmt.new_percent_formatter(commas=1, parens=1, precision=1)),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEquals(expected, actual)
            # Test if it were a list
            actual = fct([val] * 5)
            self.assertEquals([expected] * 5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val] * 5))
            pt.assert_series_equal(pd.Series([expected] * 5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({"a": [val] * 5, "b": [val] * 5}))
            pt.assert_frame_equal(pd.DataFrame({"a": [expected] * 5, "b": [expected] * 5}), actual)
Exemple #4
0
    def test_default_formats(self):
        B = float('-1,250,500,880.76'.replace(',', ''))
        M = B / 1000.
        k = M / 1000.
        p = k / 1000000.
        tests = [
            (B, '$(1.3B)', fmt.BillionDollarsFormatter),
            (B, '(1.3B)', fmt.BillionsFormatter),
            (M, '$(1.3M)', fmt.MillionDollarsFormatter),
            (M, '(1.3M)', fmt.MillionsFormatter),
            (k, '$(1.3k)', fmt.ThousandDollarsFormatter),
            (k, '(1.3k)', fmt.ThousandsFormatter),
            (k, '(1,250.50)', fmt.FloatFormatter),
            (k, '(1,251)', fmt.IntFormatter),
            # Floats
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            (str(k), '-1,251', fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            # Percents
            (.12433, '12.4%', fmt.new_percent_formatter(commas=1, precision=1)),
            (.12433, '12.433%', fmt.new_percent_formatter(commas=1, precision=3)),
            (-.12433, '-12.4%', fmt.new_percent_formatter(commas=1, parens=0, precision=1)),
            (-.12433, '(12.4%)', fmt.new_percent_formatter(commas=1, parens=1, precision=1)),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEqual(expected, actual)
            # Test if it were a list
            actual = fct([val]*5)
            self.assertEqual([expected]*5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val]*5))
            pt.assert_series_equal(pd.Series([expected]*5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({'a': [val]*5, 'b': [val]*5}))
            pt.assert_frame_equal(pd.DataFrame({'a': [expected]*5, 'b': [expected]*5}), actual)
Exemple #5
0

def pad_positive_wrapper(fmtfct):
    """Ensure that numbers are aligned in table by appending a blank space to postive values if 'parenthesis' are
    used to denote negative numbers"""

    def check_and_append(*args, **kwargs):
        result = fmtfct(*args, **kwargs)
        if fmtfct.parens and not result.endswith(')'):
            result += ' '
        return result

    return check_and_append


IntFormatter = pad_positive_wrapper(fmt.new_int_formatter(nan='-'))
FloatFormatter = pad_positive_wrapper(fmt.new_float_formatter(nan='-'))
PercentFormatter = pad_positive_wrapper(fmt.new_percent_formatter(nan='-'))
ThousandsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(nan='-'))
MillionsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(nan='-'))
BillionsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(nan='-'))
# Don't attempt to pad
DynamicNumberFormatter = fmt.DynamicNumberFormat(nan='-', pcts=1, trunc_dot_zeros=1)

DollarCentsFormatter = pad_positive_wrapper(fmt.new_float_formatter(prefix='$', nan='-'))
DollarFormatter = pad_positive_wrapper(fmt.new_int_formatter(prefix='$', nan='-'))
ThousandDollarsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(prefix='$', nan='-'))
MillionDollarsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(prefix='$', nan='-'))
BillionDollarsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(prefix='$', nan='-'))
YmdFormatter = fmt.new_datetime_formatter('%Y%m%d', True)
Y_m_dFormatter = fmt.new_datetime_formatter('%Y_%m_%d', True)
Exemple #6
0
ConditionalRedBlack = lambda x: x < 0 and dict(TEXTCOLOR=HexColor("#800000"))


def pad_positive_wrapper(fmtfct):
    """Ensure that numbers are aligned in table by appending a blank space to postive values if 'parenthesis' are
    used to denote negative numbers"""
    def check_and_append(*args, **kwargs):
        result = fmtfct(*args, **kwargs)
        if fmtfct.parens and not result.endswith(')'):
            result += ' '
        return result

    return check_and_append


IntFormatter = pad_positive_wrapper(fmt.new_int_formatter(nan='-'))
FloatFormatter = pad_positive_wrapper(fmt.new_float_formatter(nan='-'))
PercentFormatter = pad_positive_wrapper(fmt.new_percent_formatter(nan='-'))
ThousandsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(nan='-'))
MillionsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(nan='-'))
BillionsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(nan='-'))
# Don't attempt to pad
DynamicNumberFormatter = fmt.DynamicNumberFormat(method='col',
                                                 nan='-',
                                                 pcts=1,
                                                 trunc_dot_zeros=1)

DollarCentsFormatter = pad_positive_wrapper(
    fmt.new_float_formatter(prefix='$', nan='-'))
DollarFormatter = pad_positive_wrapper(
    fmt.new_int_formatter(prefix='$', nan='-'))
Exemple #7
0

def pad_positive_wrapper(fmtfct):
    """Ensure that numbers are aligned in table by appending a blank space to postive values if 'parenthesis' are
    used to denote negative numbers"""

    def check_and_append(*args, **kwargs):
        result = fmtfct(*args, **kwargs)
        if fmtfct.parens and not result.endswith(")"):
            result += " "
        return result

    return check_and_append


IntFormatter = pad_positive_wrapper(fmt.new_int_formatter(nan="-"))
FloatFormatter = pad_positive_wrapper(fmt.new_float_formatter(nan="-"))
PercentFormatter = pad_positive_wrapper(fmt.new_percent_formatter(nan="-"))
ThousandsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(nan="-"))
MillionsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(nan="-"))
BillionsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(nan="-"))
# Don't attempt to pad
DynamicNumberFormatter = fmt.DynamicNumberFormat(method="col", nan="-", pcts=1, trunc_dot_zeros=1)

DollarCentsFormatter = pad_positive_wrapper(fmt.new_float_formatter(prefix="$", nan="-"))
DollarFormatter = pad_positive_wrapper(fmt.new_int_formatter(prefix="$", nan="-"))
ThousandDollarsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(prefix="$", nan="-"))
MillionDollarsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(prefix="$", nan="-"))
BillionDollarsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(prefix="$", nan="-"))
YmdFormatter = fmt.new_datetime_formatter("%Y%m%d", True)
Y_m_dFormatter = fmt.new_datetime_formatter("%Y-%m-%d", True)
Exemple #8
0
ConditionalRedBlack = lambda x: x < 0 and dict(TEXTCOLOR=HexColor("#800000"))


def pad_positive_wrapper(fmtfct):
    """Ensure that numbers are aligned in table by appending a blank space to postive values if 'parenthesis' are
    used to denote negative numbers"""
    def check_and_append(*args, **kwargs):
        result = fmtfct(*args, **kwargs)
        if fmtfct.parens and not result.endswith(")"):
            result += " "
        return result

    return check_and_append


IntFormatter = pad_positive_wrapper(fmt.new_int_formatter(nan="-"))
FloatFormatter = pad_positive_wrapper(fmt.new_float_formatter(nan="-"))
PercentFormatter = pad_positive_wrapper(fmt.new_percent_formatter(nan="-"))
ThousandsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(nan="-"))
MillionsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(nan="-"))
BillionsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(nan="-"))
# Don't attempt to pad
DynamicNumberFormatter = fmt.DynamicNumberFormat(method="col",
                                                 nan="-",
                                                 pcts=1,
                                                 trunc_dot_zeros=1)

DollarCentsFormatter = pad_positive_wrapper(
    fmt.new_float_formatter(prefix="$", nan="-"))
DollarFormatter = pad_positive_wrapper(
    fmt.new_int_formatter(prefix="$", nan="-"))