Skip to content

Commit b031f8f

Browse files
committed
DOC: DisplayFormat and parse_value_for_display
1 parent f0e9d78 commit b031f8f

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

docs/source/widgets/index.rst

+8
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,11 @@ Drawing Widgets
6161
:maxdepth: 1
6262

6363
drawing.rst
64+
65+
Utilities
66+
---------
67+
68+
.. toctree::
69+
:maxdepth: 1
70+
71+
utilities.rst

docs/source/widgets/utilities.rst

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
########################
2+
Display Format Utilities
3+
########################
4+
5+
.. currentmodule:: pydm.widgets.display_format
6+
7+
.. autoclass:: DisplayFormat
8+
:members:
9+
10+
.. autofunction:: parse_value_for_display

pydm/widgets/display_format.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
import numpy as np
3+
from typing import Any
34

45
import logging
56
import warnings
@@ -8,15 +9,49 @@
89

910

1011
class DisplayFormat(object):
12+
"""Display format for showing data in a PyDM widget."""
13+
#: The default display format.
1114
Default = 0
15+
#: Show the data as a string.
1216
String = 1
17+
#: Show numerical values as floating point (base 10, decimal) values.
1318
Decimal = 2
19+
#: Show numerical values in scientific / exponential notation.
1420
Exponential = 3
21+
#: Show numerical values in base 16 (hexadecimal) notation.
1522
Hex = 4
23+
#: Show numerical values in base 2 (binary) notation.
1624
Binary = 5
1725

1826

19-
def parse_value_for_display(value, precision, display_format_type=DisplayFormat.Default, string_encoding="utf_8", widget=None):
27+
def parse_value_for_display(
28+
value: Any,
29+
precision: int,
30+
display_format_type: int = DisplayFormat.Default,
31+
string_encoding: str = "utf_8",
32+
widget=None,
33+
):
34+
"""
35+
Format a value to show it in a widget, based on the display format type.
36+
37+
Parameters
38+
----------
39+
value : Any
40+
The value to convert to a string.
41+
precision : int
42+
Precision of floating point values to use.
43+
display_format_type : int, optional
44+
Display format type to use.
45+
string_encoding : str, optional
46+
Encoding to use for strings.
47+
widget : QtWidgets.QWidget, optional
48+
Widget to get a name from for conversion errors.
49+
50+
Returns
51+
-------
52+
str
53+
Formatted version of ``value``.
54+
"""
2055
if value is None:
2156
return ""
2257
try:
@@ -37,7 +72,7 @@ def parse_value_for_display(value, precision, display_format_type=DisplayFormat.
3772
if zeros.size > 0:
3873
value = value[:zeros[0]]
3974
r = value.tobytes().decode(string_encoding)
40-
except:
75+
except Exception:
4176
logger.error("Could not decode {0} using {1} at widget named '{2}'.".format(
4277
value, string_encoding, widget_name))
4378
return value

0 commit comments

Comments
 (0)