Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_value_for_display(value, precision, display_format_type=DisplayFormat.Default, string_encoding="utf_8", widget=None):
if value is None:
return ""
try:
widget_name = widget.objectName()
except(AttributeError, TypeError):
widget_name = ""
if display_format_type == DisplayFormat.Default:
return value
elif display_format_type == DisplayFormat.String:
if isinstance(value, np.ndarray):
try:
# Stop at the first zero (EPICS convention)
# Assume the ndarray is one-dimensional
with warnings.catch_warnings():
warnings.simplefilter("ignore")
on the current settings for scale value, precision, a user-defined
format, and the current units. If the user is currently entering a
value in the PyDMLineEdit the text will not be changed.
"""
if self.value is None:
return
if self.hasFocus():
return
new_value = self.value
if self._display_format_type in [DisplayFormat.Default,
DisplayFormat.Decimal,
DisplayFormat.Exponential,
DisplayFormat.Hex,
DisplayFormat.Binary]:
if not isinstance(new_value, (str, np.ndarray)):
try:
new_value *= self.channeltype(self._scale)
except TypeError:
logger.error("Cannot convert the value '{0}', for channel '{1}', to type '{2}'. ".format(
self._scale, self._channel, self.channeltype))
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)
self._display = str(new_value)
if self._display_format_type == DisplayFormat.Default:
def get_display_format(value):
"""Get the display format enum value from the variety metadata value."""
if value is not None:
return getattr(DisplayFormat, value.capitalize(),
DisplayFormat.Default)
widget_cls = TyphosComboBox
else:
# Otherwise a LineEdit will suffice
widget_cls = TyphosLineEdit
elif dimensions == 1:
# Waveform
widget_cls = WaveformDialogButton
elif dimensions == 2:
# B/W image
widget_cls = ImageDialogButton
else:
raise ValueError(f"Unable to create widget for widget of "
f"shape {len(desc.get('shape'))} from {signal.name}")
if dtype == 'string' and widget_cls in (TyphosLabel, TyphosLineEdit):
kwargs['display_format'] = DisplayFormat.String
return widget_cls, kwargs
The original value given by the PV is converted to a text entry based
on the current settings for scale value, precision, a user-defined
format, and the current units. If the user is currently entering a
value in the PyDMLineEdit the text will not be changed.
"""
if self.value is None:
return
if self.hasFocus():
return
new_value = self.value
if self._display_format_type in [DisplayFormat.Default,
DisplayFormat.Decimal,
DisplayFormat.Exponential,
DisplayFormat.Hex,
DisplayFormat.Binary]:
if not isinstance(new_value, (str, np.ndarray)):
try:
new_value *= self.channeltype(self._scale)
except TypeError:
logger.error("Cannot convert the value '{0}', for channel '{1}', to type '{2}'. ".format(
self._scale, self._channel, self.channeltype))
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)
self._display = str(new_value)
# Stop at the first zero (EPICS convention)
# Assume the ndarray is one-dimensional
with warnings.catch_warnings():
warnings.simplefilter("ignore")
zeros = np.where(value == 0)[0]
if zeros.size > 0:
value = value[:zeros[0]]
r = value.tobytes().decode(string_encoding)
except:
logger.error("Could not decode {0} using {1} at widget named '{2}'.".format(
value, string_encoding, widget_name))
return value
return r
else:
return value
elif display_format_type == DisplayFormat.Decimal:
# This case is taken care by the current string formatting
# routine
return value
elif display_format_type == DisplayFormat.Exponential:
fmt_string = "{" + ":.{}e".format(precision) + "}"
try:
r = fmt_string.format(value)
except (ValueError, TypeError):
logger.error("Could not display value '{0}' using displayFormat 'Exponential' at widget named "
"'{1}'.".format(value, widget_name))
r = value
return r
elif display_format_type == DisplayFormat.Hex:
try:
r = hex(int(math.floor(value)))
except (ValueError, TypeError):
def parse_value_for_display(value, precision, display_format_type=DisplayFormat.Default, string_encoding="utf_8", widget=None):
if value is None:
return ""
try:
widget_name = widget.objectName()
except(AttributeError, TypeError):
widget_name = ""
if display_format_type == DisplayFormat.Default:
return value
elif display_format_type == DisplayFormat.String:
if isinstance(value, np.ndarray):
try:
# Stop at the first zero (EPICS convention)
# Assume the ndarray is one-dimensional
with warnings.catch_warnings():
warnings.simplefilter("ignore")
zeros = np.where(value == 0)[0]
if zeros.size > 0:
value = value[:zeros[0]]
r = value.tobytes().decode(string_encoding)
except:
logger.error("Could not decode {0} using {1} at widget named '{2}'.".format(
value, string_encoding, widget_name))
return value
return r
else:
else:
return value
elif display_format_type == DisplayFormat.Decimal:
# This case is taken care by the current string formatting
# routine
return value
elif display_format_type == DisplayFormat.Exponential:
fmt_string = "{" + ":.{}e".format(precision) + "}"
try:
r = fmt_string.format(value)
except (ValueError, TypeError):
logger.error("Could not display value '{0}' using displayFormat 'Exponential' at widget named "
"'{1}'.".format(value, widget_name))
r = value
return r
elif display_format_type == DisplayFormat.Hex:
try:
r = hex(int(math.floor(value)))
except (ValueError, TypeError):
logger.error("Could not display value '{0}' using displayFormat 'Hex' at widget named "
"'{1}'.".format(value, widget_name))
r = value
return r
elif display_format_type == DisplayFormat.Binary:
try:
r = bin(int(math.floor(value)))
except (ValueError, TypeError):
logger.error("Could not display value '{0}' using displayFormat 'Binary' at widget named "
"'{1}'.".format(value, widget_name))
r = value
return r
The original value given by the PV is converted to a text entry based
on the current settings for scale value, precision, a user-defined
format, and the current units. If the user is currently entering a
value in the PyDMLineEdit the text will not be changed.
"""
if self.value is None:
return
if self.hasFocus():
return
new_value = self.value
if self._display_format_type in [DisplayFormat.Default,
DisplayFormat.Decimal,
DisplayFormat.Exponential,
DisplayFormat.Hex,
DisplayFormat.Binary]:
if not isinstance(new_value, (str, np.ndarray)):
try:
new_value *= self.channeltype(self._scale)
except TypeError:
logger.error("Cannot convert the value '{0}', for channel '{1}', to type '{2}'. ".format(
self._scale, self._channel, self.channeltype))
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)
self._display = str(new_value)
@Property(DisplayFormat)
def displayFormat(self):
return self._display_format_type