Skip to content

Commit

Permalink
Merge #415
Browse files Browse the repository at this point in the history
415: ResourceManager.list_opened_resources r=MatthieuDartiailh a=MatthieuDartiailh

Based on @ruggiamp suggestion in #414, add a `list_opened_resources` method to the resource manager returning a snapshot of the opened resources. The documentation has been updated to add an example of its usage.

Also in the Resource class we use consistently the resource name stored in the private attribute (`_resource_name`) to avoid relying on the VisaLibrary object to get it. This should fix issues such as pyvisa/pyvisa-py#151.

@ruggiamp please test if you can (I already tested locally).

@hgrecco @tivek please review if you can.

Co-authored-by: MatthieuDartiailh <marul@laposte.net>
Co-authored-by: Matthieu Dartiailh <marul@laposte.net>
  • Loading branch information
bors[bot] and MatthieuDartiailh committed Jul 22, 2019
2 parents 24e17eb + ce7d4fa commit 54ee914
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ PyVISA Changelog
1.10 (unreleased)
-----------------

- add a list_opened_resources method to the ResourceManager PR #415
- use privately stored resource name in Resource class rather than relying on
VisaLibrary PR #415
- keep track of resources created by the ResourceManager to properly close them PR #357
- replace time.clock by time.perf_counter under Python 3 PR #441
- make the ordering of the visa library deterministic PR #399
- properly close pipes when looking for a shared libary path on linux #380
Expand Down
7 changes: 7 additions & 0 deletions docs/source/introduction/communication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ is the same as::
>>> print(my_instrument.read())


.. note::

You can access all the opened resources by calling
``rm.list_opened_resources()``. This will return a list of ``Resource``,
however note that this list is not dynamically updated.


Getting the instrument configuration right
------------------------------------------

Expand Down
8 changes: 8 additions & 0 deletions pyvisa/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,14 @@ def list_resources_info(self, query='?*::INSTR'):
return dict((resource, self.resource_info(resource))
for resource in self.list_resources(query))

def list_opened_resources(self):
"""Returns a list of all the opened resources.
:return: List of resources
:rtype: list[:class:`pyvisa.resources.resource.Resource`]
"""
return list(self._created_resources)

def resource_info(self, resource_name, extended=True):
"""Get the (extended) information of a particular resource.
Expand Down
13 changes: 9 additions & 4 deletions pyvisa/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def _internal(python_class):
def __init__(self, resource_manager, resource_name):
self._resource_manager = resource_manager
self.visalib = self._resource_manager.visalib

# We store the resource name and use preferably the private attr over
# the public descriptor intyernally because the public descriptor
# requires a live instance the VISA library, which means it is much
# slower but also can cause issue in error reporting in th repr.
self._resource_name = resource_name

self._logging_extra = {'library_path': self.visalib.library_path,
Expand Down Expand Up @@ -108,10 +113,10 @@ def __del__(self):
self.close()

def __str__(self):
return "%s at %s" % (self.__class__.__name__, self.resource_name)
return "%s at %s" % (self.__class__.__name__, self._resource_name)

def __repr__(self):
return "<%r(%r)>" % (self.__class__.__name__, self.resource_name)
return "<%r(%r)>" % (self.__class__.__name__, self._resource_name)

def __enter__(self):
return self
Expand Down Expand Up @@ -180,14 +185,14 @@ def resource_info(self):
:rtype: :class:`pyvisa.highlevel.ResourceInfo`
"""
return self.visalib.parse_resource_extended(self._resource_manager.session, self.resource_name)
return self.visalib.parse_resource_extended(self._resource_manager.session, self._resource_name)

@property
def interface_type(self):
"""The interface type of the resource as a number.
"""
return self.visalib.parse_resource(self._resource_manager.session,
self.resource_name)[0].interface_type
self._resource_name)[0].interface_type

def ignore_warning(self, *warnings_constants):
"""Ignoring warnings context manager for the current resource.
Expand Down

0 comments on commit 54ee914

Please sign in to comment.