Skip to content

Commit cd46e2a

Browse files
committed
Move LazyReCompile to bpython.lazyre
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent 1e8c893 commit cd46e2a

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

bpython/autocomplete.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from bpython import importcompletion
3535
from bpython import line as lineparts
3636
from bpython._py3compat import py3
37+
from bpython.lazyre import LazyReCompile
3738

3839

3940
# Autocomplete modes
@@ -460,7 +461,7 @@ def safe_eval(expr, namespace):
460461
raise EvaluationError
461462

462463

463-
attr_matches_re = lineparts.LazyReCompile(r"(\w+(\.\w+)*)\.(\w*)")
464+
attr_matches_re = LazyReCompile(r"(\w+(\.\w+)*)\.(\w*)")
464465

465466

466467
def attr_matches(text, namespace):

bpython/curtsiesfrontend/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from bpython.line import LazyReCompile
2+
from bpython.lazyre import LazyReCompile
33
from bpython.formatter import BPythonFormatter
44
from bpython._py3compat import PythonLexer
55
from bpython.config import Struct, loadini, default_config_path

bpython/lazyre.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# The MIT License
2+
#
3+
# Copyright (c) 2015 the bpython authors.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
23+
import re
24+
25+
26+
class LazyReCompile(object):
27+
28+
def __init__(self, regex, flags=0):
29+
self.regex = regex
30+
self.flags = flags
31+
self.compiled = None
32+
33+
def compile_regex(method):
34+
def _impl(self, *args, **kwargs):
35+
if self.compiled is None:
36+
self.compiled = re.compile(self.regex, self.flags)
37+
return method(self, *args, **kwargs)
38+
return _impl
39+
40+
@compile_regex
41+
def finditer(self, *args, **kwargs):
42+
return self.compiled.finditer(*args, **kwargs)
43+
44+
@compile_regex
45+
def search(self, *args, **kwargs):
46+
return self.compiled.search(*args, **kwargs)
47+
48+
@compile_regex
49+
def match(self, *args, **kwargs):
50+
return self.compiled.match(*args, **kwargs)

bpython/line.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,8 @@
44
Python code, and return None, or a tuple of the start index, end index, and the
55
word."""
66

7-
import re
87
from itertools import chain
9-
10-
11-
class LazyReCompile(object):
12-
13-
def __init__(self, regex, flags=0):
14-
self.regex = regex
15-
self.flags = flags
16-
self.compiled = None
17-
18-
def compile_regex(method):
19-
def _impl(self, *args, **kwargs):
20-
if self.compiled is None:
21-
self.compiled = re.compile(self.regex, self.flags)
22-
return method(self, *args, **kwargs)
23-
return _impl
24-
25-
@compile_regex
26-
def finditer(self, *args, **kwargs):
27-
return self.compiled.finditer(*args, **kwargs)
28-
29-
@compile_regex
30-
def search(self, *args, **kwargs):
31-
return self.compiled.search(*args, **kwargs)
32-
33-
@compile_regex
34-
def match(self, *args, **kwargs):
35-
return self.compiled.match(*args, **kwargs)
8+
from bpython.lazyre import LazyReCompile
369

3710

3811
current_word_re = LazyReCompile(r'[\w_][\w0-9._]*[(]?')

0 commit comments

Comments
 (0)