Skip to content

Commit

Permalink
list and dict input (#119)
Browse files Browse the repository at this point in the history
* list and dict input

* adapt test
  • Loading branch information
dsavchenko authored Oct 24, 2023
1 parent 1c86ce3 commit cc0c095
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
12 changes: 12 additions & 0 deletions nb2workflow/nbadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ def cast_parameter(x,par):
return True
else:
raise ValueError(f'Parameter {par["name"]} value "{x}" can not be interpreted as boolean.')
if par['python_type'] in [list, dict]:
try:
if type(x) is str:
decoded = json.loads(x)
else:
decoded = x
if type(decoded) is par['python_type']:
return decoded
else:
raise ValueError
except:
raise ValueError(f'Parameter {par["name"]} value "{x}" can not be interpreted as {par["python_type"].__name__}.')
return par['python_type'](x)


Expand Down
22 changes: 21 additions & 1 deletion tests/test_boolean.py → tests/test_input_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,24 @@ def test_boolean_true(client):
def test_boolean_wrong(client):
r = client.get('/api/v1.0/get/testbool', query_string={'boolpar': 'spam'})
assert r.json['issues'][0] == 'Parameter boolpar value "spam" can not be interpreted as boolean.'


def test_list(client):
r = client.get('/api/v1.0/get/structured_input', query_string={'lst': '[1, 2, 3]'})
assert r.json['output']['lst'] == [1, 2, 3]

def test_list_wrong(client):
r = client.get('/api/v1.0/get/structured_input', query_string={'lst': 'baz'})
assert r.json['issues'][0] == 'Parameter lst value "baz" can not be interpreted as list.'

def test_dict(client):
r = client.get('/api/v1.0/get/structured_input', query_string={'dct': '{"foo": "bar"}'})
assert r.json['output']['dct'] == {"foo": "bar"}

def test_dict_complex(client):
r = client.get('/api/v1.0/get/structured_input',
query_string={'dct': '{"foo": ["bar", "baz"], "spam": ["ham", "eggs"]}'})
assert r.json['output']['dct'] == {"foo": ["bar", "baz"], "spam": ["ham", "eggs"]}

def test_dict_wrong(client):
r = client.get('/api/v1.0/get/structured_input', query_string={'dct': 'baz'})
assert r.json['issues'][0] == 'Parameter dct value "baz" can not be interpreted as dict.'
2 changes: 1 addition & 1 deletion tests/test_nbadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_find_notebooks(caplog):
assert 'Ignoring pattern.' in caplog.text

nbas = find_notebooks(nb_dir)
assert len(nbas) == 2
assert len(nbas) == 3

nbas = find_notebooks(nb_dir, pattern=r'.*bool')
assert len(nbas) == 1
Expand Down
47 changes: 47 additions & 0 deletions tests/testfiles/structured_input.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"a = 'baz'\n",
"lst = ['b', 'c', 'd']\n",
"dct = {'foo': [1, 2, 3], 'bar': [4, 5, 6]}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"outputs"
]
},
"outputs": [],
"source": [
"a\n",
"lst\n",
"dct"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit cc0c095

Please sign in to comment.