예제 #1
0
def rgb_fractions_to_html_colors(colors):
    if isinstance(colors, list):
        res = []
        for color in colors:
            res.append(tools.rgb_fractions_to_html_color(*color))
        return res
    return tools.rgb_fractions_to_html_color(*colors)
예제 #2
0
def test_colors():
    row = {'col 1' : 0, 'col 2' : 0.5, 'col 3' : 1}
    expected_min_wins = {'col 1' : (0.0, 0.7, 0.0), 'col 2' : (0.0, 0.7, 0.7), 'col 3' : (0.0, 0.0, 0.7)}
    expected_max_wins = {'col 1' : (0.0, 0.0, 0.7), 'col 2' : (0.0, 0.7, 0.7), 'col 3' : (0.0, 0.7, 0.0)}
    assert tools.get_colors(row, True) == expected_min_wins
    assert tools.get_colors(row, False) == expected_max_wins
    assert tools.rgb_fractions_to_html_color(1, 0, 0.5) == 'rgb(255,0,127)'
예제 #3
0
파일: __init__.py 프로젝트: galdreiman/PAC
    def _format_row(self, row_name, row):
        """Format all entries in **row** (in place)."""
        if row_name == self.header_row:
            for col_name, value in row.items():
                row[col_name] = value.replace('_', '_' + ESCAPE_WORDBREAK)
            return

        # Get the slice of the row that should be formated (i.e. the data columns).
        # Note that there might be other columns (e.g. added by dynamic data
        # modules) that should not be formated.
        row_slice = dict((col_name, row.get(col_name))
                         for col_name in self.col_names)

        min_value, max_value = tools.get_min_max(row_slice.values())

        min_wins = self.get_min_wins(row_name)
        highlight = min_wins is not None
        colors = tools.get_colors(row_slice, min_wins) if self.colored else None

        for col_name, value in row.items():
            color = None
            bold = False
            # Format data columns
            if col_name in row_slice:
                rounded_value = round(value, 2) if isinstance(value, float) else value
                if self.colored:
                    color = tools.rgb_fractions_to_html_color(*colors[col_name])
                elif highlight and (rounded_value == min_value and min_wins or
                                    rounded_value == max_value and not min_wins):
                    bold = True
            row[col_name] = self._format_cell(row_name, col_name, value,
                                              color=color, bold=bold)
예제 #4
0
    def _format_row(self, row_name, row):
        """Format all entries in **row** (in place)."""
        if row_name == self.header_row:
            for col_name, value in row.items():
                # Allow breaking after underlines.
                value = value.replace("_", "_" + ESCAPE_WORDBREAK)
                # Right-align headers (except the left-most one).
                if col_name != self.header_column:
                    value = " " + value
                row[col_name] = value
            return

        # Get the slice of the row that should be formated (i.e. the data columns).
        # Note that there might be other columns (e.g. added by dynamic data
        # modules) that should not be formated.
        row_slice = {
            col_name: row.get(col_name)
            for col_name in self.col_names
        }

        min_wins = self.get_min_wins(row_name)
        highlight = min_wins is not None
        colored = self.colored and highlight
        colors = tools.get_colors(row_slice, min_wins) if colored else None

        if highlight:
            min_value, max_value = tools.get_min_max(row_slice.values())
        else:
            min_value, max_value = None, None

        def is_close(a, b):
            # Highlight based on precision visible in table, not actual values.
            return self._format_value(a) == self._format_value(b)

        for col_name, value in row.items():
            color = None
            bold = False
            # Format data columns
            if col_name in row_slice:
                if colored:
                    color = tools.rgb_fractions_to_html_color(
                        *colors[col_name])
                elif (highlight and value is not None
                      and ((is_close(value, min_value) and min_wins) or
                           (is_close(value, max_value) and not min_wins))):
                    bold = True
            row[col_name] = self._format_cell(row_name,
                                              col_name,
                                              value,
                                              color=color,
                                              bold=bold)
