def to_reader(model, field): flag_attr = 'allow_create_{}_g'.format(field) method_attr = 'create_{}_g'.format(field) flag_g = getattr(model.meta, flag_attr) if flag_g: return SequentialReader.wrap(getattr(model, method_attr)()) value = getattr(model, field, None) if value is None: return RandomSequentialReader.from_list([]) if isinstance(value, (list, tuple)): return RandomSequentialReader.from_list(value) return SequentialReader.wrap(iter(value)) # TypeError if not iterable
def test_sequential_reader(): def g_func(): for i in range(0, 5): yield i g = g_func() reader = SequentialReader.wrap(g) assert len(list(reader)) == 5