Skip to content

Commit 856e76f

Browse files
authored
fix #703 : Made pattern argument of Session.filter working in the same way as pattern argument of Group.matching
1 parent 5a76a7d commit 856e76f

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ Backward incompatible changes
7373
>>> arr3.equals(arr4, check_axes=True)
7474
False
7575

76+
* modified the behavior of the ``pattern`` argument of :py:obj:`Session.filter()` to work as the ``pattern``
77+
argument of :py:obj:`Group.matching()`:
78+
79+
>>> axis = Axis('a=a0..a2')
80+
>>> group = axis['a0,a1'] >> 'a01'
81+
>>> test1, zero1 = ndtest((2, 2)), zeros((3, 2))
82+
>>> s = Session([('test1', test1), ('zero1', zero1), ('axis', axis), ('group', group)])
83+
84+
>>> # get all items with names ending with '1'
85+
>>> s.filter(pattern='*1').names
86+
['test1', 'zero1']
87+
88+
>>> # get all items with names starting with letter in range a-k
89+
>>> s.filter(pattern='[a-k]*').names
90+
['axis', 'group']
91+
92+
Warning: to retrieve the previous behavior, add the character ``*`` to your pattern
93+
(e.g. ``s.filter('test')`` becomes ``s.filter('test*')``).
94+
95+
Closes :issue:`703`.
96+
7697

7798
New features
7899
------------
@@ -194,7 +215,7 @@ Miscellaneous improvements
194215
Fixes
195216
-----
196217

197-
* fixed LArray.divnot0 being slow when the divisor has many axes and many zeros (closes :issue:`705`).
218+
* fixed :py:obj:`LArray.divnot0()` being slow when the divisor has many axes and many zeros (closes :issue:`705`).
198219

199220
* fixed maximum length of sheet names (31 characters instead of 30 characters) when adding a new sheet
200221
to an Excel Workbook (closes :issue:`713`).

larray/core/session.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import os
55
import sys
6+
import re
7+
import fnmatch
68
import warnings
79
from collections import OrderedDict
810

@@ -17,10 +19,6 @@
1719
from larray.inout.session import ext_default_engine, get_file_handler
1820

1921

20-
def check_pattern(k, pattern):
21-
return k.startswith(pattern)
22-
23-
2422
# XXX: inherit from OrderedDict or LArray?
2523
class Session(object):
2624
"""
@@ -646,6 +644,11 @@ def filter(self, pattern=None, kind=None):
646644
----------
647645
pattern : str, optional
648646
Only keep arrays whose key match `pattern`.
647+
* `?` matches any single character
648+
* `*` matches any number of characters
649+
* [seq] matches any character in seq
650+
* [!seq] matches any character not in seq
651+
649652
kind : (tuple of) type, optional
650653
Only keep objects which are instances of type(s) `kind`.
651654
@@ -663,8 +666,13 @@ def filter(self, pattern=None, kind=None):
663666
664667
Filter using a pattern argument
665668
666-
>>> s.filter(pattern='test').names
667-
['test1']
669+
>>> # get all items with names ending with '1'
670+
>>> s.filter(pattern='*1').names
671+
['test1', 'zero1']
672+
673+
>>> # get all items with names starting with letter in range a-k
674+
>>> s.filter(pattern='[a-k]*').names
675+
['axis', 'group']
668676
669677
Filter using kind argument
670678
@@ -675,7 +683,9 @@ def filter(self, pattern=None, kind=None):
675683
"""
676684
items = self._objects.items()
677685
if pattern is not None:
678-
items = [(k, v) for k, v in items if check_pattern(k, pattern)]
686+
regex = fnmatch.translate(pattern)
687+
match = re.compile(regex).match
688+
items = [(k, v) for k, v in items if match(k)]
679689
if kind is not None:
680690
items = [(k, v) for k, v in items if isinstance(v, kind)]
681691
return Session(items)

larray/tests/test_session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ def test_iter(session):
138138
def test_filter(session):
139139
session.ax = 'ax'
140140
assertObjListEqual(session.filter(), [b, b12, a, a01, 'c', {}, e, g, f, 'ax'])
141-
assertObjListEqual(session.filter('a'), [a, a01, 'ax'])
142-
assert list(session.filter('a', dict)) == []
143-
assert list(session.filter('a', str)) == ['ax']
144-
assert list(session.filter('a', Axis)) == [a]
141+
assertObjListEqual(session.filter('a*'), [a, a01, 'ax'])
142+
assert list(session.filter('a*', dict)) == []
143+
assert list(session.filter('a*', str)) == ['ax']
144+
assert list(session.filter('a*', Axis)) == [a]
145145
assert list(session.filter(kind=Axis)) == [b, a]
146146
assert list(session.filter('a01', Group)) == [a01]
147147
assert list(session.filter(kind=Group)) == [b12, a01]

0 commit comments

Comments
 (0)