示例#1
0
def test_parse_value_for_display_precision(value, precision, display_format,
                                           widget, expected):
    """
    Test the correctness of the displayed value according to the specified value precision.

    Expectations:
    The formatted presentation of the displayed value must present the numeric precision as requested, providing a
    precision consideration is applicable for the provided value, for all supported display formats.

    Parameters
    ----------
    value : int, float, hex, bin, str
        The data to be formatted
    precision : int
        The numeric precision to consider during formatting
    display_format : int
        The format type for the provided value
    widget : PyDWidget
        The widget that will display the formatted value. This object can be None
    expected : str
        The expected formatted presentation of the provided value
    """
    assert parse_value_for_display(value,
                                   precision,
                                   display_format_type=display_format,
                                   widget=widget) == expected
示例#2
0
def test_parse_value_for_display_precision_incorrect_display_format(
        caplog, value, precision, display_format, widget, expected):
    """
    Test that errors will be output into stderr.

    Parameters
    ----------
    caplog : fixture
        The fixture to capture log outputs
    value : int, float, hex, bin, str
        The incorrect data
    precision : int
        The numeric precision to consider during formatting
    display_format : int
        The format type for the provided value
    widget : PyDWidget
        The widget that will display the formatted value. This object can be None
    expected : str
        The expected formatted presentation of the provided value
    """
    parsed_value = parse_value_for_display(value,
                                           precision,
                                           display_format_type=display_format,
                                           widget=widget)
    if isinstance(value, np.ndarray):
        assert (np.array_equal(value, parsed_value))
    else:
        assert (parsed_value == value)

    # Make sure logging capture the error, and have the correct error message
    for record in caplog.records:
        assert record.levelno == logging.ERROR
    assert expected in caplog.text
示例#3
0
def test_parse_value_for_display_format(value, precision, display_format,
                                        widget, expected):
    """
    Test the correctness of the displayed value according to the specified value type.

    Expectations:
    1. For each value provided, the display format (the string representation of the value) must be as expected
    2. All supported formats are to be tested

    Parameters
    ----------
    value : int, float, hex, bin, str
        The data to be formatted
    precision : int
        The numeric precision to consider during formatting
    display_format : int
        The format type for the provided value
    widget : PyDWidget
        The widget that will display the formatted value. This object can be None
    expected : str
        The expected formatted presentation of the provided value
    """
    parsed_value = parse_value_for_display(value,
                                           precision,
                                           display_format_type=display_format,
                                           widget=widget)
    assert (parsed_value == expected)
示例#4
0
def test_parse_value_for_display_precision_incorrect_display_format(
        caplog, value, precision, display_format, widget, expected):
    """
    Test that errors will be output into stderr.

    Parameters
    ----------
    caplog : fixture
        The fixture to capture log outputs
    value : int, float, hex, bin, str
        The incorrect data
    precision : int
        The numeric precision to consider during formatting
    display_format : int
        The format type for the provided value
    widget : PyDWidget
        The widget that will display the formatted value. This object can be None
    expected : str
        The expected formatted presentation of the provided value
    """
    parsed_value = parse_value_for_display(value, precision, display_format_type=display_format, widget=widget)
    if isinstance(value, np.ndarray):
        assert (np.array_equal(value, parsed_value))
    else:
        assert(parsed_value == value)

    # Make sure logging capture the error, and have the correct error message
    for record in caplog.records:
        assert record.levelno == logging.ERROR
    assert expected in caplog.text
示例#5
0
文件: label.py 项目: lnls-sirius/hla
    def value_changed(self, new_value):
        """
        Callback invoked when the Channel value is changed.
        Sets the value of new_value accordingly at the Label.

        Parameters
        ----------
        new_value : str, int, float, bool or np.ndarray
            The new value from the channel. The type depends on the channel.
        """
        super(SiriusLabel, self).value_changed(new_value)
        # If it is a DiaplayFormat.Time, parse with siriuspy.clientarch.Time
        if self._display_format_type == self.DisplayFormat.Time:
            time = _Time(int(new_value)).time().isoformat() \
                if new_value is not None else ''
            self.setText(time)
            return

        new_value = parse_value_for_display(
            value=new_value,
            precision=self.precision,
            display_format_type=self._display_format_type,
            string_encoding=self._string_encoding,
            widget=self)
        # If the value is a string, just display it as-is, no formatting
        # needed.
        if isinstance(new_value, str):
            self.setText(new_value)
            return
        # If the value is an enum, display the appropriate enum string for
        # the value.
        if self.enum_strings and isinstance(new_value, (int, float)):
            try:
                self.setText(self.enum_strings[int(new_value)])
            except IndexError:
                self.setText(f'Index Overflow [{new_value}]')
            return
        # If the value is a number (float or int), display it using a
        # format string if necessary.
        if isinstance(new_value, (int, float)):
            if self._show_units and self._unit != '':
                new_value *= self._conv
                sc, prf = func.siScale(new_value)
                self.setText(self.format_string.format(sc * new_value, prf))
            else:
                self.setText(self.format_string.format(new_value))
            return
        # If you made it this far, just turn whatever the heck the value
        # is into a string and display it.
        self.setText(str(new_value))
