nimi-python icon indicating copy to clipboard operation
nimi-python copied to clipboard

Documentation fore "return type" of nidigital fetch_capture_waveform method is unclear

Open gfisher-NI opened this issue 1 year ago • 4 comments

In the documentation for the fetch_capture_waveform method of nidigital, "Return type" is listed as "{ int: memoryview of array.array of unsigned int, int: memoryview of array.array of unsigned int, … }".

Image

This is extremely confusing and gives almost no indication of how to interpret the return type, other than it being a key-value pair.

gfisher-NI avatar Feb 17 '25 18:02 gfisher-NI

As far as I can tell, the only way to convert the return type into something usable is to index it and then convert it to bytes:

capture_samples = digital_session.fetch_capture_waveform('capture_waveform',1)
print(bytes(capture_samples[0]))

gfisher-NI avatar Feb 17 '25 18:02 gfisher-NI

The text above says

Returns dictionary where each key is a site number and value is a collection of digital states representing capture waveform data

I think that means the return type should be something like dict[int, memoryview[int]] or dict[int, collections.abc.Buffer[int]]. I assume the fact that the memoryview references an array is a hidden implementation detail.

Related: Moving the type documentation out of the docstring and into a type hint would enable validating it at build time. https://github.com/ni/nimi-python/issues/1887

bkeryan avatar Feb 17 '25 19:02 bkeryan

@bkeryan The fact that it's an array is one thing that isn't clear, but also, if I just index into the array I get something that looks like:

<memory at 0x000001B8F00A5700>

So there needs to be something about needing to perform some sort of conversion or interpretation.

gfisher-NI avatar Feb 17 '25 20:02 gfisher-NI

@gfisher-NI Are you indexing a single element or a slice? It seems that slicing a memoryview returns another memoryview that refers to the sliced data: https://docs.python.org/3/library/stdtypes.html#memoryview

bkeryan avatar Feb 17 '25 20:02 bkeryan