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

Unified Diff: build/android/pylib/utils/function_utils.py

Issue 2664873002: Add logdog_helper script. (Closed)
Patch Set: Add logdog_helper script. Created 3 years, 11 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: build/android/pylib/utils/function_utils.py
diff --git a/build/android/pylib/utils/function_utils.py b/build/android/pylib/utils/function_utils.py
new file mode 100644
index 0000000000000000000000000000000000000000..457188b57e725b30fe17c49eefaee0c13dd59afc
--- /dev/null
+++ b/build/android/pylib/utils/function_utils.py
@@ -0,0 +1,45 @@
+# Copyright 2017 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
+import logging
+
+
+def Memoize(f):
+ """Decorator to cache return values of function."""
+
+ memoize_dict = {}
+ @functools.wraps(f)
+ def wrapper(*args, **kwargs):
+ key = repr((args, kwargs))
+ if key not in memoize_dict:
+ try:
+ memoize_dict[key] = f(*args, **kwargs)
+ except Exception as e: # pylint: disable=broad-except
+ memoize_dict[key] = e
+ raise
+ return_value = memoize_dict[key]
+ if isinstance(return_value, Exception):
+ raise return_value
+ return return_value
+ return wrapper
+
+
+def NoRaiseException(default_return_value=None, exception_message=''):
mikecase (-- gone --) 2017/02/01 22:13:45 Added exception_message arg instead of always conf
+ """Returns decorator that catches and logs uncaught Exceptions.
+
+ Args:
+ default_return_value: Value to return in the case of uncaught Exception.
+ exception_message: Message for uncaught exceptions.
+ """
+ def decorator(f):
+ @functools.wraps(f)
+ def wrapper(*args, **kwargs):
+ try:
+ return f(*args, **kwargs)
+ except Exception: # pylint: disable=broad-except
+ logging.exception(exception_message)
+ return default_return_value
+ return wrapper
+ return decorator

Powered by Google App Engine
This is Rietveld 408576698