Skip to content

Commit

Permalink
fix: allow setitem over nonopen arg
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsavio committed Mar 16, 2016
1 parent 97fd678 commit 53d194d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
22 changes: 14 additions & 8 deletions hansel/crumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def _open_arg_items(self):

def _last_open_arg(self):
""" Return the name and idx of the last (right-most) open argument."""
for dpth, arg in reversed(list(self._open_arg_items())):
open_args = list(self._open_arg_items())
if not open_args:
return None, None

for dpth, arg in reversed(open_args):
return dpth, arg

def _first_open_arg(self):
Expand Down Expand Up @@ -408,7 +412,7 @@ def _check_args(self, arg_names, self_args):
if not anames and not aself:
return

if not aself:
if not aself or aself is None:
raise AttributeError('This Crumb has no remaining arguments: {}.'.format(self.path))

if not anames.issubset(aself):
Expand Down Expand Up @@ -522,6 +526,9 @@ def values_map(self, arg_name='', check_exists=False):
if not arg_name:
_, arg_name = self._last_open_arg()

if arg_name is None:
return [list(self.arg_values.items())]

arg_deps = self._arg_parents(arg_name)
values_map = None
for arg in arg_deps:
Expand Down Expand Up @@ -594,8 +601,10 @@ def ls(self, arg_name='', fullpath=True, make_crumbs=True, check_exists=True):
if not arg_name:
_, arg_name = self._last_open_arg()

self._check_open_args([arg_name])
self._check_for_ls(make_crumbs, fullpath)
if arg_name is not None:
self._check_args([arg_name], self.all_args())

self._check_ls_params(make_crumbs, fullpath)
values_map = self.values_map(arg_name, check_exists=check_exists)
if fullpath:
paths = self.build_paths(values_map, make_crumbs=make_crumbs)
Expand All @@ -604,7 +613,7 @@ def ls(self, arg_name='', fullpath=True, make_crumbs=True, check_exists=True):

return sorted(paths)

def _check_for_ls(self, make_crumbs, fullpath):
def _check_ls_params(self, make_crumbs, fullpath):
""" Raise errors if the arguments are not good for ls function."""
# if the first chunk of the path is a parameter, I am not interested in this (for now)
# check if the path is absolute, if not raise an NotImplementedError
Expand Down Expand Up @@ -709,9 +718,6 @@ def __getitem__(self, arg_name):
return self.ls(arg_name, fullpath=False, make_crumbs=False, check_exists=True)

def __setitem__(self, key, value):
if not _has_arg(self.path, arg_name=key):
raise KeyError("Expected `arg_name` to be one of ({}),"
" got {}.".format(list(self.open_args()), key))
_ = self.update(**{key: value})

def __ge__(self, other):
Expand Down
27 changes: 27 additions & 0 deletions hansel/tests/test_crumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,33 @@ def test_exists2(tmp_crumb):
assert not tmp_crumb.exists()


def test_setitem(tmp_crumb):
assert not op.exists(tmp_crumb.split()[0])

values_dict = {'session_id': ['session_{:02}'.format(i) for i in range(2)],
'subject_id': ['subj_{:03}'.format(i) for i in range(3)],
'modality': ['anat'],
'image': ['mprage1.nii', 'mprage2.nii', 'mprage3.nii'],
}

assert not tmp_crumb.exists()

_ = mktree(tmp_crumb, list(ParameterGrid(values_dict)))

cr = list(tmp_crumb.ls())[0]

assert not list(cr.open_args())

assert cr['image'] == [values_dict['image'][0]]

cr['image'] = 'mprage2.nii'

assert cr['image'] == ['mprage2.nii']

assert 'mprage2.nii' in cr.path
assert 'mprage2.nii' in cr.ls()[0].path


def test_contains(tmp_crumb):
assert 'modality' in tmp_crumb
assert 'subject_id' in tmp_crumb
Expand Down

0 comments on commit 53d194d

Please sign in to comment.