def format_axis_choices(name, axis_def): """ Transform the given choices for an axis into an user friendly display name (str): the name of the axis axis_def (Axis): the axis definition returns: choices_formatted (None or list of (value, str): axis value/user-friendly display name (including the unit). None if axis doesn't support choices. """ try: choices = axis_def.choices except AttributeError: return None if not choices: return None unit = axis_def.unit if isinstance(choices, dict): choices_formatted = list(choices.items()) # In this case, normally the values are already formatted, but for # wavelength band, the "formatted" value is still a band info (ie, two # values in m) if name == "band": def to_readable_band(v): if (isinstance(v, (tuple, list)) and len(v) > 1 and all(isinstance(c, numbers.Real) for c in v)): return fluo.to_readable_band(v) else: return v choices_formatted = [(k, to_readable_band(v)) for k, v in choices_formatted] elif len(choices) > 1 and all(isinstance(c, numbers.Real) for c in choices): choices_formatted = None 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, unit) for c in fmt] choices_formatted = list(zip(choices, fmt)) except Exception: logging.exception("Formatting error for %s", choices) if choices_formatted is None: choices_formatted = [(c, readable_str(c, unit=unit, sig=3)) for c in choices] else: choices_formatted = [(c, u"%s %s" % (choice_to_str(c), unit)) for c in choices] if not isinstance(choices, OrderedDict): # sort 2-tuples = according to first value in tuple choices_formatted = sorted(choices_formatted) return choices_formatted
def format_axis_choices(name, axis_def): """ Transform the given choices for an axis into an user friendly display name (str): the name of the axis axis_def (Axis): the axis definition returns: choices_formatted (None or list of (value, str): axis value/user-friendly display name (including the unit). None if axis doesn't support choices. """ try: choices = axis_def.choices except AttributeError: return None if not choices: return None unit = axis_def.unit if isinstance(choices, dict): choices_formatted = choices.items() # In this case, normally the values are already formatted, but for # wavelength band, the "formatted" value is still a band info (ie, two # values in m) if name == "band": def to_readable_band(v): if (isinstance(v, (tuple, list)) and len(v) > 1 and all(isinstance(c, numbers.Real) for c in v)): return fluo.to_readable_band(v) else: return v choices_formatted = [(k, to_readable_band(v)) for k, v in choices_formatted] elif len(choices) > 1 and all(isinstance(c, numbers.Real) for c in choices): choices_formatted = None 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, unit) for c in fmt] choices_formatted = zip(choices, fmt) except Exception: logging.exception("Formatting error for %s", choices) if choices_formatted is None: choices_formatted = [(c, readable_str(c, unit=unit, sig=3)) for c in choices] else: choices_formatted = [(c, u"%s %s" % (choice_to_str(c), unit)) for c in choices] if not isinstance(choices, OrderedDict): # sort 2-tuples = according to first value in tuple choices_formatted = sorted(choices_formatted) return choices_formatted
def test_to_string_pretty(self): values = [ 0.000000041003, 0.0051, 0.014, 0.39, 0.230234543545, ] for sig in [2, 4, 6]: # (None, 0, 1, 2, 4, 8): for v in values: self.assertEqual(units.round_significant(v, sig), float(units.to_string_pretty(v, sig, "s")))
def test_to_string_pretty(self): values = [ 0.000000041003, 0.0051, 0.014, 0.39, 0.230234543545, ] for sig in [2, 4, 6]: # (None, 0, 1, 2, 4, 8): for v in values: self.assertEqual( units.round_significant(v, sig), float(units.to_string_pretty(v, sig, "s")) )
def _display_raw(self): """ Set the current text to raw style (no truncation/no unit) """ if self.number is None: str_val = u"" else: if hasattr(self, 'unit'): unit = self.unit #pylint: disable=E1101 else: unit = None if self.accuracy is None: accuracy = None else: accuracy = self.accuracy + 1 str_val = units.to_string_pretty(self.number, accuracy, unit) wx.TextCtrl.ChangeValue(self, str_val)
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