Skip to content

Commit 5d34cfc

Browse files
author
Daniel Goldfarb
committed
remove "Implemented" for kwarg validators; Almost all were True anyway.
1 parent 0b613fc commit 5d34cfc

File tree

3 files changed

+24
-117
lines changed

3 files changed

+24
-117
lines changed

src/mplfinance/_arg_validators.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,12 @@ def _validate_vkwargs_dict(vkwargs):
6060
# Check that we didn't make a typo in any of the things
6161
# that should be the same for all vkwargs dict items:
6262
for key, value in vkwargs.items():
63-
if len(value) != 3:
64-
raise ValueError('Items != 3 in valid kwarg table, for kwarg "'+key+'"')
63+
if len(value) != 2:
64+
raise ValueError('Items != 2 in valid kwarg table, for kwarg "'+key+'"')
6565
if 'Default' not in value:
6666
raise ValueError('Missing "Default" value for kwarg "'+key+'"')
67-
if 'Implemented' not in value:
68-
raise ValueError('Missing "Implemented" flag for kwarg "'+key+'"')
6967
if 'Validator' not in value:
7068
raise ValueError('Missing "Validator" function for kwarg "'+key+'"')
71-
if value['Implemented'] not in [True,False]:
72-
raise ValueError('"Implemented" flag NOT True or False for kwarg "'+key+'"')
7369

7470
def _process_kwargs(kwargs, vkwargs):
7571
'''
@@ -80,7 +76,7 @@ def _process_kwargs(kwargs, vkwargs):
8076
as kwargs and return the configuration dictionary.
8177
'''
8278
# initialize configuration from valid_kwargs_table:
83-
config = {}
79+
config = {}
8480
for key, value in vkwargs.items():
8581
config[key] = value['Default']
8682

@@ -89,18 +85,16 @@ def _process_kwargs(kwargs, vkwargs):
8985
for key in kwargs.keys():
9086
if key not in vkwargs:
9187
raise KeyError('Unrecognized kwarg="'+str(key)+'"')
92-
elif not vkwargs[key]['Implemented']:
93-
raise NotImplementedError('kwarg "'+key+'" is NOT YET implemented.')
9488
else:
9589
value = kwargs[key]
9690
try:
9791
valid = vkwargs[key]['Validator'](value)
9892
except Exception as ex:
99-
raise ValueError('kwarg "'+key+'" with invalid value: "'+str(value)+'"') from ex
93+
raise ValueError('kwarg "'+key+'" validator raised exception to value: "'+str(value)+'"') from ex
10094
if not valid:
10195
import inspect
10296
v = inspect.getsource(vkwargs[key]['Validator']).strip()
103-
raise ValueError('kwarg "'+key+'" with invalid value: "'+str(value)+'"\n '+v)
97+
raise ValueError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n '+v)
10498

10599
# ---------------------------------------------------------------
106100
# At this point in the loop, if we have not raised an exception,

