Skip to content

Commit

Permalink
Merge pull request #652 from mwcmwc12/IntFlag_update_for_ControlFlow_…
Browse files Browse the repository at this point in the history
…issue_#316

Int flag update for control flow issue pyvisa/pyvisa-py#316
  • Loading branch information
MatthieuDartiailh committed Jan 28, 2022
2 parents ae8c8b1 + 91aff73 commit 53a1d41
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ PyVISA Changelog
- allow trailing separators in ascii blocks PR #581
Numpy style extraction already handled this case correctly but it was not so
for the builtin parser.
- changed constant ControlFlow enum type from IntEnum to IntFlag PR#652
This change also triggers a change in attributes.py to include a new Attribute
class, FlagAttribute where the enum type is declared at IntFlag.
Class AttrVI_ATTR_ASRL_FLOW_CNTRL now inherits from FlagAttribute.
Flow control attribute per ivi foundation definition is a flag and
multiple flow control types can be set.

1.11.3 (07-11-2020)
-------------------
Expand Down
34 changes: 33 additions & 1 deletion pyvisa/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,38 @@ def pre_set(self, value: enum.IntEnum) -> Any:
return value


class FlagAttribute(Attribute):
"""Class for attributes with Flag values that map to a PyVISA Enum."""

#: Enum type with valid values.
enum_type: ClassVar[Type[enum.IntFlag]]

@classmethod
def redoc(cls) -> None:
"""Add the enum member to the docstring."""
super(FlagAttribute, cls).redoc()
cls.__doc__ += "\n:type: :class:%s.%s" % (
cls.enum_type.__module__,
cls.enum_type.__name__,
)

def post_get(self, value: Any) -> enum.IntFlag:
"""Convert the VISA value to the proper enum member."""
return self.enum_type(value)

def pre_set(self, value: enum.IntFlag) -> Any:
"""Validate the value passed against the enum."""
# Python 3.8 raise if a non-Enum is used for value
try:
value = self.enum_type(value)
except ValueError:
raise ValueError(
"%r is an invalid value for attribute %s, "
"should be a %r" % (value, self.visa_name, self.enum_type)
)
return value


class IntAttribute(Attribute):
"""Class for attributes with integers values."""

Expand Down Expand Up @@ -1612,7 +1644,7 @@ class AttrVI_ATTR_ASRL_STOP_BITS(EnumAttribute):
enum_type = constants.StopBits


class AttrVI_ATTR_ASRL_FLOW_CNTRL(EnumAttribute):
class AttrVI_ATTR_ASRL_FLOW_CNTRL(FlagAttribute):
"""Indicate the type of flow control used by the transfer mechanism."""

resources = [(constants.InterfaceType.asrl, "INSTR")]
Expand Down
2 changes: 1 addition & 1 deletion pyvisa/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ class WireMode(enum.IntEnum):


@enum.unique
class ControlFlow(enum.IntEnum):
class ControlFlow(enum.IntFlag):
"""Control flow for a serial resource."""

none = VI_ASRL_FLOW_NONE
Expand Down

0 comments on commit 53a1d41

Please sign in to comment.