File: formatters.py

package info (click to toggle)
python-pygal 3.0.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: python: 7,359; makefile: 9
file content (114 lines) | stat: -rw-r--r-- 3,007 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
# This file is part of pygal
#
# A python svg graph plotting library
# Copyright © 2012-2016 Kozea
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
"""
Formatters to use with `value_formatter` and `x_value_formatter` configs

"""
from __future__ import division

from datetime import date, datetime, time
from math import floor, log

from pygal.util import float_format


class Formatter(object):
    pass


class HumanReadable(Formatter):
    """Format a number to engineer scale"""
    ORDERS = "yzafpnµm kMGTPEZY"

    def __init__(self, none_char='∅'):
        self.none_char = none_char

    def __call__(self, val):
        if val is None:
            return self.none_char
        order = val and int(floor(log(abs(val)) / log(1000)))
        orders = self.ORDERS.split(" ")[int(order > 0)]
        if order == 0 or order > len(orders):
            return float_format(val / (1000**int(order)))
        return (
            float_format(val / (1000**int(order))) +
            orders[int(order) - int(order > 0)]
        )


class Significant(Formatter):
    """Show precision significant digit of float"""

    def __init__(self, precision=10):
        self.format = '%%.%dg' % precision

    def __call__(self, val):
        if val is None:
            return ''
        return self.format % val


class Integer(Formatter):
    """Cast number to integer"""

    def __call__(self, val):
        if val is None:
            return ''
        return '%d' % val


class Raw(Formatter):
    """Cast everything to string"""

    def __call__(self, val):
        if val is None:
            return ''
        return str(val)


class IsoDateTime(Formatter):
    """Iso format datetimes"""

    def __call__(self, val):
        if val is None:
            return ''
        return val.isoformat()


class Default(Significant, IsoDateTime, Raw):
    """Try to guess best format from type"""

    def __call__(self, val):
        if val is None:
            return ''
        if isinstance(val, (int, float)):
            return Significant.__call__(self, val)
        if isinstance(val, (date, time, datetime)):
            return IsoDateTime.__call__(self, val)
        return Raw.__call__(self, val)


# Formatters with default options
human_readable = HumanReadable()
significant = Significant()
integer = Integer()
raw = Raw()

# Default config formatter
default = Default()