From 67ab324090116c9b4eb34fd30449def4d134adc8 Mon Sep 17 00:00:00 2001 From: gongweibao Date: Mon, 25 Jun 2018 08:47:47 -0500 Subject: [PATCH] Remove duplicated code. (#11685) --- python/paddle/fluid/framework.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 4c1c8443a64..eee64fc0509 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -559,19 +559,8 @@ class Operator(object): self.attrs[attr_name] is None): continue attr_val = self.attrs[attr_name] - if isinstance(attr_val, Block): - self.desc.set_block_attr(attr_name, - self.attrs[attr_name].desc) - elif isinstance(attr_val, list) and attr_val and \ - all(isinstance(v, Block) for v in attr_val): - self.desc.set_blocks_attr(attr_name, - [v.desc for v in attr_val]) - elif isinstance(attr_val, core.BlockDesc) or \ - isinstance(attr_val, core.ProgramDesc): - self.desc.set_serialized_attr( - attr_name, attr_val.serialize_to_string()) - else: - self.desc.set_attr(attr_name, attr_val) + self._update_desc_attr(attr_name, attr_val) + self.desc.check_attrs() if self.has_kernel(type): self.desc.infer_var_type(self.block.desc) @@ -718,6 +707,19 @@ class Operator(object): ValueError: If the type of value doesn't match with desc.attr_type(name). """ self.attrs[name] = val + self._update_desc_attr(name, val) + + def _update_desc_attr(self, name, val): + """ + Update the value of desc's attribute by attribute's name. + + Args: + name(str): the attribute name. + val(bool|int|str|float|list): the value of the attribute. + + Raises: + ValueError: If the type of value doesn't match with desc.attr_type(name). + """ if isinstance(val, Block): self.desc.set_block_attr(name, val.desc) elif isinstance(val, list) and val and all( -- GitLab