Skip to content

Commit

Permalink
Merge #536
Browse files Browse the repository at this point in the history
536: pyvisa.utils: add support for "s" and "p" format r=MatthieuDartiailh a=MatthieuDartiailh

- [x] Closes #532 
- [x] Executed ``black . && isort -c . && flake8`` with no errors
- [x] The change is fully covered by automated unit tests
- [ ] Documented in docs/ as appropriate
- [ ] Added an entry to the CHANGES file


Co-authored-by: MatthieuDartiailh <marul@laposte.net>
  • Loading branch information
bors[bot] and MatthieuDartiailh committed Aug 21, 2020
2 parents 461275b + 6ad1b27 commit a61f6b7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ jobs:
- name: Install graphviz
uses: kamiazya/setup-graphviz@v1
- name: Install doc building tools
# Skip 3.2 since there is weird issue with " in Parameters
run: |
pip install sphinx!=3.2 sphinx_rtd_theme
pip install sphinx sphinx_rtd_theme
- name: Build documentation
run: |
mkdir docs_output;
Expand Down
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ to be removed in 1.10:
the ascii or binary variant of the function (`read_xxxx_values`, `write_xxxx_values`,
`query_xxxx_values`)

- Add support for "s" and "p" binary format which can be used to retrieve binary files
from instruments. PR #536
- Convert the testsuite to use pytest PR #531
This is motivated by the possibility to re-use it in PyVISA-py. However since
unittest use inheritance to detect tests, doing so would have been clunky, pytest
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ steps:
echo Install codecov
pip install codecov
echo Run codecov
codecov --file coverage.xml --token $(CODECOV_TOKEN) --env PYVISA_KEYSIGHT_VIRTUAL_INSTR --tries 5 --required -F unittest --name codecov-umbrella
codecov --file coverage.xml --token $(CODECOV_TOKEN) --env PYVISA_KEYSIGHT_VIRTUAL_INSTR --tries 5 --required -F unittests --name codecov-umbrella
displayName: 'Upload test coverage results'

- script: |
Expand Down
5 changes: 5 additions & 0 deletions docs/source/introduction/rvalues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ many bytes to expect. For those case, you can pass the expected number of
points using the ``data_points`` keyword argument. The number of bytes will be
inferred from the datatype of the block.

Finally if you are reading a file for example and simply want to extract a bytes
object, you can use the ``"s"`` datatype and pass ``bytes`` as container.


Writing ASCII values
--------------------
Expand Down Expand Up @@ -159,6 +162,8 @@ Again you can specify the ``datatype`` and ``endianness``.

>>> inst.write_binary_values('WLISt:WAVeform:DATA somename,', values, datatype='d', is_big_endian=False)

If your data are already in a ``bytes`` object you can use the ``"s"`` format.


When things are not what they should be
---------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions pyvisa/testsuite/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@ def test_noninteger_binary_block(self):
fblock = lambda block, cont: fb(block, fmt, endi, cont)
self.round_trip_block_conversion(values, tblock, fblock, msg)

def test_bytes_binary_block(self):
values = b"dbslbw cj saj \x00\x76"
for block, tb, fb in zip(
("ieee", "hp"),
(util.to_ieee_block, util.to_hp_block),
(util.from_ieee_block, util.from_hp_block),
):
for fmt in "sp":
block = tb(values, datatype="s")
print(block)
rt = fb(block, datatype="s", container=bytes)
assert values == rt

def test_malformed_binary_block_header(self):
values = list(range(10))
for header, tb, fb in zip(
Expand Down
16 changes: 13 additions & 3 deletions pyvisa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,15 @@ def from_binary_block(
fullfmt = "%s%d%s" % (endianess, array_length, datatype)

try:
return container(struct.unpack_from(fullfmt, block, offset))
raw_data = struct.unpack_from(fullfmt, block, offset)
except struct.error:
raise ValueError("Binary data was malformed")

if datatype in "sp":
raw_data = raw_data[0]

return container(raw_data)


def to_binary_block(
iterable: Sequence[Union[int, float]],
Expand Down Expand Up @@ -723,14 +728,19 @@ def to_binary_block(
"""
array_length = len(iterable)

endianess = ">" if is_big_endian else "<"
fullfmt = "%s%d%s" % (endianess, array_length, datatype)

if isinstance(header, str):
header = bytes(header, "ascii")

return header + struct.pack(fullfmt, *iterable)
if datatype in ("s", "p"):
block = struct.pack(fullfmt, iterable)

else:
block = struct.pack(fullfmt, *iterable)

return header + block


def to_ieee_block(
Expand Down

0 comments on commit a61f6b7

Please sign in to comment.