Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3193adf

Browse files
committed
Replace compiled_regex with cached_property
1 parent37d5fdd commit3193adf

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

‎bpython/lazyre.py‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License
22
#
3-
# Copyright (c) 2015 Sebastian Ramacher
3+
# Copyright (c) 2015-2021 Sebastian Ramacher
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -20,8 +20,13 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222

23-
importfunctools
2423
importre
24+
fromtypingimportOptional,Iterator
25+
26+
try:
27+
fromfunctoolsimportcached_property
28+
exceptImportError:
29+
frombackports.cached_propertyimportcached_property# type: ignore
2530

2631

2732
classLazyReCompile:
@@ -30,32 +35,22 @@ class LazyReCompile:
3035
This class allows one to store regular expressions and compiles them on
3136
first use."""
3237

33-
def__init__(self,regex,flags=0):
38+
def__init__(self,regex:str,flags:int=0)->None:
3439
self.regex=regex
3540
self.flags=flags
36-
self.compiled=None
37-
38-
defcompile_regex(method):
39-
@functools.wraps(method)
40-
def_impl(self,*args,**kwargs):
41-
ifself.compiledisNone:
42-
self.compiled=re.compile(self.regex,self.flags)
43-
returnmethod(self,*args,**kwargs)
4441

45-
return_impl
42+
@cached_property
43+
defcompiled(self)->re.Pattern:
44+
returnre.compile(self.regex,self.flags)
4645

47-
@compile_regex
48-
deffinditer(self,*args,**kwargs):
46+
deffinditer(self,*args,**kwargs)->Iterator[re.Match]:
4947
returnself.compiled.finditer(*args,**kwargs)
5048

51-
@compile_regex
52-
defsearch(self,*args,**kwargs):
49+
defsearch(self,*args,**kwargs)->Optional[re.Match]:
5350
returnself.compiled.search(*args,**kwargs)
5451

55-
@compile_regex
56-
defmatch(self,*args,**kwargs):
52+
defmatch(self,*args,**kwargs)->Optional[re.Match]:
5753
returnself.compiled.match(*args,**kwargs)
5854

59-
@compile_regex
60-
defsub(self,*args,**kwargs):
55+
defsub(self,*args,**kwargs)->str:
6156
returnself.compiled.sub(*args,**kwargs)

‎requirements.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Pygments
2+
backports.cached-property;python_version<"3.9"
23
curtsies>=0.3.5
34
cwcwidth
45
greenlet
56
pyxdg
67
requests
7-
setuptools
8+
setuptools

‎setup.cfg‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ packages =
2020
bpython.translations
2121
bpdb
2222
install_requires =
23-
pygments
24-
requests
23+
backports.cached-property; python_version < "3.9"
2524
curtsies >=0.3.5
26-
greenlet
2725
cwcwidth
26+
greenlet
27+
pygments
2828
pyxdg
29+
requests
2930

3031
[options.extras_require]
3132
clipboard = pyperclip

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp