Skip to content

Commit a4ca273

Browse files
committed
RF: refactorings from comments
More explicit stuff in docstrings. Use generic OrderedDict rather than externals copy.
1 parent cee8b22 commit a4ca273

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

nibabel/nicom/ascconv.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
import re
77
import ast
8-
from ..externals import OrderedDict
8+
from collections import OrderedDict
99

1010

1111
ASCCONV_RE = re.compile(
@@ -94,15 +94,20 @@ def assign2atoms(assign_ast, default_class=int):
9494
target = target.value
9595
prev_target_type = list
9696
else:
97-
raise AscconvParseError(
98-
'Unexpected LHS element {0}'.format(target))
97+
raise AscconvParseError(f'Unexpected LHS element {target}')
9998
return reversed(atoms)
10099

101100

102101
def _create_obj_in(atom, root):
103-
""" Create object defined in `atom` in dict-like given by `root`
102+
""" Find / create object defined in `atom` in dict-like given by `root`
103+
104+
Returns corresponding value if there is already a key matching
105+
`atom.obj_id` in `root`.
104106
105-
Return defined object.
107+
Otherwise, create new object with ``atom.obj_type`, insert into dictionary,
108+
and return new object.
109+
110+
Can therefore modify `root` in place.
106111
"""
107112
name = atom.obj_id
108113
obj = root.get(name, NoValue)
@@ -114,9 +119,15 @@ def _create_obj_in(atom, root):
114119

115120

116121
def _create_subscript_in(atom, root):
117-
""" Create object defined in `atom` at index ``atom.obj_id`` in list `root`
122+
""" Find / create and insert object defined by `atom` from list `root`
123+
124+
The `atom` has an index, defined in ``atom.obj_id``. If `root` is long
125+
enough to contain this index, return the object at that index. Otherwise,
126+
extend `root` with None elements to contain index ``atom.obj_id``, then
127+
create a new object via ``atom.obj_type()``, insert at the end of the list,
128+
and return this object.
118129
119-
Return defined object.
130+
Can therefore modify `root` in place.
120131
"""
121132
curr_n = len(root)
122133
index = atom.obj_id
@@ -154,7 +165,7 @@ def obj_from_atoms(atoms, namespace):
154165
root_obj = _create_subscript_in(el, root_obj)
155166
if not isinstance(root_obj, el.obj_type):
156167
raise AscconvParseError(
157-
'Unexpected type for {0} in {1}'.format(el.obj_id, prev_root))
168+
f'Unexpected type for {el.obj_id} in {prev_root}')
158169
return prev_root, el.obj_id
159170

160171

@@ -166,7 +177,7 @@ def _get_value(assign):
166177
return value.s
167178
if isinstance(value, ast.UnaryOp) and isinstance(value.op, ast.USub):
168179
return -value.operand.n
169-
raise AscconvParseError('Unexpected RHS of assignment: {0}'.format(value))
180+
raise AscconvParseError(f'Unexpected RHS of assignment: {value}')
170181

171182

172183
def parse_ascconv(ascconv_str, str_delim='"'):

nibabel/nicom/tests/test_ascconv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"""
33

44
from os.path import join as pjoin, dirname
5+
from collections import OrderedDict
56

67
import numpy as np
78

89
from .. import ascconv
9-
from ...externals import OrderedDict
1010

1111
from numpy.testing import assert_array_equal, assert_array_almost_equal
1212

0 commit comments

Comments
 (0)