Changeset 3194 for trunk/CifFile/CifFile.py
- Timestamp:
- Dec 15, 2017 1:19:06 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CifFile/CifFile.py
r3137 r3194 2817 2817 return lambda a:a #can't do anything numeric 2818 2818 2819 def convert_list_values(structure,dimension):2820 """Convert the values according to the element2821 structure given in [[structure]]"""2822 if isinstance(structure,(unicode,str)): #simple repetition2823 func_def = "element_convert = convert_single_value('%s')" % structure2824 else:2825 func_def = "def element_convert(element):\n"2826 func_def += " final_val = []\n"2827 for pos_no in range(len(structure)):2828 func_def += " final_val.append("2829 type_spec = structure[pos_no]2830 if type_spec == 'Real':2831 cf = "float_with_esd("2832 elif type_spec in ('Count','Integer','Index','Binary','Hexadecimal','Octal'):2833 cf = 'int('2834 elif type_spec == 'Complex':2835 cf = 'complex('2836 elif type_spec == 'Imag':2837 cf = 'complex(0,'2838 elif type_spec in ('Code','Name','Tag'):2839 cf = '('2840 else: cf = ''2841 func_def += cf2842 func_def += "element[%d]" % pos_no2843 if "(" in cf: func_def +=")"2844 if type_spec in ('Code','Name','Tag'):2845 func_def +=".lower()"2846 func_def +=")\n" # close append2847 func_def += " return final_val\n"2848 print(func_def)2849 exec(func_def, globals()) #(re)defines element_convert in global namespace2850 if len(dimension)> 0 and int(dimension[0]) != 1:2851 return lambda a: list(map(element_convert,a))2852 else: return element_convert2853 2854 def convert_matrix_values(valtype):2855 """Convert a dREL String or Float valued List structure to a numpy matrix structure"""2856 # first convert to numpy array, then let numpy do the work2857 try: import numpy2858 except:2859 return lambda a:a #cannot do it2860 func_def = "def matrix_convert(a):\n"2861 func_def += " import numpy\n"2862 func_def += " p = numpy.array(a)\n"2863 if valtype == 'Real':2864 func_def+= " return p.astype('float')\n"2865 elif valtype == 'Integer':2866 func_def +=" return p.astype('int')\n"2867 elif valtype == 'Complex':2868 func_def +=" return p.astype('complex')\n"2869 else:2870 raise ValueError('Unknown matrix value type')2871 exec(func_def,globals()) #matrix convert is defined2872 return matrix_convert2819 #def convert_list_values(structure,dimension): 2820 # """Convert the values according to the element 2821 # structure given in [[structure]]""" 2822 # if isinstance(structure,(unicode,str)): #simple repetition 2823 # func_def = "element_convert = convert_single_value('%s')" % structure 2824 # else: 2825 # func_def = "def element_convert(element):\n" 2826 # func_def += " final_val = []\n" 2827 # for pos_no in range(len(structure)): 2828 # func_def += " final_val.append(" 2829 # type_spec = structure[pos_no] 2830 # if type_spec == 'Real': 2831 # cf = "float_with_esd(" 2832 # elif type_spec in ('Count','Integer','Index','Binary','Hexadecimal','Octal'): 2833 # cf = 'int(' 2834 # elif type_spec == 'Complex': 2835 # cf = 'complex(' 2836 # elif type_spec == 'Imag': 2837 # cf = 'complex(0,' 2838 # elif type_spec in ('Code','Name','Tag'): 2839 # cf = '(' 2840 # else: cf = '' 2841 # func_def += cf 2842 # func_def += "element[%d]" % pos_no 2843 # if "(" in cf: func_def +=")" 2844 # if type_spec in ('Code','Name','Tag'): 2845 # func_def +=".lower()" 2846 # func_def +=")\n" # close append 2847 # func_def += " return final_val\n" 2848 # print(func_def) 2849 # exec(func_def, globals()) #(re)defines element_convert in global namespace 2850 # if len(dimension)> 0 and int(dimension[0]) != 1: 2851 # return lambda a: list(map(element_convert,a)) 2852 # else: return element_convert 2853 # 2854 #def convert_matrix_values(valtype): 2855 # """Convert a dREL String or Float valued List structure to a numpy matrix structure""" 2856 # # first convert to numpy array, then let numpy do the work 2857 # try: import numpy 2858 # except: 2859 # return lambda a:a #cannot do it 2860 # func_def = "def matrix_convert(a):\n" 2861 # func_def += " import numpy\n" 2862 # func_def += " p = numpy.array(a)\n" 2863 # if valtype == 'Real': 2864 # func_def+= " return p.astype('float')\n" 2865 # elif valtype == 'Integer': 2866 # func_def +=" return p.astype('int')\n" 2867 # elif valtype == 'Complex': 2868 # func_def +=" return p.astype('complex')\n" 2869 # else: 2870 # raise ValueError('Unknown matrix value type') 2871 # exec(func_def,globals()) #matrix convert is defined 2872 # return matrix_convert 2873 2873 2874 2874 def interpret_structure(struc_spec):
Note: See TracChangeset
for help on using the changeset viewer.