Coverage for larch/utils/show.py: 11%

99 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-10-16 21:04 +0000

1#!/usr/bin/env python 

2""" 

3 Larch show() function 

4""" 

5import os 

6import sys 

7import types 

8import numpy 

9from larch import Group, repr_value 

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 > show_tree(group1) 

37 """ 

38 if groups_shown is None: 

39 groups_shown = [] 

40 for item in dir(group): 

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

42 continue 

43 obj = getattr(group, item) 

44 dval = None 

45 if _larch.symtable.isgroup(obj): 

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

47 if id(obj) in groups_shown: 

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

49 else: 

50 groups_shown.append(id(obj)) 

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

52 else: 

53 dval = repr(obj) 

54 if isinstance(obj, numpy.ndarray): 

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

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

57 repr(obj.dtype)) 

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

59 

60 

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

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

63 """show group members: 

64 Options 

65 ------- 

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

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

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

69 with_methods: suppress display of methods if False 

70 

71 """ 

72 if sym is None: 

73 sym = _larch.symtable 

74 group = None 

75 symtable = _larch.symtable 

76 display = symtable._sys.display 

77 with_color = with_color and display.use_color 

78 

79 title = sym 

80 if symtable.isgroup(sym): 

81 group = sym 

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

83 elif isinstance(sym, types.ModuleType): 

84 group = sym 

85 title = sym.__name__ 

86 

87 if group is None: 

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

89 return 

90 if title.startswith(symtable.top_group): 

91 title = title[6:] 

92 

93 if group == symtable: 

94 title = 'Group _main' 

95 

96 ## set colors for output 

97 colopts1 = display.colors['text'] 

98 colopts2 = display.colors['text2'] 

99 if with_color: 

100 if color is not None: 

101 colopts1['color'] = color 

102 if color2 is not None: 

103 colopts2['color'] = color2 

104 

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

106 

107 members = dir(group) 

108 dmembers = [] 

109 nmethods = 0 

110 for item in members: 

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

112 not with_private): 

113 continue 

114 obj = getattr(group, item) 

115 if callable(obj): 

116 nmethods +=1 

117 if not with_methods: 

118 continue 

119 dmembers.append((item, obj)) 

120 write = _larch.writer.write 

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

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

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

124 

125 count = 0 

126 for item, obj in dmembers: 

127 dval = repr_value(obj) 

128 if color_output: 

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

130 count += 1 

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

132 if color_output: 

133 _larch.writer.set_textstyle('text') 

134 

135 _larch.writer.flush() 

136 

137def get_termcolor_opts(dtype, _larch=None): 

138 """ get color options suitable for passing to 

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

140 

141 first argument should be string of 

142 'text', 'text2', 'error', 'comment'""" 

143 out = {'color': None} 

144 display = _larch.symtable._sys.display 

145 if display.use_color: 

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

147 return out 

148 

149 

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

151 get_termcolor_opts= get_termcolor_opts)