Skip to content

Commit b69497f

Browse files
author
Johannes Otepka
committed
glob and regex supoort added
1 parent b9f604d commit b69497f

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

ipyparallel/controller/dictdb.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import copy
3939
from copy import deepcopy
4040
from datetime import datetime
41+
import fnmatch
42+
import re
4143

4244
from traitlets import Dict, Float, Integer, Unicode
4345
from traitlets.config.configurable import LoggingConfigurable
@@ -59,6 +61,8 @@
5961
'$all': lambda a, b: all([a in bb for bb in b]),
6062
'$mod': lambda a, b: a % b[0] == b[1],
6163
'$exists': lambda a, b: (b and a is not None) or (a is None and not b),
64+
'$glob': lambda a, b: fnmatch.fnmatch(a, b),
65+
'$regex': lambda a, b: re.match(b, a),
6266
}
6367

6468

ipyparallel/controller/sqlitedb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Distributed under the terms of the Modified BSD License.
55
import json
66
import os
7+
import re
78

89
try:
910
import cPickle as pickle
@@ -47,6 +48,8 @@
4748
# '$all': None,
4849
# '$mod': None,
4950
# '$exists' : None
51+
'$glob': "GLOB",
52+
'$regex': "REGEXP",
5053
}
5154
null_operators = {
5255
'=': "IS NULL",
@@ -97,6 +100,10 @@ def _convert_timestamp(s):
97100
"""Adapt text timestamp to datetime"""
98101
return ensure_timezone(dateutil_parse(s))
99102

103+
def _regexp(expr, item):
104+
"""sqlite callback function for performing a regex operation"""
105+
reg = re.compile(expr)
106+
return reg.match(item) is not None
100107

101108
# -----------------------------------------------------------------------------
102109
# SQLiteDB class
@@ -271,6 +278,7 @@ def _init_db(self):
271278
# isolation_level = None)#,
272279
cached_statements=64,
273280
)
281+
self._db.create_function("REGEXP", 2, _regexp)
274282
# print dir(self._db)
275283
first_table = previous_table = self.table
276284
i = 0

ipyparallel/tests/test_db.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import time
99
from datetime import datetime, timedelta
1010
from unittest import TestCase
11+
import fnmatch
12+
import re
1113

1214
import pytest
1315
from jupyter_client.session import Session
@@ -131,6 +133,40 @@ def test_find_records_in(self):
131133
found = [r['msg_id'] for r in recs]
132134
assert set(odd) == set(found)
133135

136+
# disabled for now. jo, 5.3.26
137+
# def test_find_records_glob(self):
138+
# """test finding records with '$glob' operators"""
139+
# hist = self.db.get_history()
140+
#
141+
# pattern = "*"+hist[0][5:-2]+"_?"
142+
# ref = [msg_id for msg_id in hist if fnmatch.fnmatch(msg_id, pattern)]
143+
# recs = self.db.find_records({'msg_id': {'$glob': pattern}}, ["msg_id"])
144+
# found = [r['msg_id'] for r in recs]
145+
# assert set(ref) == set(found)
146+
#
147+
# pattern = "*_1?"
148+
# ref = [msg_id for msg_id in hist if fnmatch.fnmatch(msg_id, pattern)]
149+
# recs = self.db.find_records({'msg_id': {'$glob': pattern}},["msg_id"])
150+
# found = [r['msg_id'] for r in recs]
151+
# assert set(ref) == set(found)
152+
153+
def test_find_records_regex(self):
154+
"""test finding records with '$regex' operators"""
155+
hist = self.db.get_history()
156+
157+
pattern = "^.+_.$"
158+
ref = [msg_id for msg_id in hist if re.match(pattern,msg_id)]
159+
recs = self.db.find_records({'msg_id': {'$regex': pattern}}, ["msg_id"])
160+
found = [r['msg_id'] for r in recs]
161+
assert set(ref) == set(found)
162+
163+
pattern = "^.+_1[0-9]$"
164+
ref = [msg_id for msg_id in hist if re.match(pattern,msg_id)]
165+
recs = self.db.find_records({'msg_id': {'$regex': pattern}}, ["msg_id"])
166+
found = [r['msg_id'] for r in recs]
167+
assert set(ref) == set(found)
168+
169+
134170
def test_get_history(self):
135171
msg_ids = self.db.get_history()
136172
latest = datetime(1984, 1, 1).replace(tzinfo=utc)

0 commit comments

Comments
 (0)