Skip to content

Commit d09702d

Browse files
committed
presentation order of special/general cases in source code
1 parent 818bfae commit d09702d

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

unpythonic/fup.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ def make_output(seq):
9898
if hasattr(cls, "_make"): # namedtuple support
9999
return cls._make(gen)
100100
return cls(gen)
101-
if isinstance(indices, (list, tuple)):
102-
seq = target
103-
for index, value in zip(indices, values):
104-
seq = ShadowedSequence(seq, index, value)
105-
return make_output(seq)
106-
# one index (or slice), value(s) pair only
107-
return make_output(ShadowedSequence(target, indices, values))
101+
if not isinstance(indices, (list, tuple)):
102+
# one index (or slice), value(s) pair only
103+
return make_output(ShadowedSequence(target, indices, values))
104+
seq = target
105+
for index, value in zip(indices, values):
106+
seq = ShadowedSequence(seq, index, value)
107+
return make_output(seq)
108108
if mappings:
109109
t = copy(target)
110110
t.update(**mappings) # TODO: use collections.ChainMap instead?
@@ -132,15 +132,15 @@ def __getitem__(self, k):
132132
ix = self.ix
133133
l = len(self)
134134
if in_slice(k, ix, l):
135-
if isinstance(ix, slice):
136-
# we already know k is in ix, so skip validation for speed.
137-
i = _index_in_slice(k, ix, l, _validate=False)
138-
if i >= len(self.v):
139-
# TODO: Would be nice to raise IndexError, but the genexpr
140-
# in fupdate automatically catches that, hiding the error.
141-
raise ValueError("Replacement sequence too short; attempted to access index {} with len {} (items: {})".format(i, len(self.v), self.v))
142-
return self.v[i]
143-
return self.v # int, just one item
135+
if isinstance(ix, int):
136+
return self.v # just one item
137+
# we already know k is in ix, so skip validation for speed.
138+
i = _index_in_slice(k, ix, l, _validate=False)
139+
if i >= len(self.v):
140+
# TODO: Would be nice to raise IndexError, but the genexpr
141+
# in fupdate automatically catches that, hiding the error.
142+
raise ValueError("Replacement sequence too short; attempted to access index {} with len {} (items: {})".format(i, len(self.v), self.v))
143+
return self.v[i]
144144
return self.seq[k] # not in slice
145145

146146
def __len__(self):
@@ -165,15 +165,15 @@ def in_slice(i, s, l=None):
165165
raise TypeError("i must be int, got {} with value {}".format(type(i), i))
166166
wrap = _make_negidx_converter(l)
167167
i = wrap(i)
168-
if isinstance(s, slice):
169-
start, stop, step = _canonize_slice(s, l, wrap)
170-
cmp_start, cmp_end = (ge, lt) if step > 0 else (le, gt)
171-
at_or_after_start = cmp_start(i, start)
172-
before_stop = cmp_end(i, stop)
173-
on_grid = (i - start) % step == 0
174-
return at_or_after_start and on_grid and before_stop
175-
s = wrap(s) # int
176-
return i == s
168+
if isinstance(s, int):
169+
s = wrap(s)
170+
return i == s
171+
start, stop, step = _canonize_slice(s, l, wrap)
172+
cmp_start, cmp_end = (ge, lt) if step > 0 else (le, gt)
173+
at_or_after_start = cmp_start(i, start)
174+
before_stop = cmp_end(i, stop)
175+
on_grid = (i - start) % step == 0
176+
return at_or_after_start and on_grid and before_stop
177177

178178
def index_in_slice(i, s, l=None):
179179
"""Return the index of the int i in the slice s, or None if i is not in s.

0 commit comments

Comments
 (0)