@@ -97,34 +97,33 @@ def make_output(seq):
9797 gen = (x for x in seq )
9898 if hasattr (cls , "_make" ): # namedtuple support
9999 return cls ._make (gen )
100- else :
101- return cls (gen )
100+ return cls (gen )
102101 if isinstance (indices , (list , tuple )):
103102 seq = target
104103 for index , value in zip (indices , values ):
105104 seq = ShadowedSequence (seq , index , value )
106105 return make_output (seq )
107- else : # one index (or slice), value(s) pair only
108- return make_output (ShadowedSequence (target , indices , values ))
109- elif mappings :
106+ # one index (or slice), value(s) pair only
107+ return make_output (ShadowedSequence (target , indices , values ))
108+ if mappings :
110109 t = copy (target )
111110 t .update (** mappings ) # TODO: use collections.ChainMap instead?
112111 return t
113112 return copy (target )
114113
115114# Needed by fupdate for immutable sequence inputs (no item assignment).
116115class ShadowedSequence (Sequence ):
117- def __init__ (self , seq , ix , v ):
118- """Sequence with some elements shadowed by those from another sequence.
116+ """Sequence with some elements shadowed by those from another sequence.
119117
120- Or in other words, a functionally updated view of a sequence.
118+ Or in other words, a functionally updated view of a sequence.
121119
122- Essentially, ``out[k] = v[index_in_slice(k, ix)] if in_slice(k, ix) else seq[k]``,
123- but doesn't actually allocate ``out``.
120+ Essentially, ``out[k] = v[index_in_slice(k, ix)] if in_slice(k, ix) else seq[k]``,
121+ but doesn't actually allocate ``out``.
124122
125- ``ix`` may be integer (if ``v`` represents one item only)
126- or slice (if ``v`` is intended as a sequence).
127- """
123+ ``ix`` may be integer (if ``v`` represents one item only)
124+ or slice (if ``v`` is intended as a sequence).
125+ """
126+ def __init__ (self , seq , ix , v ):
128127 self .seq = seq
129128 self .ix = ix
130129 self .v = v
@@ -141,10 +140,8 @@ def __getitem__(self, k):
141140 # in fupdate automatically catches that, hiding the error.
142141 raise ValueError ("Replacement sequence too short; attempted to access index {} with len {} (items: {})" .format (i , len (self .v ), self .v ))
143142 return self .v [i ]
144- else : # int, just one item
145- return self .v
146- else :
147- return self .seq [k ]
143+ return self .v # int, just one item
144+ return self .seq [k ] # not in slice
148145
149146 def __len__ (self ):
150147 return len (self .seq )
@@ -175,9 +172,8 @@ def in_slice(i, s, l=None):
175172 before_stop = cmp_end (i , stop )
176173 on_grid = (i - start ) % step == 0
177174 return at_or_after_start and on_grid and before_stop
178- else :
179- s = wrap (s )
180- return i == s
175+ s = wrap (s ) # int
176+ return i == s
181177
182178def index_in_slice (i , s , l = None ):
183179 """Return the index of the int i in the slice s, or None if i is not in s.
0 commit comments