|
| 1 | +import mplfinance as mpf |
| 2 | +import pandas as pd |
| 3 | +import textwrap |
| 4 | + |
| 5 | +def df_wrapcols(df,wrap_columns=None): |
| 6 | + |
| 7 | + if wrap_columns is None: return df |
| 8 | + if not isinstance(wrap_columns,dict): |
| 9 | + raise TypeError('wrap_columns must be a dict of column_names and wrap_lengths') |
| 10 | + |
| 11 | + for col in wrap_columns: |
| 12 | + if col not in df.columns: |
| 13 | + raise ValueError('column "'+str(col)+'" not found in df.columns') |
| 14 | + |
| 15 | + index = [] |
| 16 | + column_data = {} |
| 17 | + for col in df.columns: |
| 18 | + column_data[col] = [] |
| 19 | + |
| 20 | + for ix in df.index: |
| 21 | + row = df.loc[ix,] |
| 22 | + |
| 23 | + row_data = {} |
| 24 | + for col in row.index: |
| 25 | + cstr = str(row[col]) |
| 26 | + if col in wrap_columns: |
| 27 | + wlen = wrap_columns[col] |
| 28 | + tw = textwrap.wrap(cstr,wlen) if not cstr.isspace() else [' '] |
| 29 | + else: |
| 30 | + tw = [cstr] |
| 31 | + row_data[col] = tw |
| 32 | + |
| 33 | + cmax = max(row_data,key=lambda k: len(row_data[k])) |
| 34 | + rlen = len(row_data[cmax]) |
| 35 | + for r in range(rlen): |
| 36 | + for col in row.index: |
| 37 | + extension = [' ']*(rlen - len(row_data[col])) |
| 38 | + row_data[col].extend(extension) |
| 39 | + column_data[col].append(row_data[col][r]) |
| 40 | + ixstr = str(ix)+'.'+str(r) if r > 0 else str(ix) |
| 41 | + index.append(ixstr) |
| 42 | + |
| 43 | + return pd.DataFrame(column_data,index=index) |
| 44 | + |
| 45 | +def make_left_formatter(maxwidth): |
| 46 | + wm3 = maxwidth-3 |
| 47 | + w = maxwidth |
| 48 | + def left_formatter(value): |
| 49 | + if not isinstance(value,str): |
| 50 | + return f'{value:<}' |
| 51 | + elif value[0:maxwidth] == '-'*maxwidth: |
| 52 | + return f'{value:<{w}.{w}s}' |
| 53 | + #elif len(value) > maxwidth and value[0:maxwidth] != '-'*maxwidth: |
| 54 | + elif len(value) > maxwidth: |
| 55 | + return f'{value:<{wm3}.{wm3}s}...' |
| 56 | + else: |
| 57 | + return f'{value:<{w}.{w}s}' |
| 58 | + return left_formatter |
| 59 | + |
| 60 | + |
| 61 | +def kwarg_help( func_name, kwarg_names=None ): |
| 62 | + |
| 63 | + func_kwarg_map = { |
| 64 | + 'plot' : mpf.plotting._valid_plot_kwargs, |
| 65 | + 'make_addplot' : mpf.plotting._valid_addplot_kwargs, |
| 66 | + 'addplot' : mpf.plotting._valid_addplot_kwargs, |
| 67 | + 'make_marketcolors' : mpf._styles._valid_make_marketcolors_kwargs, |
| 68 | + 'marketcolors' : mpf._styles._valid_make_marketcolors_kwargs, |
| 69 | + 'make_mpf_style' : mpf._styles._valid_make_mpf_style_kwargs, |
| 70 | + 'mpf_style' : mpf._styles._valid_make_mpf_style_kwargs, |
| 71 | + 'style' : mpf._styles._valid_make_mpf_style_kwargs, |
| 72 | + 'renko_params' : mpf._utils._valid_renko_kwargs, |
| 73 | + 'renko' : mpf._utils._valid_renko_kwargs, |
| 74 | + 'pnf_params' : mpf._utils._valid_pnf_kwargs, |
| 75 | + 'pnf' : mpf._utils._valid_pnf_kwargs, |
| 76 | + 'hlines' : mpf._utils._valid_lines_kwargs, |
| 77 | + 'alines' : mpf._utils._valid_lines_kwargs, |
| 78 | + 'tlines' : mpf._utils._valid_lines_kwargs, |
| 79 | + 'vlines' : mpf._utils._valid_lines_kwargs, |
| 80 | + 'lines' : mpf._utils._valid_lines_kwargs, |
| 81 | + } |
| 82 | + |
| 83 | + if func_name not in func_kwarg_map: |
| 84 | + raise ValueError('Function name "'+func_name+'" NOT a valid function name') |
| 85 | + |
| 86 | + print('func_name=',func_name) |
| 87 | + |
| 88 | + vks = func_kwarg_map[func_name]() |
| 89 | + |
| 90 | + df = (pd.DataFrame(vks).T.head(18)).drop('Validator',axis=1) |
| 91 | + print('valid kwargs=\n',df) |
| 92 | + |
| 93 | + |
| 94 | +kwarg_help('plot') |
| 95 | +kwarg_help('xyz') |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +# vk = mpf.plotting._valid_plot_kwargs() |
| 100 | +# |
| 101 | +# df = (pd.DataFrame(vk).T.head(18)).drop('Validator',axis=1) |
| 102 | +# |
| 103 | +# df['Kwarg'] = df.index.values |
| 104 | +# df['Default'] = ["'"+d+"'" if isinstance(d,str) else str(d) for d in df['Default']] |
| 105 | +# |
| 106 | +# df = df[['Kwarg','Default','Description']] |
| 107 | +# df = df.head(5).append(df.tail(7)) |
| 108 | +# |
| 109 | +# # df.sort_index(inplace=True) |
| 110 | +# |
| 111 | +# df |
| 112 | +# |
| 113 | +# print('===========================') |
| 114 | +# |
| 115 | +# print(df) |
| 116 | +# |
| 117 | +# print('===========================') |
| 118 | +# |
| 119 | +# def make_left_formatter(maxwidth): |
| 120 | +# wm3 = maxwidth-3 |
| 121 | +# w = maxwidth |
| 122 | +# def left_formatter(value): |
| 123 | +# if not isinstance(value,str): |
| 124 | +# return f'{value:<}' |
| 125 | +# elif value[0:maxwidth] == '-'*maxwidth: |
| 126 | +# return f'{value:<{w}.{w}s}' |
| 127 | +# #elif len(value) > maxwidth and value[0:maxwidth] != '-'*maxwidth: |
| 128 | +# elif len(value) > maxwidth: |
| 129 | +# return f'{value:<{wm3}.{wm3}s}...' |
| 130 | +# else: |
| 131 | +# return f'{value:<{w}.{w}s}' |
| 132 | +# return left_formatter |
| 133 | +# |
| 134 | +# def df_wrapcols(df,wrap_columns=None): |
| 135 | +# |
| 136 | +# if wrap_columns is None: return df |
| 137 | +# if not isinstance(wrap_columns,dict): |
| 138 | +# raise TypeError('wrap_columns must be a dict of column_names and wrap_lengths') |
| 139 | +# |
| 140 | +# for col in wrap_columns: |
| 141 | +# if col not in df.columns: |
| 142 | +# raise ValueError('column "'+str(col)+'" not found in df.columns') |
| 143 | +# |
| 144 | +# index = [] |
| 145 | +# column_data = {} |
| 146 | +# for col in df.columns: |
| 147 | +# column_data[col] = [] |
| 148 | +# |
| 149 | +# for ix in df.index: |
| 150 | +# row = df.loc[ix,] |
| 151 | +# |
| 152 | +# row_data = {} |
| 153 | +# for col in row.index: |
| 154 | +# cstr = str(row[col]) |
| 155 | +# if col in wrap_columns: |
| 156 | +# wlen = wrap_columns[col] |
| 157 | +# tw = textwrap.wrap(cstr,wlen) if not cstr.isspace() else [' '] |
| 158 | +# else: |
| 159 | +# tw = [cstr] |
| 160 | +# row_data[col] = tw |
| 161 | +# |
| 162 | +# cmax = max(row_data,key=lambda k: len(row_data[k])) |
| 163 | +# rlen = len(row_data[cmax]) |
| 164 | +# for r in range(rlen): |
| 165 | +# for col in row.index: |
| 166 | +# extension = [' ']*(rlen - len(row_data[col])) |
| 167 | +# row_data[col].extend(extension) |
| 168 | +# column_data[col].append(row_data[col][r]) |
| 169 | +# ixstr = str(ix)+'.'+str(r) if r > 0 else str(ix) |
| 170 | +# index.append(ixstr) |
| 171 | +# |
| 172 | +# return pd.DataFrame(column_data,index=index) |
| 173 | +# |
| 174 | +# WRAPLEN = 55 |
| 175 | +# |
| 176 | +# df = df_wrapcols(df,wrap_columns={'Description':WRAPLEN}) |
| 177 | +# print('===========================') |
| 178 | +# print('dfnew1=',df) |
| 179 | +# |
| 180 | +# |
| 181 | +# # print('===========================') |
| 182 | +# # df.columns = [ ' '+col for col in df.columns ] |
| 183 | +# |
| 184 | +# dividers = [] |
| 185 | +# for col in df.columns: |
| 186 | +# dividers.append('-'*int(df[col].str.len().max())) |
| 187 | +# dfd = pd.DataFrame(dividers).T |
| 188 | +# dfd.columns = df.columns |
| 189 | +# dfd.index = pd.Index(['---']) |
| 190 | +# |
| 191 | +# print('===========================') |
| 192 | +# |
| 193 | +# df = dfd.append(df) |
| 194 | +# |
| 195 | +# fmts = {'Kwarg': make_left_formatter(df['Kwarg'].str.len().max()+1), |
| 196 | +# 'Description': make_left_formatter(WRAPLEN), |
| 197 | +# 'Default': make_left_formatter(8), |
| 198 | +# } |
| 199 | +# s = df.to_string(formatters=fmts,index=False,justify='left') |
| 200 | +# |
| 201 | +# print('\n ',s.replace('\n','\n ')) |
| 202 | +# |
| 203 | +# print('===========================') |
| 204 | +# |
0 commit comments