Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{'int': '1', 'empty_int': '', 'str': 'hello', 'empty_str': '', 'bool': 'true', 'float': '1.1'}
)
val = utils.get_str_arg(req, 'str')
assert isinstance(val, str) and val == 'hello'
val = utils.get_str_arg(req, 'str_def', default='def')
assert val == 'def'
val = utils.get_str_arg(req, 'empty_str')
assert val is None
with mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_str_arg(req, 'str', default='def')
assert val == 'def'
val = utils.get_int_arg(req, 'int')
assert isinstance(val, int) and val == 1
val = utils.get_int_arg(req, 'int_def', default=2)
assert val == 2
val = utils.get_int_arg(req, 'empty_int')
assert val is None
with mock.patch('{}.int'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_int_arg(req, 'int', default=2)
assert val == 2
val = utils.get_bool_arg(req, 'bool')
assert isinstance(val, bool) and val
val = utils.get_float_arg(req, 'float')
assert isinstance(val, float) and val == 1.1
val = utils.get_float_arg(req, 'int_def', default=2.0)
assert val == 2.0
val = utils.get_float_arg(req, 'empty_float')
assert val is None
with mock.patch('{}.float'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_float_arg(req, 'float', default=2.0)
assert val == 2
def test_getters(builtin_pkg):
req = build_req_tuple(
{'int': '1', 'empty_int': '', 'str': 'hello', 'empty_str': '', 'bool': 'true', 'float': '1.1'}
)
val = utils.get_str_arg(req, 'str')
assert isinstance(val, str) and val == 'hello'
val = utils.get_str_arg(req, 'str_def', default='def')
assert val == 'def'
val = utils.get_str_arg(req, 'empty_str')
assert val is None
with mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_str_arg(req, 'str', default='def')
assert val == 'def'
val = utils.get_int_arg(req, 'int')
assert isinstance(val, int) and val == 1
val = utils.get_int_arg(req, 'int_def', default=2)
assert val == 2
val = utils.get_int_arg(req, 'empty_int')
assert val is None
with mock.patch('{}.int'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_int_arg(req, 'int', default=2)
assert val == 2
val = utils.get_bool_arg(req, 'bool')
assert isinstance(val, bool) and val
val = utils.get_float_arg(req, 'float')
assert isinstance(val, float) and val == 1.1
val = utils.get_float_arg(req, 'int_def', default=2.0)
assert val == 2.0
val = utils.get_float_arg(req, 'empty_float')
assert isinstance(val, str) and val == 'hello'
val = utils.get_str_arg(req, 'str_def', default='def')
assert val == 'def'
val = utils.get_str_arg(req, 'empty_str')
assert val is None
with mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_str_arg(req, 'str', default='def')
assert val == 'def'
val = utils.get_int_arg(req, 'int')
assert isinstance(val, int) and val == 1
val = utils.get_int_arg(req, 'int_def', default=2)
assert val == 2
val = utils.get_int_arg(req, 'empty_int')
assert val is None
with mock.patch('{}.int'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_int_arg(req, 'int', default=2)
assert val == 2
val = utils.get_bool_arg(req, 'bool')
assert isinstance(val, bool) and val
val = utils.get_float_arg(req, 'float')
assert isinstance(val, float) and val == 1.1
val = utils.get_float_arg(req, 'int_def', default=2.0)
assert val == 2.0
val = utils.get_float_arg(req, 'empty_float')
assert val is None
with mock.patch('{}.float'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_float_arg(req, 'float', default=2.0)
assert val == 2
def test_getters(builtin_pkg):
req = build_req_tuple(
{'int': '1', 'empty_int': '', 'str': 'hello', 'empty_str': '', 'bool': 'true', 'float': '1.1'}
)
val = utils.get_str_arg(req, 'str')
assert isinstance(val, str) and val == 'hello'
val = utils.get_str_arg(req, 'str_def', default='def')
assert val == 'def'
val = utils.get_str_arg(req, 'empty_str')
assert val is None
with mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_str_arg(req, 'str', default='def')
assert val == 'def'
val = utils.get_int_arg(req, 'int')
assert isinstance(val, int) and val == 1
val = utils.get_int_arg(req, 'int_def', default=2)
assert val == 2
val = utils.get_int_arg(req, 'empty_int')
assert val is None
with mock.patch('{}.int'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
val = utils.get_int_arg(req, 'int', default=2)
assert val == 2
val = utils.get_bool_arg(req, 'bool')
assert isinstance(val, bool) and val
val = utils.get_float_arg(req, 'float')
assert isinstance(val, float) and val == 1.1
val = utils.get_float_arg(req, 'int_def', default=2.0)
assert val == 2.0
val = utils.get_float_arg(req, 'empty_float')
assert val is None
with mock.patch('{}.float'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
def get_histogram(data_id):
"""
:class:`flask:flask.Flask` route which returns output from numpy.histogram to front-end as JSON
:param data_id: integer string identifier for a D-Tale process's data
:type data_id: str
:param col: string from flask.request.args['col'] containing name of a column in your dataframe
:param query: string from flask.request.args['query'] which is applied to DATA using the query() function
:param bins: the number of bins to display in your histogram, options on the front-end are 5, 10, 20, 50
:returns: JSON {results: DATA, desc: output from pd.DataFrame[col].describe(), success: True/False}
"""
col = get_str_arg(request, 'col', 'values')
query = get_str_arg(request, 'query')
bins = get_int_arg(request, 'bins', 20)
try:
data = DATA[data_id]
if query:
data = data.query(query)
selected_col = find_selected_column(data, col)
data = data[~pd.isnull(data[selected_col])][[selected_col]]
hist = np.histogram(data, bins=bins)
desc = load_describe(data[selected_col])
return jsonify(data=[json_float(h) for h in hist[0]], labels=['{0:.1f}'.format(l) for l in hist[1]], desc=desc)
except BaseException as e:
return jsonify(dict(error=str(e), traceback=str(traceback.format_exc())))