MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / _sequence_like

Function _sequence_like

python/paddle/utils/layers_utils.py:191–208  ·  view source on GitHub ↗

Convert the sequence `args` to the same type as `instance`.

(instance, args)

Source from the content-addressed store, hash-verified

189
190
191def _sequence_like(instance, args):
192 """
193 Convert the sequence `args` to the same type as `instance`.
194 """
195 if isinstance(instance, dict):
196 result = dict(zip(_sorted(instance), args))
197 return type(instance)((key, result[key]) for key in instance.keys())
198 elif (
199 isinstance(instance, tuple)
200 and hasattr(instance, "_fields")
201 and isinstance(instance._fields, Sequence)
202 and all(isinstance(f, str) for f in instance._fields)
203 ):
204 # This is a namedtuple
205 return type(instance)(*args)
206 else:
207 # Not a namedtuple
208 return type(instance)(args)
209
210
211def _packed_nest_with_indices(structure, flat, index):

Callers 2

pack_sequence_asFunction · 0.85

Calls 4

_sortedFunction · 0.85
typeFunction · 0.50
allFunction · 0.50
keysMethod · 0.45

Tested by

no test coverage detected