예제 #5
0
def test_colors():
    row = {"col 1": 0, "col 2": 0.5, "col 3": 1}
    expected_min_wins = {
        "col 1": (0.0, 0.7, 0.0),
        "col 2": (0.0, 0.7, 0.7),
        "col 3": (0.0, 0.0, 0.7),
    }
    expected_max_wins = {
        "col 1": (0.0, 0.0, 0.7),
        "col 2": (0.0, 0.7, 0.7),
        "col 3": (0.0, 0.7, 0.0),
    }
    assert tools.get_colors(row, True) == expected_min_wins
    assert tools.get_colors(row, False) == expected_max_wins
    assert tools.rgb_fractions_to_html_color(1, 0, 0.5) == "rgb(255,0,127)"
예제 #6
0
def test_colors():
    row = {'col 1': 0, 'col 2': 0.5, 'col 3': 1}
    expected_min_wins = {
        'col 1': (0.0, 0.7, 0.0),
        'col 2': (0.0, 0.7, 0.7),
        'col 3': (0.0, 0.0, 0.7)
    }
    expected_max_wins = {
        'col 1': (0.0, 0.0, 0.7),
        'col 2': (0.0, 0.7, 0.7),
        'col 3': (0.0, 0.7, 0.0)
    }
    assert tools.get_colors(row, True) == expected_min_wins
    assert tools.get_colors(row, False) == expected_max_wins
    assert tools.rgb_fractions_to_html_color(1, 0, 0.5) == 'rgb(255,0,127)'
예제 #7
0
    def _format_row(self, row_name, row):
        """Format all entries in **row** (in place)."""
        if row_name == self.header_row:
            for col_name, value in row.items():
                # Allow breaking after underlines.
                value = value.replace('_', '_' + ESCAPE_WORDBREAK)
                # Right-align headers (except the left-most one).
                if col_name != self.header_column:
                    value = ' ' + value
                row[col_name] = value
            return

        # Get the slice of the row that should be formated (i.e. the data columns).
        # Note that there might be other columns (e.g. added by dynamic data
        # modules) that should not be formated.
        row_slice = dict(
            (col_name, row.get(col_name)) for col_name in self.col_names)

        min_wins = self.get_min_wins(row_name)
        highlight = min_wins is not None
        colored = self.colored and highlight
        colors = tools.get_colors(row_slice, min_wins) if colored else None

        if highlight:
            min_value, max_value = tools.get_min_max(row_slice.values())
        else:
            min_value, max_value = None, None

        def is_close(a, b, rel_tol=1e-09, abs_tol=0.0):
            return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

        for col_name, value in row.items():
            color = None
            bold = False
            # Format data columns
            if col_name in row_slice:
                if colored:
                    color = tools.rgb_fractions_to_html_color(
                        *colors[col_name])
                elif highlight and value is not None and (
                    (is_close(value, min_value) and min_wins) or
                    (is_close(value, max_value) and not min_wins)):
                    bold = True
            row[col_name] = self._format_cell(row_name,
                                              col_name,
                                              value,
                                              color=color,
                                              bold=bold)
예제 #8
0
    def _format_row(self, row_name, row):
        """Format all entries in **row** (in place)."""
        if row_name == self.header_row:
            for col_name, value in row.items():
                row[col_name] = value.replace('_', '_' + ESCAPE_WORDBREAK)
            return

        # Get the slice of the row that should be formated (i.e. the data columns).
        # Note that there might be other columns (e.g. added by dynamic data
        # modules) that should not be formated.
        row_slice = dict(
            (col_name, row.get(col_name)) for col_name in self.col_names)

        min_value, max_value = tools.get_min_max(row_slice.values())

        min_wins = self.get_min_wins(row_name)
        highlight = min_wins is not None
        colors = tools.get_colors(row_slice,
                                  min_wins) if self.colored else None

        for col_name, value in row.items():
            color = None
            bold = False
            # Format data columns
            if col_name in row_slice:
                rounded_value = round(value, 2) if isinstance(value,
                                                              float) else value
                if self.colored:
                    color = tools.rgb_fractions_to_html_color(
                        *colors[col_name])
                elif highlight and (rounded_value == min_value and min_wins
                                    or rounded_value == max_value
                                    and not min_wins):
                    bold = True
            row[col_name] = self._format_cell(row_name,
                                              col_name,
                                              value,
                                              color=color,
                                              bold=bold)