def format_choices(choices, uniformat=True, si=None): """ Transform the given choices into an ordered list of (value, formatted value) tuples Args: choices (Iterable): The choices to be formatted or None uniformat (bool): If True, all the values will be formatted using the same SI prefix si (bool): si unit to format the choice values to. This argument takes precedence over the `uniformat` argument. Returns: ([(value, formatted value)], si prefix) or (None, None) """ if choices: choices_si_prefix = None # choice_fmt is an iterable of tuples: (choice, formatted choice) if isinstance(choices, dict): # In this case we assume that the values are already formatted choices_formatted = choices.items() elif si is not None: fmt, choices_si_prefix = utun.si_scale_list(choices, si) choices_formatted = zip(choices, [u"%g" % c for c in fmt]) elif len(choices) > 1 and all([isinstance(c, numbers.Real) for c in choices]): try: # TODO: does it matter? Can't we always do non-uniform? # Or just guess based on the magnitude between the lowest and biggest value? if uniformat: fmt, choices_si_prefix = utun.si_scale_list(choices) choices_formatted = zip(choices, [u"%g" % c for c in fmt]) else: fmt = [] for choice in choices: fmt.append(to_string_si_prefix(choice)) return zip(choices, fmt), choices_si_prefix except Exception: logging.exception("Formatting error!") choices_formatted = [(c, choice_to_str(c)) for c in choices] else: choices_formatted = [(c, choice_to_str(c)) for c in choices] if not isinstance(choices, OrderedDict): choices_formatted = sorted(choices_formatted) return choices_formatted, choices_si_prefix else: return None, None
def test_to_string_si_prefix(self): # (input) (expected output) values = [((1.0,), "1 "), ((-1.234,), "-1.234 "), ((-1234,), "-1.234 k"), ((1600,), "1.6 k"), ((-1600,), "-1.6 k"), ((0.0001236,), u"123.6 µ"), ((0.0012,), "1.2 m"), ((0,), "0 "), ] for (i, eo) in values: o = units.to_string_si_prefix(*i) self.assertEquals(o, eo, u"%f is '%s' while expected '%s'" % (i[0], o, eo))
def format_choices(choices): """ Transform the given choices into an ordered list of (value, formatted value) tuples Args: choices (Iterable): The choices to be formatted or None Returns: ([(value, formatted value)], si prefix) or (None, None) """ if choices: choices_si_prefix = None # choice_fmt is an iterable of tuples: (choice, formatted choice) if isinstance(choices, dict): # In this case we assume that the values are already formatted choices_formatted = choices.items() elif len(choices) > 1 and all(isinstance(c, numbers.Real) for c in choices): try: choices = sorted(choices) # Can we fit them (more or less) all with the same unit prefix? mn_non0 = min(c for c in choices if c != 0) if abs(choices[-1] / mn_non0) < 1000: fmt, choices_si_prefix = utun.si_scale_list(choices) fmt = [utun.to_string_pretty(c, 3) for c in fmt] choices_formatted = zip(choices, fmt) else: fmt = [to_string_si_prefix(c, sig=3) for c in choices] return zip(choices, fmt), None except Exception: logging.exception("Formatting error for %s", choices) choices_formatted = [(c, choice_to_str(c)) for c in choices] else: choices_formatted = [(c, choice_to_str(c)) for c in choices] if not isinstance(choices, OrderedDict): choices_formatted = sorted(choices_formatted) return choices_formatted, choices_si_prefix else: return None, None