src/mplfinance/_styles.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,51 +56,39 @@ def _apply_mpfstyle(style):
5656
def _valid_make_mpf_style_kwargs():
5757
vkwargs = {
5858
'base_mpf_style': { 'Default' : None,
59-
'Implemented' : True,
6059
'Validator' : lambda value: value in _styles.keys() },
6160

6261
'base_mpl_style': { 'Default' : None,
63-
'Implemented' : True,
6462
'Validator' : lambda value: isinstance(value,str) }, # and is in plt.style.available
6563

6664
'marketcolors' : { 'Default' : None, #
67-
'Implemented' : True,
6865
'Validator' : lambda value: isinstance(value,dict) },
6966

7067
'mavcolors' : { 'Default' : None,
71-
'Implemented' : True,
7268
'Validator' : lambda value: isinstance(value,list) }, # TODO: all([mcolors.is_color_like(v) for v in value.values()])
7369

7470
'facecolor' : { 'Default' : None,
75-
'Implemented' : True,
7671
'Validator' : lambda value: isinstance(value,str) },
7772

7873
'edgecolor' : { 'Default' : None,
79-
'Implemented' : True,
8074
'Validator' : lambda value: isinstance(value,str) },
8175

8276
'figcolor' : { 'Default' : None,
83-
'Implemented' : True,
8477
'Validator' : lambda value: isinstance(value,str) },
8578

8679
'gridcolor' : { 'Default' : None,
87-
'Implemented' : True,
8880
'Validator' : lambda value: isinstance(value,str) },
8981

9082
'gridstyle' : { 'Default' : None,
91-
'Implemented' : True,
9283
'Validator' : lambda value: isinstance(value,str) },
9384

9485
'gridaxis' : { 'Default' : None,
95-
'Implemented' : True,
9686
'Validator' : lambda value: value in [ 'vertical'[0:len(value)], 'horizontal'[0:len(value)], 'both'[0:len(value)] ] },
9787

9888
'y_on_right' : { 'Default' : None,
99-
'Implemented' : True,
10089
'Validator' : lambda value: isinstance(value,bool) },
10190

10291
'rc' : { 'Default' : None,
103-
'Implemented' : True,
10492
'Validator' : lambda value: isinstance(value,dict) },
10593

10694
}
@@ -137,46 +125,37 @@ def _valid_mpf_color_spec(value):
137125
def _valid_make_marketcolors_kwargs():
138126
vkwargs = {
139127
'up' : { 'Default' : None,
140-
'Implemented' : True,
141128
'Validator' : lambda value: mcolors.is_color_like(value) },
142129

143130
'down' : { 'Default' : None,
144-
'Implemented' : True,
145131
'Validator' : lambda value: mcolors.is_color_like(value) },
146132

147133
'alpha' : { 'Default' : None,
148-
'Implemented' : True,
149134
'Validator' : lambda value: ( isinstance(value,float) and
150135
0.0 <= value and 1.0 >= value ) },
151136

152137
'edge' : { 'Default' : None,
153-
'Implemented' : True,
154138
'Validator' : lambda value: _valid_mpf_color_spec(value) },
155139

156140
'wick' : { 'Default' : None,
157-
'Implemented' : True,
158141
'Validator' : lambda value: isinstance(value,dict)
159142
or isinstance(value,str)
160143
or mcolors.is_color_like(value) },
161144

162145
'ohlc' : { 'Default' : None,
163-
'Implemented' : True,
164146
'Validator' : lambda value: isinstance(value,dict)
165147
or isinstance(value,str)
166148
or mcolors.is_color_like(value) },
167149

168150
'volume' : { 'Default' : None,
169-
'Implemented' : True,
170151
'Validator' : lambda value: isinstance(value,dict)
171152
or isinstance(value,str)
172153
or mcolors.is_color_like(value) },
173154

174155
'inherit' : { 'Default' : False,
175-
'Implemented' : True,
176156
'Validator' : lambda value: isinstance(value,bool) },
177157

178158
'base_mpf_style': { 'Default' : None,
179-
'Implemented' : True,
180159
'Validator' : lambda value: isinstance(value,str) },
181160
}
182161
_validate_vkwargs_dict(vkwargs)

src/mplfinance/plotting.py

Lines changed: 19 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
from mplfinance import _styles
2020

21-
from mplfinance._arg_validators import _check_and_prepare_data
22-
from mplfinance._arg_validators import _mav_validator
23-
from mplfinance._arg_validators import _process_kwargs
21+
from mplfinance._arg_validators import _check_and_prepare_data, _mav_validator
22+
from mplfinance._arg_validators import _process_kwargs, _validate_vkwargs_dict
2423

2524

2625
def with_rc_context(func):
@@ -46,124 +45,77 @@ def _warn_no_xgaps_deprecated(value):
4645
return isinstance(value,bool)
4746

4847