示例#6
0
def test_parse_value_for_display_precision(value, precision, display_format, widget, expected):
    """
    Test the correctness of the displayed value according to the specified value precision.

    Expectations:
    The formatted presentation of the displayed value must present the numeric precision as requested, providing a
    precision consideration is applicable for the provided value, for all supported display formats.

    Parameters
    ----------
    value : int, float, hex, bin, str
        The data to be formatted
    precision : int
        The numeric precision to consider during formatting
    display_format : int
        The format type for the provided value
    widget : PyDWidget
        The widget that will display the formatted value. This object can be None
    expected : str
        The expected formatted presentation of the provided value
    """
    assert parse_value_for_display(
        value, precision, display_format_type=display_format, widget=widget) == expected
示例#7
0
def test_parse_value_for_display_format(value, precision, display_format, widget, expected):
    """
    Test the correctness of the displayed value according to the specified value type.

    Expectations:
    1. For each value provided, the display format (the string representation of the value) must be as expected
    2. All supported formats are to be tested

    Parameters
    ----------
    value : int, float, hex, bin, str
        The data to be formatted
    precision : int
        The numeric precision to consider during formatting
    display_format : int
        The format type for the provided value
    widget : PyDWidget
        The widget that will display the formatted value. This object can be None
    expected : str
        The expected formatted presentation of the provided value
    """
    parsed_value = parse_value_for_display(
        value, precision, display_format_type=display_format, widget=widget)
    assert(parsed_value == expected)
示例#8
0
    def value_changed(self, new_value):
        """
        Callback invoked when the Channel value is changed.

        Sets the value of new_value accordingly at the Label.

        Parameters
        ----------
        new_value : str, int, float, bool or np.ndarray
            The new value from the channel. The type depends on the channel.
        """
        super(PyDMLogLabel, self).value_changed(new_value)

        new_value = parse_value_for_display(
            value=new_value,
            precision=self._prec,
            display_format_type=self._display_format_type,
            string_encoding=self._string_encoding,
            widget=self)

        if self.count() > self._buffer_size:
            self.clear()

        prefix = ''
        if self._prepend_date_time:
            timestamp = self._plugin_conns[self.channel].pv.timestamp
            prefix += _Time(timestamp).strftime(self._date_time_fmt)
            prefix += ' '
        # If the value is a string, just display it as-is, no formatting
        # needed.
        item = None
        if isinstance(new_value, str):
            if self._replace:
                last_item = self.item(self.count() - 1)
                if last_item is not None:
                    last_text = last_item.text().lower()
                    for r in self._replace:
                        if r.lower() in new_value.lower() and \
                                r.lower() in last_text.lower():
                            item = last_item
                            item.setText(prefix + new_value)
                            return
            if item is None:
                item = QListWidgetItem(prefix + new_value)
            if new_value.lower().startswith(('err', 'fatal')):
                item.setForeground(self.errorcolor)
            elif new_value.lower().startswith('warn'):
                item.setForeground(self.warncolor)
        # If the value is an enum, display the appropriate enum string for
        # the value.
        elif self.enum_strings is not None and isinstance(new_value, int):
            try:
                item = QListWidgetItem(prefix + self.enum_strings[new_value])
            except IndexError:
                item = QListWidgetItem("**INVALID**")
        # If the value is a number (float or int), display it using a
        # format string if necessary.
        elif isinstance(new_value, (int, float)):
            item = QListWidgetItem(prefix +
                                   self.format_string.format(new_value))
        # If you made it this far, just turn whatever the heck the value
        # is into a string and display it.
        else:
            item = QListWidgetItem(prefix + str(new_value))

        if item is not None:
            self.addItem(item)
            self.scrollToBottom()