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)'
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)"
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)'
def get_elementwise_colors(cells, min_wins): """Returns element-wise colors over all lists of values in *cells*. """ contains_list = any(isinstance(cell, list) for cell in cells.values()) if not contains_list: return tools.get_colors(cells, min_wins) num_elements = max([len(cell) for cell in cells.values()]) res = {key : [] for key in cells} for index in range(num_elements): values = {key : cell[index] if index < len(cell) else None for key, cell in cells.items()} colors = tools.get_colors(values, min_wins) for key, color in colors.items(): res[key].append(color) return res
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)
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)
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)
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)