Skip to content
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

Support chained repeated capabilities #1291

Merged
merged 20 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ All notable changes to this project will be documented in this file.
* Zip file per driver for all examples and any helper files
* Link to zip file on examples documentation
* Support for Python 3.8
* Support for nested repeated capabilities. This allows things like
``` python
session.sites[0, 1].pins['A', 'B'].ppmu_voltage_level = 4
```

The repeated capabilities will be expanded to `'site0/A,site0/B,site1/A,site1/B'`
* #### Changed
* `import_attribute_configuration_buffer()` now accepts `list` of numbers that are integers less than 255, `array.array('b')`, `bytes`, `bytearray` for configuration buffer - [#1013](https://github.com/ni/nimi-python/issues/1013)
* `export_attribute_configuration_buffer()` now returns `bytes` as the buffer type - [#1013](https://github.com/ni/nimi-python/issues/1013)
Expand Down
19 changes: 12 additions & 7 deletions build/templates/session.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,20 @@ class _Lock(object):
% endif
% if len(config['repeated_capabilities']) > 0:
class _RepeatedCapabilities(object):
def __init__(self, session, prefix):
def __init__(self, session, prefix, current_repeated_capability_list):
self._session = session
self._prefix = prefix
# We need at least one element. If we get an empty list, make the one element an empty string
self._current_repeated_capability_list = current_repeated_capability_list if len(current_repeated_capability_list) > 0 else ['']
# Now we know there is at lease one entry, so we look if it is an empty string or not
self._seperator = '/' if len(self._current_repeated_capability_list[0]) > 0 else ''

def __getitem__(self, repeated_capability):
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
complete_rep_cap_list = [current_rep_cap + self._seperator + rep_cap for current_rep_cap in self._current_repeated_capability_list for rep_cap in rep_caps_list]

return _SessionBase(${config['session_handle_parameter_name']}=self._session._${config['session_handle_parameter_name']}, repeated_capability_list=rep_caps_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)
return _SessionBase(${config['session_handle_parameter_name']}=self._session._${config['session_handle_parameter_name']}, repeated_capability_list=complete_rep_cap_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)


# This is a very simple context manager we can use when we need to set/get attributes
Expand Down Expand Up @@ -196,6 +201,11 @@ constructor_params = helper.filter_parameters(init_function, helper.ParameterUsa
param_list.append("encoding=" + pp.pformat(encoding))
self._param_list = ', '.join(param_list)

# Instantiate any repeated capability objects
% for rep_cap in config['repeated_capabilities']:
self.${rep_cap['python_name']} = _RepeatedCapabilities(self, '${rep_cap["prefix"]}', repeated_capability_list)
% endfor

self._is_frozen = freeze_it

def __repr__(self):
Expand Down Expand Up @@ -260,11 +270,6 @@ class Session(_SessionBase):
self._${config['session_handle_parameter_name']} = 0 # This must be set before calling ${init_function['python_name']}().
self._${config['session_handle_parameter_name']} = self.${init_function['python_name']}(${init_call_params})

# Instantiate any repeated capability objects
% for rep_cap in config['repeated_capabilities']:
self.${rep_cap['python_name']} = _RepeatedCapabilities(self, '${rep_cap["prefix"]}')
% endfor

% if config['supports_nitclk']:
self.tclk = nitclk.SessionReference(self._${config['session_handle_parameter_name']})

Expand Down
15 changes: 10 additions & 5 deletions generated/nidcpower/nidcpower/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,20 @@ def __exit__(self, exc_type, exc_value, traceback):


class _RepeatedCapabilities(object):
def __init__(self, session, prefix):
def __init__(self, session, prefix, current_repeated_capability_list):
self._session = session
self._prefix = prefix
# We need at least one element. If we get an empty list, make the one element an empty string
self._current_repeated_capability_list = current_repeated_capability_list if len(current_repeated_capability_list) > 0 else ['']
# Now we know there is at lease one entry, so we look if it is an empty string or not
self._seperator = '/' if len(self._current_repeated_capability_list[0]) > 0 else ''

def __getitem__(self, repeated_capability):
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
complete_rep_cap_list = [current_rep_cap + self._seperator + rep_cap for current_rep_cap in self._current_repeated_capability_list for rep_cap in rep_caps_list]

return _SessionBase(vi=self._session._vi, repeated_capability_list=rep_caps_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)
return _SessionBase(vi=self._session._vi, repeated_capability_list=complete_rep_cap_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)


# This is a very simple context manager we can use when we need to set/get attributes
Expand Down Expand Up @@ -2689,6 +2694,9 @@ def __init__(self, repeated_capability_list, vi, library, encoding, freeze_it=Fa
param_list.append("encoding=" + pp.pformat(encoding))
self._param_list = ', '.join(param_list)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '', repeated_capability_list)

self._is_frozen = freeze_it

def __repr__(self):
Expand Down Expand Up @@ -4164,9 +4172,6 @@ def __init__(self, resource_name, channels=None, reset=False, options={}):
self._vi = 0 # This must be set before calling _initialize_with_channels().
self._vi = self._initialize_with_channels(resource_name, channels, reset, options)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '')

# Store the parameter list for later printing in __repr__
param_list = []
param_list.append("resource_name=" + pp.pformat(resource_name))
Expand Down
23 changes: 14 additions & 9 deletions generated/nidigital/nidigital/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@ def __exit__(self, exc_type, exc_value, traceback):


class _RepeatedCapabilities(object):
def __init__(self, session, prefix):
def __init__(self, session, prefix, current_repeated_capability_list):
self._session = session
self._prefix = prefix
# We need at least one element. If we get an empty list, make the one element an empty string
self._current_repeated_capability_list = current_repeated_capability_list if len(current_repeated_capability_list) > 0 else ['']
# Now we know there is at lease one entry, so we look if it is an empty string or not
self._seperator = '/' if len(self._current_repeated_capability_list[0]) > 0 else ''

def __getitem__(self, repeated_capability):
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
complete_rep_cap_list = [current_rep_cap + self._seperator + rep_cap for current_rep_cap in self._current_repeated_capability_list for rep_cap in rep_caps_list]

return _SessionBase(vi=self._session._vi, repeated_capability_list=rep_caps_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)
return _SessionBase(vi=self._session._vi, repeated_capability_list=complete_rep_cap_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)


# This is a very simple context manager we can use when we need to set/get attributes
Expand Down Expand Up @@ -606,6 +611,13 @@ def __init__(self, repeated_capability_list, vi, library, encoding, freeze_it=Fa
param_list.append("encoding=" + pp.pformat(encoding))
self._param_list = ', '.join(param_list)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '', repeated_capability_list)
self.pins = _RepeatedCapabilities(self, '', repeated_capability_list)
self.devices = _RepeatedCapabilities(self, '', repeated_capability_list)
self.pattern_opcode_events = _RepeatedCapabilities(self, 'patternOpcodeEvent', repeated_capability_list)
self.conditional_jump_triggers = _RepeatedCapabilities(self, 'conditionalJumpTrigger', repeated_capability_list)

self._is_frozen = freeze_it

def __repr__(self):
Expand Down Expand Up @@ -1632,13 +1644,6 @@ def __init__(self, resource_name, id_query=False, reset_device=False, options={}
self._vi = 0 # This must be set before calling _init_with_options().
self._vi = self._init_with_options(resource_name, id_query, reset_device, options)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '')
self.pins = _RepeatedCapabilities(self, '')
self.devices = _RepeatedCapabilities(self, '')
self.pattern_opcode_events = _RepeatedCapabilities(self, 'patternOpcodeEvent')
self.conditional_jump_triggers = _RepeatedCapabilities(self, 'conditionalJumpTrigger')

self.tclk = nitclk.SessionReference(self._vi)

# Store the parameter list for later printing in __repr__
Expand Down
4 changes: 2 additions & 2 deletions generated/nidmm/nidmm/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ def __init__(self, repeated_capability_list, vi, library, encoding, freeze_it=Fa
param_list.append("encoding=" + pp.pformat(encoding))
self._param_list = ', '.join(param_list)

# Instantiate any repeated capability objects

self._is_frozen = freeze_it

def __repr__(self):
Expand Down Expand Up @@ -1169,8 +1171,6 @@ def __init__(self, resource_name, id_query=False, reset_device=False, options={}
self._vi = 0 # This must be set before calling _init_with_options().
self._vi = self._init_with_options(resource_name, id_query, reset_device, options)

# Instantiate any repeated capability objects

# Store the parameter list for later printing in __repr__
param_list = []
param_list.append("resource_name=" + pp.pformat(resource_name))
Expand Down
16 changes: 11 additions & 5 deletions generated/nifake/nifake/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,20 @@ def __exit__(self, exc_type, exc_value, traceback):


class _RepeatedCapabilities(object):
def __init__(self, session, prefix):
def __init__(self, session, prefix, current_repeated_capability_list):
self._session = session
self._prefix = prefix
# We need at least one element. If we get an empty list, make the one element an empty string
self._current_repeated_capability_list = current_repeated_capability_list if len(current_repeated_capability_list) > 0 else ['']
# Now we know there is at lease one entry, so we look if it is an empty string or not
self._seperator = '/' if len(self._current_repeated_capability_list[0]) > 0 else ''

def __getitem__(self, repeated_capability):
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
complete_rep_cap_list = [current_rep_cap + self._seperator + rep_cap for current_rep_cap in self._current_repeated_capability_list for rep_cap in rep_caps_list]

return _SessionBase(vi=self._session._vi, repeated_capability_list=rep_caps_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)
return _SessionBase(vi=self._session._vi, repeated_capability_list=complete_rep_cap_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)


# This is a very simple context manager we can use when we need to set/get attributes
Expand Down Expand Up @@ -195,6 +200,10 @@ def __init__(self, repeated_capability_list, vi, library, encoding, freeze_it=Fa
param_list.append("encoding=" + pp.pformat(encoding))
self._param_list = ', '.join(param_list)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '', repeated_capability_list)
self.sites = _RepeatedCapabilities(self, 'site', repeated_capability_list)

self._is_frozen = freeze_it

def __repr__(self):
Expand Down Expand Up @@ -726,9 +735,6 @@ def __init__(self, resource_name, options={}, id_query=False, reset_device=False
self._vi = 0 # This must be set before calling _init_with_options().
self._vi = self._init_with_options(resource_name, options, id_query, reset_device)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '')

self.tclk = nitclk.SessionReference(self._vi)

# Store the parameter list for later printing in __repr__
Expand Down
16 changes: 16 additions & 0 deletions generated/nifake/nifake/unit_tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,22 @@ def test_repeated_capabilities_list(self):
with nifake.Session('dev1') as session:
assert session.channels['r0']._repeated_capability_list == ['r0']

def test_nested_repeated_capabilities_list(self):
with nifake.Session('dev1') as session:
assert session.sites[0, 1].channels[2, 3]._repeated_capability_list == ['site0/2', 'site0/3', 'site1/2', 'site1/3']

def test_nested_repeated_capability_method_on_specific_channel(self):
test_maximum_time_ms = 10 # milliseconds
test_maximum_time_us = 10000 # microseconds
test_maximum_time = datetime.timedelta(milliseconds=test_maximum_time_ms)
test_reading = 5
self.patched_library.niFake_ReadFromChannel.side_effect = self.side_effects_helper.niFake_ReadFromChannel
self.side_effects_helper['ReadFromChannel']['reading'] = test_reading
with nifake.Session('dev1') as session:
value = session.sites[0, 1].channels[2, 3].read_from_channel(test_maximum_time)
self.patched_library.niFake_ReadFromChannel.assert_called_once_with(_matchers.ViSessionMatcher(SESSION_NUM_FOR_TEST), _matchers.ViStringMatcher('site0/2,site0/3,site1/2,site1/3'), _matchers.ViInt32Matcher(test_maximum_time_us), _matchers.ViReal64PointerMatcher())
assert value == test_reading

# Attributes

def test_get_attribute_int32(self):
Expand Down
19 changes: 12 additions & 7 deletions generated/nifgen/nifgen/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@ def __exit__(self, exc_type, exc_value, traceback):


class _RepeatedCapabilities(object):
def __init__(self, session, prefix):
def __init__(self, session, prefix, current_repeated_capability_list):
self._session = session
self._prefix = prefix
# We need at least one element. If we get an empty list, make the one element an empty string
self._current_repeated_capability_list = current_repeated_capability_list if len(current_repeated_capability_list) > 0 else ['']
# Now we know there is at lease one entry, so we look if it is an empty string or not
self._seperator = '/' if len(self._current_repeated_capability_list[0]) > 0 else ''

def __getitem__(self, repeated_capability):
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
complete_rep_cap_list = [current_rep_cap + self._seperator + rep_cap for current_rep_cap in self._current_repeated_capability_list for rep_cap in rep_caps_list]

return _SessionBase(vi=self._session._vi, repeated_capability_list=rep_caps_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)
return _SessionBase(vi=self._session._vi, repeated_capability_list=complete_rep_cap_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)


# This is a very simple context manager we can use when we need to set/get attributes
Expand Down Expand Up @@ -948,6 +953,11 @@ def __init__(self, repeated_capability_list, vi, library, encoding, freeze_it=Fa
param_list.append("encoding=" + pp.pformat(encoding))
self._param_list = ', '.join(param_list)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '', repeated_capability_list)
self.script_triggers = _RepeatedCapabilities(self, 'ScriptTrigger', repeated_capability_list)
self.markers = _RepeatedCapabilities(self, 'Marker', repeated_capability_list)

self._is_frozen = freeze_it

def __repr__(self):
Expand Down Expand Up @@ -3135,11 +3145,6 @@ def __init__(self, resource_name, channel_name=None, reset_device=False, options
self._vi = 0 # This must be set before calling _initialize_with_channels().
self._vi = self._initialize_with_channels(resource_name, channel_name, reset_device, options)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '')
self.script_triggers = _RepeatedCapabilities(self, 'ScriptTrigger')
self.markers = _RepeatedCapabilities(self, 'Marker')

self.tclk = nitclk.SessionReference(self._vi)

# Store the parameter list for later printing in __repr__
Expand Down
15 changes: 10 additions & 5 deletions generated/niscope/niscope/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,20 @@ def __exit__(self, exc_type, exc_value, traceback):


class _RepeatedCapabilities(object):
def __init__(self, session, prefix):
def __init__(self, session, prefix, current_repeated_capability_list):
self._session = session
self._prefix = prefix
# We need at least one element. If we get an empty list, make the one element an empty string
self._current_repeated_capability_list = current_repeated_capability_list if len(current_repeated_capability_list) > 0 else ['']
# Now we know there is at lease one entry, so we look if it is an empty string or not
self._seperator = '/' if len(self._current_repeated_capability_list[0]) > 0 else ''

def __getitem__(self, repeated_capability):
'''Set/get properties or call methods with a repeated capability (i.e. channels)'''
rep_caps_list = _converters.convert_repeated_capabilities(repeated_capability, self._prefix)
complete_rep_cap_list = [current_rep_cap + self._seperator + rep_cap for current_rep_cap in self._current_repeated_capability_list for rep_cap in rep_caps_list]

return _SessionBase(vi=self._session._vi, repeated_capability_list=rep_caps_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)
return _SessionBase(vi=self._session._vi, repeated_capability_list=complete_rep_cap_list, library=self._session._library, encoding=self._session._encoding, freeze_it=True)


# This is a very simple context manager we can use when we need to set/get attributes
Expand Down Expand Up @@ -1663,6 +1668,9 @@ def __init__(self, repeated_capability_list, vi, library, encoding, freeze_it=Fa
param_list.append("encoding=" + pp.pformat(encoding))
self._param_list = ', '.join(param_list)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '', repeated_capability_list)

self._is_frozen = freeze_it

def __repr__(self):
Expand Down Expand Up @@ -4109,9 +4117,6 @@ def __init__(self, resource_name, id_query=False, reset_device=False, options={}
self._vi = 0 # This must be set before calling _init_with_options().
self._vi = self._init_with_options(resource_name, id_query, reset_device, options)

# Instantiate any repeated capability objects
self.channels = _RepeatedCapabilities(self, '')

self.tclk = nitclk.SessionReference(self._vi)

# Store the parameter list for later printing in __repr__
Expand Down
Loading