From fc21f2fe729d5775802d2da842093983d3814cce Mon Sep 17 00:00:00 2001 From: Vincent Esposito Date: Fri, 15 Sep 2023 15:39:50 -0700 Subject: [PATCH 1/2] customize calc namespace to have most used function at the top level --- hutch_python/calc_defaults.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hutch_python/calc_defaults.py b/hutch_python/calc_defaults.py index 36838924..b576ea9a 100644 --- a/hutch_python/calc_defaults.py +++ b/hutch_python/calc_defaults.py @@ -31,9 +31,23 @@ def collect_functions(modules): pass return HelpfulNamespace(**functions) +# import specific function to have at the top level of the namespace +try: + from pcdscalc.diffraction import (bragg_angle, darwin_width) +except ImportError: + print("Failed to import functions from pcdscalc.diffraction") + +try: + from pcdscalc.xray import transmission +except ImportError: + print("Failed to import functions from pcdscalc.xray") calc_namespace = HelpfulNamespace( + darwin_width=darwin_width, + bragg_angle=bragg_angle, + transmission=transmission, be_lens=collect_functions(['pcdscalc.be_lens_calcs']), common=collect_functions(['pcdscalc.common']), diffraction=collect_functions(['pcdscalc.diffraction']), + xray=collect_functions(['pcdscalc.xray']) ) From c5b3b45f42a3aeb8fd03fdab13fb3a7b0d6da733 Mon Sep 17 00:00:00 2001 From: Vincent Esposito Date: Thu, 21 Sep 2023 17:24:32 -0700 Subject: [PATCH 2/2] fix try/except for function import --- hutch_python/calc_defaults.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/hutch_python/calc_defaults.py b/hutch_python/calc_defaults.py index b576ea9a..b41521f2 100644 --- a/hutch_python/calc_defaults.py +++ b/hutch_python/calc_defaults.py @@ -31,23 +31,27 @@ def collect_functions(modules): pass return HelpfulNamespace(**functions) + # import specific function to have at the top level of the namespace +funcs = {} try: - from pcdscalc.diffraction import (bragg_angle, darwin_width) + from pcdscalc.diffraction import bragg_angle, darwin_width + funcs['bragg_angle'] = bragg_angle + funcs['darwin_width'] = darwin_width except ImportError: print("Failed to import functions from pcdscalc.diffraction") try: from pcdscalc.xray import transmission + funcs['transmission'] = transmission except ImportError: print("Failed to import functions from pcdscalc.xray") calc_namespace = HelpfulNamespace( - darwin_width=darwin_width, - bragg_angle=bragg_angle, - transmission=transmission, be_lens=collect_functions(['pcdscalc.be_lens_calcs']), common=collect_functions(['pcdscalc.common']), diffraction=collect_functions(['pcdscalc.diffraction']), - xray=collect_functions(['pcdscalc.xray']) + xray=collect_functions(['pcdscalc.xray']), + **funcs + )