49-
def _valid_kwargs_table():
48+
def _valid_plot_kwargs():
5049
'''
5150
Construct and return the "valid kwargs table" for the mplfinance.plot() function.
5251
A valid kwargs table is a `dict` of `dict`s. The keys of the outer dict are the
5352
valid key-words for the function. The value for each key is a dict containing
54-
3 specific keys: "Default", "Implemented", and "Validator" with the following values:
53+
2 specific keys: "Default", and "Validator" with the following values:
5554
"Default" - The default value for the kwarg if none is specified.
56-
"Implemented" - Boolean, has this kwarg been implemented or not.
57-
NOTE: A non-implemented kwarg will still be present in the
58-
configuration dict, along with the kwarg's default value.
5955
"Validator" - A function that takes the caller specified value for the kwarg,
6056
and validates that it is the correct type, and (for kwargs with
6157
a limited set of allowed values) may also validate that the
6258
kwarg value is one of the allowed values.
6359
'''
60+
6461
vkwargs = {
6562
'type' : { 'Default' : 'ohlc',
66-
67-
'Implemented' : True,
6863
'Validator' : lambda value: value in ['candle','candlestick','ohlc','bars','ohlc bars','line'] },
6964

7065
'style' : { 'Default' : 'default',
71-
72-
'Implemented' : True,
7366
'Validator' : lambda value: value in _styles.available_styles() or isinstance(value,dict) },
7467

7568
'volume' : { 'Default' : False,
76-
77-
'Implemented' : True,
7869
'Validator' : lambda value: isinstance(value,bool) },
7970

8071
'mav' : { 'Default' : None,
81-
82-
'Implemented' : True,
8372
'Validator' : _mav_validator },
8473

8574
'study' : { 'Default' : None,
86-
87-
'Implemented' : False,
8875
'Validator' : lambda value: isinstance(value,dict) }, #{'studyname': {study parms}} example: {'TE':{'mav':20,'upper':2,'lower':2}}
8976

9077
'marketcolors': { 'Default' : None, # use 'style' for default, instead.
91-
92-
'Implemented' : True,
9378
'Validator' : lambda value: isinstance(value,dict) },
9479

9580
'no_xgaps' : { 'Default' : True, # None means follow default logic below:
96-
# True for intraday data spanning 2 or more days, else False
97-
'Implemented' : True,
9881
'Validator' : lambda value: _warn_no_xgaps_deprecated(value) },
9982

100-
'show_nontrading': { 'Default' : False, # None means follow default logic below:
101-
# True for intraday data spanning 2 or more days, else False
102-
'Implemented' : True,
83+
'show_nontrading': { 'Default' : False,
10384
'Validator' : lambda value: isinstance(value,bool) },
10485

10586
'figscale' : { 'Default' : 1.0, # scale base figure size up or down.
106-
107-
'Implemented' : True,
10887
'Validator' : lambda value: isinstance(value,float) or isinstance(value,int) },
10988

110-
'figratio' : { 'Default' : (8.00,5.75), # aspect ratio; will equal fig size when figscale=1.0
111-
112-
'Implemented' : True,
89+
'figratio' : { 'Default' : (8.00,5.75), # aspect ratio; will equal fig size when figscale=1.0
11390
'Validator' : lambda value: isinstance(value,(tuple,list))
11491
and len(value) == 2
11592
and isinstance(value[0],(float,int))
11693
and isinstance(value[1],(float,int)) },
11794

118-
'title' : { 'Default' : None, # Plot Title
119-
120-
'Implemented' : True,
95+
'title' : { 'Default' : None, # Plot Title
12196
'Validator' : lambda value: isinstance(value,str) },
12297

123-
'ylabel' : { 'Default' : 'Price', # y-axis label
124-
125-
'Implemented' : True,
98+
'ylabel' : { 'Default' : 'Price', # y-axis label
12699
'Validator' : lambda value: isinstance(value,str) },
127100

128-
'ylabel_lower': { 'Default' : None, # y-axis label default logic below
129-
130-
'Implemented' : True,
101+
'ylabel_lower': { 'Default' : None, # y-axis label default logic below
131102
'Validator' : lambda value: isinstance(value,str) },
132103

133-
'xlabel' : { 'Default' : None, # x-axis label, default is None because obvious it's time or date
134-
135-
'Implemented' : False, # x-axis label, NOT implemented because obvious it's time or date (will see if users ask for it).
136-
'Validator' : lambda value: isinstance(value,str) },
104+
#'xlabel' : { 'Default' : None, # x-axis label, default is None because obvious it's time or date
105+
# 'Validator' : lambda value: isinstance(value,str) },
137106

138107
'addplot' : { 'Default' : None,
139-
140-
'Implemented' : True,
141108
'Validator' : lambda value: isinstance(value,dict) or (isinstance(value,list) and all([isinstance(d,dict) for d in value])) },
142109

143110
'savefig' : { 'Default' : None,
144-
145-
'Implemented' : True,
146111
'Validator' : lambda value: isinstance(value,dict) or isinstance(value,str) },
147112

148113
'block' : { 'Default' : True,
149-
150-
'Implemented' : True,
151114
'Validator' : lambda value: isinstance(value,bool) },
152115

153116
}
154-
# Check that we didn't make a typo in any of the things above:
155-
# that should otherwise be the same for all kwags:
156-
for key, value in vkwargs.items():
157-
if len(value) != 3:
158-
raise ValueError('Items != 3 in valid kwarg table, for kwarg "'+key+'"')
159-
if 'Default' not in value:
160-
raise ValueError('Missing "Default" value for kwarg "'+key+'"')
161-
if 'Implemented' not in value:
162-
raise ValueError('Missing "Implemented" flag for kwarg "'+key+'"')
163-
if 'Validator' not in value:
164-
raise ValueError('Missing "Validator" function for kwarg "'+key+'"')
165-
if value['Implemented'] not in [True,False]:
166-
raise ValueError('"Implemented" flag NOT True or False for kwarg "'+key+'"')
117+
118+
_validate_vkwargs_dict(vkwargs)
167119

168120
return vkwargs
169121

@@ -195,7 +147,7 @@ def plot( data, **kwargs ):
195147

196148
dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data)
197149

198-
config = _process_kwargs(kwargs, _valid_kwargs_table())
150+
config = _process_kwargs(kwargs, _valid_plot_kwargs())
199151

200152
style = config['style']
201153
if isinstance(style,str):
@@ -510,7 +462,7 @@ def plot( data, **kwargs ):
510462

511463

512464

513-
def _valid_addplot_kwargs_table():
465+
def _valid_addplot_kwargs():
514466

515467
valid_markers = ['.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8',
516468
's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd', '|', '_', 'P',
@@ -519,49 +471,31 @@ def _valid_addplot_kwargs_table():
519471

520472
valid_linestyles = ['-','solid','--','dashed','-.','dashdot','.','dotted',None,' ','']
521473

474+
522475
vkwargs = {
523476
'scatter' : { 'Default' : False,
524-
'Implemented' : True,
525477
'Validator' : lambda value: isinstance(value,bool) },
526478

527479
'panel' : { 'Default' : 'main',
528-
'Implemented' : True,
529480
'Validator' : lambda value: value in ['main','lower'] },
530481

531482
'marker' : { 'Default' : 'o',
532-
'Implemented' : True,
533483
'Validator' : lambda value: value in valid_markers },
534484

535485
'markersize' : { 'Default' : 18,
536-
'Implemented' : True,
537486
'Validator' : lambda value: isinstance(value,(int,float)) },
538487

539488
'color' : { 'Default' : None,
540-
'Implemented' : True,
541489
'Validator' : lambda value: mcolors.is_color_like(value) },
542490

543491
'linestyle' : { 'Default' : None,
544-
'Implemented' : True,
545492
'Validator' : lambda value: value in valid_linestyles },
546493

547494
'secondary_y' : { 'Default' : 'auto',
548-
'Implemented' : True,
549495
'Validator' : lambda value: isinstance(value,bool) or value == 'auto' }
550496
}
551497

552-
# Check that we didn't make a typo in any of the things above
553-
# that should otherwise be the same for all kwags:
554-
for key, value in vkwargs.items():
555-
if len(value) != 3:
556-
raise ValueError('Items != 3 in valid kwarg table, for kwarg "'+key+'"')
557-
if 'Default' not in value:
558-
raise ValueError('Missing "Default" value for kwarg "'+key+'"')
559-
if 'Implemented' not in value:
560-
raise ValueError('Missing "Implemented" flag for kwarg "'+key+'"')
561-
if 'Validator' not in value:
562-
raise ValueError('Missing "Validator" function for kwarg "'+key+'"')
563-
if value['Implemented'] not in [True,False]:
564-
raise ValueError('"Implemented" flag NOT True or False for kwarg "'+key+'"')
498+
_validate_vkwargs_dict(vkwargs)
565499

566500
return vkwargs
567501

@@ -576,6 +510,6 @@ def make_addplot(data, **kwargs):
576510
if not isinstance(data, (pd.Series, pd.DataFrame, np.ndarray, list)):
577511
raise TypeError('Wrong type for data, in make_addplot()')
578512

579-
config = _process_kwargs(kwargs, _valid_addplot_kwargs_table())
513+
config = _process_kwargs(kwargs, _valid_addplot_kwargs())
580514

581515
return dict( data=data, **config)

0 commit comments

Comments
 (0)