Skip to content

Commit

Permalink
Implemented filter in rname
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Jul 23, 2015
1 parent 22a9228 commit 512ecb7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pyvisa/rname.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from __future__ import division, unicode_literals, print_function, absolute_import

import re
from collections import namedtuple, defaultdict

from pyvisa import constants
Expand Down Expand Up @@ -407,3 +408,20 @@ def to_canonical_name(resource_name):


parse_resource_name = ResourceName.from_string


def filter(resources, query):
"""Filter a list of resources according to a query expression.
The search criteria specified in the query parameter has two parts:
1. a VISA regular expression over a resource string.
2. optional logical expression over attribute values (not implemented).
:param resources: iterable of resources.
:param query: query expression.
"""

query = query.replace('?*', '.*')
matcher = re.compile(query, re.IGNORECASE)

return tuple(res for res in resources if matcher.match(res))
15 changes: 15 additions & 0 deletions pyvisa/testsuite/test_rname.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,18 @@ def test_usb_raw(self):
board='2',
usb_interface_number='3',
canonical_resource_name='USB2::0x1234::125::A22-5::3::RAW')

def test_filter(self):
run_list = (
'GPIB0::8::65535::INSTR',
'TCPIP0::localhost:1111::inst0::INSTR',
'ASRL1::INSTR',
'USB0::0x1111::0x2222::0x4445::0::RAW',
'USB0::0x1111::0x2222::0x1234::0::INSTR',
'TCPIP0::localhost::10001::SOCKET',
)
self.assertEqual(rname.filter(run_list, '?*::INSTR'),
('GPIB0::8::65535::INSTR',
'TCPIP0::localhost:1111::inst0::INSTR',
'ASRL1::INSTR',
'USB0::0x1111::0x2222::0x1234::0::INSTR'))

0 comments on commit 512ecb7

Please sign in to comment.