util: new cached_property decorator

This commit is contained in:
The MMGen Project 2025-04-21 14:01:16 +00:00
commit b53fd52f47
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2

View file

@ -470,3 +470,12 @@ def have_sudo(*, silent=False):
return True
except:
return False
def cached_property(orig_func):
@property
def new_func(self):
attr_name = '_' + orig_func.__name__
if not hasattr(self, attr_name):
setattr(self, attr_name, orig_func(self))
return getattr(self, attr_name)
return new_func