ni / nidaqmx-python

A Python API for interacting with NI-DAQmx
Other
447 stars 161 forks source link

Missing usage of slots in classes with DAQmx attributes #656

Open zhindes opened 2 weeks ago

zhindes commented 2 weeks ago

A few classes that contain DAQmx attributes are missing slots declarations. This can cause confusion if a user typo's one of our attribute names and then nothing useful happens.

For example, this code doesn't do what you expect, because the attribute is called samp_quant_samp_per_chan even though the cfg_implicit_timing function variable name is samps_per_chan for the same setting. Fun!

with nidaqmx.Task() as co_task:
    co_task.co_channels.add_co_pulse_chan_freq("Dev1/ctr0", freq=10000, duty_cycle=0.5)
    co_task.co_channels.all.co_pulse_term = ""
    co_task.timing.cfg_implicit_timing(sample_mode=nidaqmx.constants.AcquisitionType.FINITE, samps_per_chan=256)

    for i in range(0, 100):
        number_of_samples = random.randint(2, 256)

        # Update the CO task
        co_task.timing.samps_per_chan = number_of_samples # <-- THIS IS DOING NOTHING!
        print(f"Number of samples: {co_task.timing.samps_per_chan}")
        co_task.start()
        co_task.wait_until_done()
        co_task.stop()

Templates that have attributes that don't have slots (bad!):

There are some iffy ones... e.g., collections don't have properties, only functions. But maybe they should have slots not to confuse people? idk. https://github.com/ni/nidaqmx-python/blob/master/src/codegen/templates/task/collections/_ai_channel_collection.py.mako. I think starting with that list above is good enough for now.

Note: this was create in response to an internal issue: Bug 2924340: Setting non-existent attribute on the CO timing does not cause assertion in python

bkeryan commented 2 weeks ago

FYI, https://stackoverflow.com/questions/472000/usage-of-slots has some interesting details: