X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=tools%2Fmisc.py;h=2c785482e5a9e1da735a4d05c3b75ec41b78493b;hb=74cf9d437ac242f610b17d388d3728b36afb4f64;hp=110c370a74500672c1749dbaf4bb4b9f7c115ae8;hpb=30a3414a97f6bbcae73a936f658b639296fcdf98;p=matthijs%2Fprojects%2Fxerxes.git diff --git a/tools/misc.py b/tools/misc.py index 110c370..2c78548 100644 --- a/tools/misc.py +++ b/tools/misc.py @@ -13,3 +13,30 @@ def make_iter(value): except TypeError: pass return [value] +# vim: set sts=4 sw=4 expandtab: + +""" +Decarator that catches any exception raised by the decorated function, +prints it to stdout and raises it again. +""" +def log_error(func): + def show(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception, e: + import traceback + traceback.print_exc() + raise e + return show + +def make_choices(objects): + """ + Transforms a list (or iteratable) of model objects to a list + suitable to be used as a list of choices in form widgets like + Select. + + This fullfills a similar (but simpler) function as + django.forms.models.ModelChoiceIterator, but that one requires a + FormField and is not public. + """ + return [(o.pk, o) for o in objects]