Skip to content

Commit 2b481a7

Browse files
authored
Dev: Use functools.cache instead of custom memoize (#1939)
Since functools.cache is available in Python 3.9+, remove file pyshim.py and function utils.memoize.
2 parents f141f31 + 41a8320 commit 2b481a7

File tree

7 files changed

+22
-56
lines changed

7 files changed

+22
-56
lines changed

crmsh/pyshim.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

crmsh/ra.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import copy
77
import re
88
import glob
9+
import functools
910
from lxml import etree
1011
from . import cache
1112
from . import constants
@@ -102,7 +103,7 @@ def include(ra):
102103
return cache.store(ident, sorted(list(set(ra for ra in find_types() if include(ra)))))
103104

104105

105-
@utils.memoize
106+
@functools.cache
106107
def ra_meta(ra_class, ra_type, ra_provider):
107108
"""
108109
Return metadata for the given class/type/provider
@@ -112,12 +113,12 @@ def ra_meta(ra_class, ra_type, ra_provider):
112113
return crm_resource("--show-metadata %s:%s" % (ra_class, ra_type))
113114

114115

115-
@utils.memoize
116+
@functools.cache
116117
def get_stonithd_meta():
117118
return RAInfo(utils.pacemaker_fenced(), "metadata")
118119

119120

120-
@utils.memoize
121+
@functools.cache
121122
def get_properties_meta():
122123
cluster_option_meta = utils.get_cluster_option_metadata()
123124
if cluster_option_meta:
@@ -128,28 +129,28 @@ def get_properties_meta():
128129
raise ValueError("No cluster option metadata found")
129130

130131

131-
@utils.memoize
132+
@functools.cache
132133
def get_property_options(property_name):
133134
return get_properties_meta().param_options(property_name)
134135

135136

136-
@utils.memoize
137+
@functools.cache
137138
def get_properties_list():
138139
try:
139140
return list(get_properties_meta().params().keys())
140141
except:
141142
return []
142143

143144

144-
@utils.memoize
145+
@functools.cache
145146
def get_resource_meta():
146147
resource_meta = utils.get_resource_metadata()
147148
if resource_meta:
148149
return RAInfo("resource_meta", None, meta_string=resource_meta)
149150
return None
150151

151152

152-
@utils.memoize
153+
@functools.cache
153154
def get_resource_meta_list():
154155
try:
155156
return list(get_resource_meta().params().keys())

crmsh/report/sh.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import subprocess
22
import typing
33

4-
import crmsh.pyshim
54
import crmsh.sh
65
import crmsh.userdir
76

crmsh/sh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import subprocess
2727
import typing
2828
from io import StringIO
29+
from functools import cache
2930

3031
from . import constants
31-
from .pyshim import cache
3232
from . import user_of_host
3333
from .user_of_host import UserOfHost
3434

crmsh/ui_history.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
import re
99
import bz2
10+
from functools import cache
1011
from . import config
1112
from . import command
1213
from . import completers as compl
@@ -28,7 +29,7 @@
2829
ptest_options = ["@v+", "nograph", "scores", "actions", "utilization"]
2930

3031

31-
@utils.memoize
32+
@cache
3233
def crm_report():
3334
return history.Report()
3435

crmsh/user_of_host.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import subprocess
55
import time
66
import typing
7+
from functools import cache
78

89
from . import config
910
from . import constants
1011
from . import userdir
11-
from .pyshim import cache
1212

1313

1414
logger = logging.getLogger(__name__)

crmsh/utils.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import string
2121
import pwd
2222
import grp
23-
import functools
2423
import gzip
2524
import bz2
2625
import lzma
@@ -33,6 +32,7 @@
3332
from lxml import etree
3433
from packaging import version
3534
from enum import IntFlag, auto
35+
from functools import cache
3636

3737
import crmsh.parallax
3838
import crmsh.user_of_host
@@ -97,20 +97,6 @@ def raise_exception(e):
9797
raise e
9898

9999

100-
def memoize(function):
101-
"Decorator to invoke a function once only for any argument"
102-
memoized = {}
103-
104-
@functools.wraps(function)
105-
def inner(*args):
106-
if args in memoized:
107-
return memoized[args]
108-
r = function(*args)
109-
memoized[args] = r
110-
return r
111-
return inner
112-
113-
114100
@contextmanager
115101
def nogc():
116102
gc.disable()
@@ -135,7 +121,7 @@ def user_pair_for_ssh(host):
135121
raise ValueError('Can not create ssh session from {} to {}.'.format(this_node(), host))
136122

137123

138-
@memoize
124+
@cache
139125
def this_node():
140126
'returns name of this node (hostname)'
141127
return os.uname()[1]
@@ -202,37 +188,37 @@ def pacemaker_20_daemon(new, old):
202188
return old
203189

204190

205-
@memoize
191+
@cache
206192
def pacemaker_attrd():
207193
return pacemaker_20_daemon("pacemaker-attrd", "attrd")
208194

209195

210-
@memoize
196+
@cache
211197
def pacemaker_based():
212198
return pacemaker_20_daemon("pacemaker-based", "cib")
213199

214200

215-
@memoize
201+
@cache
216202
def pacemaker_controld():
217203
return pacemaker_20_daemon("pacemaker-controld", "crmd")
218204

219205

220-
@memoize
206+
@cache
221207
def pacemaker_execd():
222208
return pacemaker_20_daemon("pacemaker-execd", "lrmd")
223209

224210

225-
@memoize
211+
@cache
226212
def pacemaker_fenced():
227213
return pacemaker_20_daemon("pacemaker-fenced", "stonithd")
228214

229215

230-
@memoize
216+
@cache
231217
def pacemaker_remoted():
232218
return pacemaker_20_daemon("pacemaker-remoted", "pacemaker_remoted")
233219

234220

235-
@memoize
221+
@cache
236222
def pacemaker_schedulerd():
237223
return pacemaker_20_daemon("pacemaker-schedulerd", "pengine")
238224

@@ -2042,7 +2028,7 @@ def detect_gcp():
20422028
return False
20432029

20442030

2045-
@memoize
2031+
@cache
20462032
def detect_cloud():
20472033
"""
20482034
Tries to determine which (if any) cloud environment

0 commit comments

Comments
 (0)