Skip to content

Commit 52c3b54

Browse files
_kwarg_help() working version
1 parent 7d5514a commit 52c3b54

File tree

1 file changed

+69
-127
lines changed

1 file changed

+69
-127
lines changed

src/mplfinance/_kwarg_help.py

Lines changed: 69 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -50,155 +50,97 @@ def left_formatter(value):
5050
return f'{value:<}'
5151
elif value[0:maxwidth] == '-'*maxwidth:
5252
return f'{value:<{w}.{w}s}'
53-
#elif len(value) > maxwidth and value[0:maxwidth] != '-'*maxwidth:
5453
elif len(value) > maxwidth:
5554
return f'{value:<{wm3}.{wm3}s}...'
5655
else:
5756
return f'{value:<{w}.{w}s}'
5857
return left_formatter
5958

6059

61-
def kwarg_help( func_name, kwarg_names=None ):
60+
def kwarg_help( func_name=None, kwarg_names=None ):
6261

6362
func_kwarg_map = {
6463
'plot' : mpf.plotting._valid_plot_kwargs,
6564
'make_addplot' : mpf.plotting._valid_addplot_kwargs,
66-
'addplot' : mpf.plotting._valid_addplot_kwargs,
6765
'make_marketcolors' : mpf._styles._valid_make_marketcolors_kwargs,
68-
'marketcolors' : mpf._styles._valid_make_marketcolors_kwargs,
6966
'make_mpf_style' : mpf._styles._valid_make_mpf_style_kwargs,
67+
'renko_params' : mpf._utils._valid_renko_kwargs,
68+
'pnf_params' : mpf._utils._valid_pnf_kwargs,
69+
'lines' : mpf._utils._valid_lines_kwargs,
70+
}
71+
72+
func_kwarg_aliases = {
73+
'addplot' : mpf.plotting._valid_addplot_kwargs,
74+
'marketcolors' : mpf._styles._valid_make_marketcolors_kwargs,
7075
'mpf_style' : mpf._styles._valid_make_mpf_style_kwargs,
7176
'style' : mpf._styles._valid_make_mpf_style_kwargs,
72-
'renko_params' : mpf._utils._valid_renko_kwargs,
7377
'renko' : mpf._utils._valid_renko_kwargs,
74-
'pnf_params' : mpf._utils._valid_pnf_kwargs,
7578
'pnf' : mpf._utils._valid_pnf_kwargs,
7679
'hlines' : mpf._utils._valid_lines_kwargs,
7780
'alines' : mpf._utils._valid_lines_kwargs,
7881
'tlines' : mpf._utils._valid_lines_kwargs,
7982
'vlines' : mpf._utils._valid_lines_kwargs,
80-
'lines' : mpf._utils._valid_lines_kwargs,
8183
}
8284

83-
if func_name not in func_kwarg_map:
85+
if func_name is None:
86+
print('\nUsage: `kwarg_help(func_name)` or `kwarg_help(func_name,kwarg_names)`')
87+
print(' kwarg_help is available for the following func_names:')
88+
s = str(list(func_kwarg_map.keys()))
89+
text = textwrap.wrap(s,68)
90+
for t in text:
91+
print(' ',t)
92+
print()
93+
return
94+
95+
fkmap = {**func_kwarg_map, **func_kwarg_aliases}
96+
97+
if func_name not in fkmap:
8498
raise ValueError('Function name "'+func_name+'" NOT a valid function name')
8599

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-
#
100+
if kwarg_names is not None and isinstance(kwarg_names,str):
101+
kwarg_names = [ kwarg_names, ]
102+
103+
if ( kwarg_names is not None
104+
and (not isinstance(kwarg_names,(list,tuple))
105+
or not all([isinstance(k,str) for k in kwarg_names])
106+
)
107+
):
108+
raise ValueError('kwarg_names must be a sequence (list,tuple) of strings')
109+
110+
vks = fkmap[func_name]()
111+
112+
df = (pd.DataFrame(vks).T).drop('Validator',axis=1)
113+
df.index.name = 'Kwarg'
114+
df.reset_index(inplace=True)
115+
116+
if kwarg_names is not None:
117+
for k in kwarg_names:
118+
if k not in df['Kwarg'].values:
119+
print(' Warning: "'+k+'" is not a valid `kwarg_name` for `func_name` "'+func_name,'"')
120+
df = df[ df['Kwarg'].isin(kwarg_names) ]
121+
if len(df) < 1:
122+
raise ValueError(' None of specified `kwarg_names` are valid for `func_name` "'+func_name,'"')
123+
124+
wraplen = 80 - (df['Kwarg'].str.len().max()+1 + 8)
125+
df = df_wrapcols(df,wrap_columns={'Description':wraplen})
126+
127+
dividers = []
128+
for col in df.columns:
129+
dividers.append('-'*int(df[col].str.len().max()))
130+
dfd = pd.DataFrame(dividers).T
131+
dfd.columns = df.columns
132+
dfd.index = pd.Index(['---'])
133+
134+
df = dfd.append(df)
135+
136+
formatters = { 'Kwarg' : make_left_formatter(df['Kwarg'].str.len().max()+1),
137+
'Default' : make_left_formatter( 8 ),
138+
'Description' : make_left_formatter( wraplen ),
139+
}
140+
141+
print('\n ','-'*78)
142+
print(' Kwargs for func_name "'+func_name+'":')
143+
144+
s = df.to_string(formatters=formatters,index=False,justify='left')
145+
146+
print('\n ',s.replace('\n','\n '))

0 commit comments

Comments
 (0)