Changeset 4655
- Timestamp:
- Nov 10, 2020 11:59:25 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIobj.py
r4654 r4655 2718 2718 sqrt = sq = lambda x: np.sqrt(x) 2719 2719 pi = lambda: np.pi 2720 2721 def FindFunction(f): 2722 '''Find the object corresponding to function f 2723 2724 :param str f: a function name such as 'numpy.exp' 2725 :returns: (pkgdict,pkgobj) where pkgdict contains a dict 2726 that defines the package location(s) and where pkgobj 2727 defines the object associated with the function. 2728 If the function is not found, pkgobj is None. 2729 ''' 2730 df = f.split('.') 2731 pkgdict = {} 2732 # no listed module name, try in current namespace 2733 if len(df) == 1: 2734 try: 2735 fxnobj = eval(f) 2736 return pkgdict,fxnobj 2737 except (AttributeError, NameError): 2738 return None,None 2739 2740 # includes a package, see if package is already imported 2741 pkgnam = '.'.join(df[:-1]) 2742 try: 2743 fxnobj = eval(f) 2744 pkgdict[pkgnam] = eval(pkgnam) 2745 return pkgdict,fxnobj 2746 except (AttributeError, NameError): 2747 pass 2748 # package not yet imported, so let's try 2749 if '.' not in sys.path: sys.path.append('.') 2750 pkgnam = '.'.join(df[:-1]) 2751 #for pkg in f.split('.')[:-1]: # if needed, descend down the tree 2752 # if pkgname: 2753 # pkgname += '.' + pkg 2754 # else: 2755 # pkgname = pkg 2756 try: 2757 exec('import '+pkgnam) 2758 pkgdict[pkgnam] = eval(pkgnam) 2759 fxnobj = eval(f) 2760 except Exception as msg: 2761 print('load of '+pkgnam+' failed with error='+str(msg)) 2762 return {},None 2763 # can we access the function? I am not exactly sure what 2764 # I intended this to test originally (BHT) 2765 try: 2766 fxnobj = eval(f,globals(),pkgdict) 2767 return pkgdict,fxnobj 2768 except Exception as msg: 2769 print('call to',f,' failed with error=',str(msg)) 2770 return None,None # not found 2771 2720 2772 class ExpressionObj(object): 2721 2773 '''Defines an object with a user-defined expression, to be used for … … 2873 2925 self.lastError = ('','') 2874 2926 import ast 2875 def FindFunction(f):2876 '''Find the object corresponding to function f2877 :param str f: a function name such as 'numpy.exp'2878 :returns: (pkgdict,pkgobj) where pkgdict contains a dict2879 that defines the package location(s) and where pkgobj2880 defines the object associated with the function.2881 If the function is not found, pkgobj is None.2882 '''2883 df = f.split('.')2884 pkgdict = {}2885 # no listed module name, try in current namespace2886 if len(df) == 1:2887 try:2888 fxnobj = eval(f)2889 return pkgdict,fxnobj2890 except (AttributeError, NameError):2891 return None,None2892 2893 # includes a package, see if package is already imported2894 pkgnam = '.'.join(df[:-1])2895 try:2896 fxnobj = eval(f)2897 pkgdict[pkgnam] = eval(pkgnam)2898 return pkgdict,fxnobj2899 except (AttributeError, NameError):2900 pass2901 # package not yet imported, so let's try2902 if '.' not in sys.path: sys.path.append('.')2903 pkgnam = '.'.join(df[:-1])2904 #for pkg in f.split('.')[:-1]: # if needed, descend down the tree2905 # if pkgname:2906 # pkgname += '.' + pkg2907 # else:2908 # pkgname = pkg2909 try:2910 exec('import '+pkgnam)2911 pkgdict[pkgnam] = eval(pkgnam)2912 fxnobj = eval(f)2913 except Exception as msg:2914 print('load of '+pkgnam+' failed with error='+str(msg))2915 return {},None2916 # can we access the function? I am not exactly sure what2917 # I intended this to test originally (BHT)2918 try:2919 fxnobj = eval(f,globals(),pkgdict)2920 return pkgdict,fxnobj2921 except Exception as msg:2922 print('call to',f,' failed with error=',str(msg))2923 return None,None # not found2924 2927 def ASTtransverse(node,fxn=False): 2925 2928 '''Transverse a AST-parsed expresson, compiling a list of variables
Note: See TracChangeset
for help on using the changeset viewer.