From 7c09f41bf9bd9e4539809609e353c20a3cc5da65 Mon Sep 17 00:00:00 2001 From: Felix Zumstein Date: Sat, 14 Jan 2017 17:56:09 +0000 Subject: [PATCH] lifted get_category out of xlfunc --- xlwings/udfs.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/xlwings/udfs.py b/xlwings/udfs.py index 31e8c9355..a37423966 100644 --- a/xlwings/udfs.py +++ b/xlwings/udfs.py @@ -54,20 +54,24 @@ def func_sig(f): } +def get_category(**func_kwargs): + if 'category' in func_kwargs: + category = func_kwargs.pop('category') + if isinstance(category, int): + if 1 <= category <= 14: + return category + raise Exception( + 'There is only 14 build-in categories available in Excel. Please use a string value to specify a custom category.') + if isinstance(category, str): + return category[:255] + raise Exception( + 'Category {0} should either be a predefined Excel category (int value) or a custom one (str value).'.format( + category)) + return 14 # Default category is "User Defined" + + def xlfunc(f=None, **kwargs): def inner(f): - def get_category(**func_kwargs): - if 'category' in func_kwargs: - category = func_kwargs.pop('category') - if isinstance(category, int): - if 1 <= category <= 14: - return category - raise Exception('There is only 14 build-in categories available in Excel. Please use a string value to specify a custom category.') - if isinstance(category, str): - return category[:255] - raise Exception('Category {0} should either be a predefined Excel category (int value) or a custom one (str value).'.format(category)) - return 14 # Default category is "User Defined" - if not hasattr(f, "__xlfunc__"): xlf = f.__xlfunc__ = {} xlf["name"] = f.__name__