Skip to content

Commit

Permalink
[CodeStyle][UP028] using yield from (PaddlePaddle#52059)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liyulingyue authored Mar 25, 2023
1 parent 33b289d commit 85e2075
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ select = [
"UP025",
"UP026",
# "UP027",
# "UP028",
"UP028",
"UP029",
# "UP030",
# "UP031",
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/dataset/imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def load(pattern, out, label):
load(neg_pattern, INS, 1)

def reader():
for doc, label in INS:
yield doc, label
yield from INS

return reader

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ def local_iter():
'''

def local_iter():
for sample in samples:
yield sample
yield from samples

return local_iter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ def load(pattern, out, label):
load(neg_pattern, INS, 1)

def reader():
for doc, label in INS:
yield doc, label
yield from INS

return reader

Expand Down
3 changes: 1 addition & 2 deletions python/paddle/fluid/tests/unittests/feed_data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
def cyclic_reader(reader):
def __reader__():
while True:
for data in reader():
yield data
yield from reader()

return __reader__

Expand Down
18 changes: 6 additions & 12 deletions python/paddle/fluid/tests/unittests/test_imperative_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,15 @@ def need_no_grad_func(a, b=1):
)

def test_gen():
for i in range(3):
yield i
yield from range(3)

a = 0
for i in test_gen():
a += i

@paddle.no_grad()
def test_wrapped_gen():
for i in range(3):
yield i
yield from range(3)

b = 0
for i in test_wrapped_gen():
Expand Down Expand Up @@ -151,17 +149,15 @@ def need_enable_grad_func(a, b=1):
)

def test_gen():
for i in range(3):
yield i
yield from range(3)

a = 0
for i in test_gen():
a += i

@paddle.enable_grad()
def test_wrapped_gen():
for i in range(3):
yield i
yield from range(3)

b = 0
for i in test_wrapped_gen():
Expand Down Expand Up @@ -215,17 +211,15 @@ def need_enable_grad_func(a, b=1):
)

def test_gen():
for i in range(3):
yield i
yield from range(3)

a = 0
for i in test_gen():
a += i

@paddle.set_grad_enabled(True)
def test_wrapped_gen():
for i in range(3):
yield i
yield from range(3)

b = 0
for i in test_wrapped_gen():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ def get_feed_data_reader():
all_batch_tensors.append(tensors)

def __reader__():
for t in all_batch_tensors:
yield t
yield from all_batch_tensors

feed_data_reader = FeedDataReader(
feed_list=transformer_model.build_inputs(
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/nn/layer/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ def iter_inputs_and_params(self, inputs_in, param_attr_in=None):
inputs = inputs_in if (inputs_in is not None) else []
inputs = self._multiple_input(inputs)
param_attrs = self._multiple_param_attr(len(inputs), param_attr_in)
for ipt, param_attr in zip(inputs, param_attrs):
yield ipt, param_attr
yield from zip(inputs, param_attrs)

def input_dtype(self, inputs_in):
"""Get input data type
Expand Down
9 changes: 3 additions & 6 deletions python/paddle/reader/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def reader():
all_data = tuple(reader())

def __impl__():
for item in all_data:
yield item
yield from all_data

return __impl__

Expand Down Expand Up @@ -118,8 +117,7 @@ def reader():
rs = []
for r in readers:
rs.append(r())
for e in map(func, *rs):
yield e
yield from map(func, *rs)

return reader

Expand Down Expand Up @@ -228,8 +226,7 @@ def reader():
for r in readers:
rs.append(r())

for e in itertools.chain(*rs):
yield e
yield from itertools.chain(*rs)

return reader

Expand Down
3 changes: 1 addition & 2 deletions python/paddle/utils/layers_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ def _yield_value(iterable):
for key in _sorted(iterable):
yield iterable[key]
else:
for value in iterable:
yield value
yield from iterable


def _yield_flat_nest(nest):
Expand Down

0 comments on commit 85e2075

Please sign in to comment.