Coverage for /Users/Newville/Codes/xraylarch/larch/utils/show.py: 11%

107 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-09 10:08 -0600

1#!/usr/bin/env python 

2""" 

3 Larch show() function 

4""" 

5import os 

6import sys 

7import types 

8import numpy 

9from larch import Group 

10 

11TERMCOLOR_COLORS = ('grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') 

12 

13def get(sym=None, _larch=None): 

14 """get object from symbol table from symbol name: 

15 

16 >>> g = group(a = 1, b=2.3, z = 'a string') 

17 >>> print(get('g.z')) 

18 'a string' 

19 

20 """ 

21 if sym is None: 

22 sym = _larch.symtable 

23 group = None 

24 symtable = _larch.symtable 

25 if symtable.isgroup(sym): 

26 group = sym 

27 elif isinstance(sym, types.ModuleType): 

28 group = sym 

29 elif isinstance(sym, str): 

30 group = symtable._lookup(sym, create=False) 

31 return group 

32 

33 

34def show_tree(group, indent=0, groups_shown=None, _larch=None): 

35 """show members of a Group, with a tree structure for sub-groups 

36 

37 > show_tree(group1) 

38 

39 """ 

40 if groups_shown is None: 

41 groups_shown = [] 

42 for item in dir(group): 

43 if (item.startswith('__') and item.endswith('__')): 

44 continue 

45 obj = getattr(group, item) 

46 dval = None 

47 if _larch.symtable.isgroup(obj): 

48 _larch.writer.write('%s %s: %s\n' % (indent*' ', item, obj)) 

49 if id(obj) in groups_shown: 

50 _larch.writer.write('%s (shown above)\n' % (indent*' ')) 

51 else: 

52 groups_shown.append(id(obj)) 

53 show_tree(obj, indent=indent+3, _larch=_larch, groups_shown=groups_shown) 

54 else: 

55 dval = repr(obj) 

56 if isinstance(obj, numpy.ndarray): 

57 if len(obj) > 10 or len(obj.shape)>1: 

58 dval = "array<shape=%s, type=%s>" % (repr(obj.shape), 

59 repr(obj.dtype)) 

60 _larch.writer.write('%s %s: %s\n' % (indent*' ', item, dval)) 

61 

62def show(sym=None, with_private=False, with_color=True, color=None, 

63 color2=None, truncate=True, with_methods=True, _larch=None): 

64 """show group members: 

65 Options 

66 ------- 

67 with_private: show 'private' members ('__private__') if True 

68 with_color: show alternating lines in color if True and color is available. 

69 truncate: truncate representation of lengthy lists and tuples if True 

70 with_methods: suppress display of methods if False 

71 

72 """ 

73 if sym is None: 

74 sym = _larch.symtable 

75 group = None 

76 symtable = _larch.symtable 

77 display = symtable._sys.display 

78 with_color = with_color and display.use_color 

79 

80 title = sym 

81 if symtable.isgroup(sym): 

82 group = sym 

83 title = repr(sym)[1:-1] 

84 elif isinstance(sym, types.ModuleType): 

85 group = sym 

86 title = sym.__name__ 

87 

88 if group is None: 

89 _larch.writer.write("%s\n" % repr(sym)) 

90 return 

91 if title.startswith(symtable.top_group): 

92 title = title[6:] 

93 

94 if group == symtable: 

95 title = 'Group _main' 

96 

97 ## set colors for output 

98 colopts1 = display.colors['text'] 

99 colopts2 = display.colors['text2'] 

100 if with_color: 

101 if color is not None: 

102 colopts1['color'] = color 

103 if color2 is not None: 

104 colopts2['color'] = color2 

105 

106 _copts = {1: colopts1, 0: colopts2} 

107 

108 members = dir(group) 

109 dmembers = [] 

110 nmethods = 0 

111 for item in members: 

112 if (item.startswith('__') and item.endswith('__') and 

113 not with_private): 

114 continue 

115 obj = getattr(group, item) 

116 if callable(obj): 

117 nmethods +=1 

118 if not with_methods: 

119 continue 

120 dmembers.append((item, obj)) 

121 write = _larch.writer.write 

122 color_output = hasattr(_larch.writer, 'set_textstyle') 

123 title_fmt = '== %s: %i methods, %i attributes ==\n' 

124 write(title_fmt % (title, nmethods, len(dmembers)-nmethods)) 

125 

126 count = 0 

127 for item, obj in dmembers: 

128 if (isinstance(obj, numpy.ndarray) and 

129 (len(obj) > 10 or len(obj.shape)>1)): 

130 dval = "array<shape=%s, type=%s>" % (repr(obj.shape), 

131 repr(obj.dtype)) 

132 elif isinstance(obj, (list, tuple)) and truncate and len(obj) > 5: 

133 dval = "[%s, %s, ... %s, %s]" % (repr(obj[0]), repr(obj[1]), 

134 repr(obj[-2]), repr(obj[-1])) 

135 else: 

136 try: 

137 dval = repr(obj) 

138 except: 

139 dval = obj 

140 if color_output: 

141 _larch.writer.set_textstyle({True:'text', False:'text2'}[(count%2)==1]) 

142 count += 1 

143 write(' %s: %s\n' % (item, dval)) 

144 if color_output: 

145 _larch.writer.set_textstyle('text') 

146 

147 _larch.writer.flush() 

148 

149def get_termcolor_opts(dtype, _larch=None): 

150 """ get color options suitable for passing to 

151 larch's writer.write() for color output 

152 

153 first argument should be string of 

154 'text', 'text2', 'error', 'comment'""" 

155 out = {'color': None} 

156 display = _larch.symtable._sys.display 

157 if display.use_color: 

158 out = getattr(display.colors, dtype, out) 

159 return out 

160 

161 

162_larch_builtins = dict(show=show, show_tree=show_tree, get=get, 

163 get_termcolor_opts= get_termcolor_opts)