Browse Source

util: new `cached_property` decorator

The MMGen Project 7 months ago
parent
commit
b53fd52f47
1 changed files with 9 additions and 0 deletions
  1. 9 0
      mmgen/util.py

+ 9 - 0
mmgen/util.py

@@ -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