Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Unified Diff: infra/libs/decorators/decorators.py

Issue 355153002: Refactor infra git libs and testing. (Closed) Base URL: https://chromium.googlesource.com/infra/infra@fake_testing_support
Patch Set: Change config ref to have a sandard naming scheme Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: infra/libs/decorators/decorators.py
diff --git a/infra/libs/decorators/decorators.py b/infra/libs/decorators/decorators.py
new file mode 100644
index 0000000000000000000000000000000000000000..f3b48f03a8aef4cd3c7195cd4c7dcf10f8604a8e
--- /dev/null
+++ b/infra/libs/decorators/decorators.py
@@ -0,0 +1,56 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import functools
+
+
+class cached_property(object):
+ """Like @property, except that the result of get is cached on
+ self.{'_' + fn.__name__}.
+
+ NOTE: This implementation is not threadsafe.
+
+ >>> class Test(object):
+ ... @cached_property
+ ... def foo(self):
+ ... print "hello"
+ ... return 10
+ ...
+ >>> t = Test()
+ >>> t.foo
+ hello
+ 10
+ >>> t.foo
+ 10
+ >>> t.foo = 20
+ >>> t.foo
+ 20
+ >>> del t.foo
+ >>> t.foo
+ hello
+ 10
+ >>>
+ """
+ def __init__(self, fn):
+ self.func = fn
+ self._iname = "_" + fn.__name__
+ functools.update_wrapper(self, fn)
+
+ def __get__(self, inst, cls=None):
+ if inst is None:
+ return self
+ if not hasattr(inst, self._iname):
+ val = self.func(inst)
+ # Some methods call out to another layer to calculate the value. This
+ # higher layer will assign directly to the property, so we have to do
+ # the extra hasattr here to determine if the value has been set as a side
+ # effect of func()
+ if not hasattr(inst, self._iname):
+ setattr(inst, self._iname, val)
+ return getattr(inst, self._iname)
+
+ def __delete__(self, inst):
+ assert inst is not None
+ if hasattr(inst, self._iname):
+ delattr(inst, self._iname)

Powered by Google App Engine
This is Rietveld 408576698