Chromium Code Reviews| Index: build/android/pylib/utils/python_utils.py |
| diff --git a/build/android/pylib/utils/python_utils.py b/build/android/pylib/utils/python_utils.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1ba208ec067aab75bd85f07d4b392a45d610cc49 |
| --- /dev/null |
| +++ b/build/android/pylib/utils/python_utils.py |
| @@ -0,0 +1,44 @@ |
| +# Copyright 2017 The Chromium Authors. All rights reserved. |
|
jbudorick
2017/01/31 16:11:53
python_utils doesn't really say anything as a name
|
| +# 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): |
|
jbudorick
2017/01/31 16:11:53
I could've sworn that we had this somewhere, but I
mikecase (-- gone --)
2017/01/31 23:08:25
A slightly different implementation of this existe
|
| + """Decorator to cache return values of function.""" |
| + |
| + memoize_dict = {} |
| + @functools.wraps(f) |
| + def wrapper(*args, **kwargs): |
| + key = repr((args, kwargs)) |
|
jbudorick
2017/01/31 16:11:53
4 space indents...? Did you lift this from somewhe
mikecase (-- gone --)
2017/01/31 23:08:25
...
|
| + 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): |
| + """Returns decorator that catches and logs uncaught Exceptions. |
| + |
| + Args: |
| + default_return_value: Value to return in the case of uncaught Exception. |
| + """ |
| + def decorator(f): |
| + @functools.wraps(f) |
| + def wrapper(*args, **kwargs): |
| + try: |
| + return f(*args, **kwargs) |
| + except Exception: # pylint: disable=broad-except |
| + logging.exception('message') |
|
jbudorick
2017/01/31 16:11:53
You're just going to log 'message'?
mikecase (-- gone --)
2017/01/31 23:08:25
I mean, logging.exception will log the exception.
jbudorick
2017/01/31 23:15:12
Can it be an additional argument to NoRaiseExcepti
|
| + return default_return_value |
| + return wrapper |
| + return decorator |