From 8a0da9cb890af7622ab44f6d8e5fa4449c0a9e2c Mon Sep 17 00:00:00 2001 From: Mike Hiretsky Date: Fri, 14 Nov 2014 08:53:08 +0300 Subject: [PATCH] Begin refactoring --- pym/calculate/lib/utils/tools.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pym/calculate/lib/utils/tools.py b/pym/calculate/lib/utils/tools.py index f6a6d98..a1a4791 100644 --- a/pym/calculate/lib/utils/tools.py +++ b/pym/calculate/lib/utils/tools.py @@ -13,6 +13,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os +import traceback from functools import wraps from itertools import tee @@ -175,3 +177,27 @@ def debug(prefix="DEBUG {name}({args},{kw})={ret}"): return ret return wrapper return debug_decor + + +def short_traceback(e1, e2, e3): + """ + Return short traceback + """ + frame = e3 + for i in apply(traceback.format_exception, (e1, e2, e3)): + print i, + while frame.tb_next: + frame = frame.tb_next + module, part = os.path.split(frame.tb_frame.f_code.co_filename) + if part.endswith('.py'): + part = part[:-3] + fallback_mod = part + modname = [part] + while module != '/' and not module.endswith('site-packages'): + module, part = os.path.split(module) + modname.insert(0, part) + if module.endswith('site-packages'): + modname = ".".join(modname) + else: + modname = fallback_mod + return "%s:%s(%s:%s)" % (e1.__name__, str(e2), modname, frame.tb_lineno)