-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Prototype] Address Event Callbacks Contain Unusable Parameters Issue #472
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -760,6 +760,46 @@ def read(self, number_of_samples_per_channel=NUM_SAMPLES_UNSET, | |
|
||
return data.tolist() | ||
|
||
def add_arguments_if_necessary(self, callback_function, expected_number_of_arguments): | ||
if (callback_function.__code__.co_argcount < expected_number_of_arguments): | ||
print("Not enough arguments! Adding arguments") | ||
if ("every_n_samples_event_type" in parameters): | ||
DeborahOoi96 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [style] If-statements in Python do not have parens unless they are needed for grouping or splitting across multiple lines. Running |
||
result = self.n_samples_event_wrapper(parameters, callback_function) | ||
elif ("signal_type" in parameters): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every N and signal events pass the callback an enum, but they use a bare ctypes or int type rather than the enum classes from nidaqmx.constants. See my prototype: https://github.com/ni/nidaqmx-python/pull/474/files
|
||
result = self.register_signal_event_wrapper(parameters, callback_function) | ||
elif ("status" in parameters): | ||
result = self.register_done_event_wrapper(parameters, callback_function) | ||
else: | ||
result = callback_function | ||
return result | ||
|
||
def n_samples_event_wrapper(self, parameters, callback_function): | ||
if ("task_handle" and "callback_data" not in parameters): | ||
result = lambda task_handle, every_n_samples_event_type, number_of_samples, callback_data: callback_function(every_n_samples_event_type, number_of_samples) | ||
elif ("task_handle" not in parameters): | ||
result = lambda task_handle, every_n_samples_event_type, number_of_samples, callback_data: callback_function(every_n_samples_event_type, number_of_samples, callback_data) | ||
elif ("callback_data" not in parameters): | ||
result = lambda task_handle, every_n_samples_event_type, number_of_samples, callback_data: callback_function(task_handle, every_n_samples_event_type, number_of_samples) | ||
return result | ||
|
||
def register_signal_event_wrapper(self, parameters, callback_function): | ||
if ("task_handle" and "callback_data" not in parameters): | ||
result = lambda task_handle, signal_type, callback_data: callback_function(signal_type) | ||
elif ("task_handle" not in parameters): | ||
DeborahOoi96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = lambda task_handle, signal_type, callback_data: callback_function(signal_type, callback_data) | ||
elif ("callback_data" not in parameters): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last parameter would be useful if the user was allowed to pass in an optional Python object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an example of what I mean:
It's not critical because Python makes it easy to bind a closure or to pass in a member function as the callback, but it's a potential nice-to-have and it would avoid removing parameters from the signature. |
||
result = lambda task_handle, signal_type, callback_data: callback_function(task_handle, signal_type) | ||
return result | ||
|
||
def register_done_event_wrapper(self, parameters, callback_function): | ||
if ("task_handle" and "callback_data" not in parameters): | ||
result = lambda task_handle, status, callback_data: callback_function(status) | ||
elif ("task_handle" not in parameters): | ||
result = lambda task_handle, status, callback_data: callback_function(status, callback_data) | ||
elif ("callback_data" not in parameters): | ||
result = lambda task_handle, status, callback_data: callback_function(task_handle, status) | ||
return result | ||
|
||
def register_done_event(self, callback_method): | ||
""" | ||
Registers a callback function to receive an event when a task stops due | ||
|
@@ -788,6 +828,7 @@ def register_done_event(self, callback_method): | |
function. | ||
""" | ||
if callback_method is not None: | ||
callback_method = self.add_arguments_if_necessary(callback_method, 3) | ||
# If the event is already registered, the interpreter should raise DaqError with code | ||
# DAQmxErrors.DONE_EVENT_ALREADY_REGISTERED. | ||
event_handler = self._interpreter.register_done_event(self._handle, 0, callback_method, None) | ||
|
@@ -836,6 +877,7 @@ def register_every_n_samples_acquired_into_buffer_event( | |
function. | ||
""" | ||
if callback_method is not None: | ||
callback_method = self.add_arguments_if_necessary(callback_method, 4) | ||
# If the event is already registered, the interpreter should raise DaqError with code | ||
# DAQmxErrors.EVERY_N_SAMPS_ACQ_INTO_BUFFER_EVENT_ALREADY_REGISTERED. | ||
event_handler = self._interpreter.register_every_n_samples_event( | ||
|
@@ -887,6 +929,7 @@ def register_every_n_samples_transferred_from_buffer_event( | |
function. | ||
""" | ||
if callback_method is not None: | ||
callback_method = self.add_arguments_if_necessary(callback_method, 4) | ||
# If the event is already registered, the interpreter should raise DaqError with code | ||
# DAQmxErrors.EVERY_N_SAMPS_TRANSFERRED_FROM_BUFFER_EVENT_ALREADY_REGISTERED. | ||
event_handler = self._interpreter.register_every_n_samples_event( | ||
|
@@ -933,6 +976,7 @@ def register_signal_event(self, signal_type, callback_method): | |
function. | ||
""" | ||
if callback_method is not None: | ||
callback_method = self.add_arguments_if_necessary(callback_method, 3) | ||
# If the event is already registered, the interpreter should raise DaqError with code | ||
# DAQmxErrors.SIGNAL_EVENT_ALREADY_REGISTERED. | ||
event_handler = self._interpreter.register_signal_event( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__code__
is a CPython implementation detail and probably won't work with PyPy.Using the
inspect
module is probably more portable.