perf: reduce the number of judgments

This commit is contained in:
topsworld 2024-12-31 15:40:43 +08:00
parent 7b7553c5f8
commit 8abf087c3e
2 changed files with 44 additions and 60 deletions

View File

@ -122,33 +122,25 @@ class Notify(MIoTActionEntity, NotifyEntity):
return return
in_value: list[dict] = [] in_value: list[dict] = []
for index, prop in enumerate(self.spec.in_): for index, prop in enumerate(self.spec.in_):
if ( if prop.format_ == 'str':
prop.format_ == 'str' if isinstance(in_list[index], (bool, int, float, str)):
and isinstance(in_list[index], (bool, int, float, str))
):
in_value.append( in_value.append(
{'piid': prop.iid, 'value': str(in_list[index])}) {'piid': prop.iid, 'value': str(in_list[index])})
continue continue
if ( elif prop.format_ == 'bool':
prop.format_ == 'bool' if isinstance(in_list[index], (bool, int)):
and isinstance(in_list[index], (bool, int)) # yes, no, on, off, true, false and other bool types
): # will also be parsed as 0 and 1 of int.
# yes, no, on, off, true, false and other bool types will also
# be parsed as 0 and 1 of int.
in_value.append( in_value.append(
{'piid': prop.iid, 'value': bool(in_list[index])}) {'piid': prop.iid, 'value': bool(in_list[index])})
continue continue
if ( elif prop.format_ == 'float':
prop.format_ == 'float' if isinstance(in_list[index], (int, float)):
and isinstance(in_list[index], (int, float))
):
in_value.append( in_value.append(
{'piid': prop.iid, 'value': in_list[index]}) {'piid': prop.iid, 'value': in_list[index]})
continue continue
if ( elif prop.format_ == 'int':
prop.format_ == 'int' if isinstance(in_list[index], int):
and isinstance(in_list[index], int)
):
in_value.append( in_value.append(
{'piid': prop.iid, 'value': in_list[index]}) {'piid': prop.iid, 'value': in_list[index]})
continue continue

View File

@ -141,33 +141,25 @@ class ActionText(MIoTActionEntity, TextEntity):
f'invalid action params, {value}') f'invalid action params, {value}')
in_value: list[dict] = [] in_value: list[dict] = []
for index, prop in enumerate(self.spec.in_): for index, prop in enumerate(self.spec.in_):
if ( if prop.format_ == 'str':
prop.format_ == 'str' if isinstance(in_list[index], (bool, int, float, str)):
and isinstance(in_list[index], (bool, int, float, str))
):
in_value.append( in_value.append(
{'piid': prop.iid, 'value': str(in_list[index])}) {'piid': prop.iid, 'value': str(in_list[index])})
continue continue
if ( elif prop.format_ == 'bool':
prop.format_ == 'bool' if isinstance(in_list[index], (bool, int)):
and isinstance(in_list[index], (bool, int)) # yes, no, on, off, true, false and other bool types
): # will also be parsed as 0 and 1 of int.
# yes, no, on, off, true, false and other bool types will also
# be parsed as 0 and 1 of int.
in_value.append( in_value.append(
{'piid': prop.iid, 'value': bool(in_list[index])}) {'piid': prop.iid, 'value': bool(in_list[index])})
continue continue
if ( elif prop.format_ == 'float':
prop.format_ == 'float' if isinstance(in_list[index], (int, float)):
and isinstance(in_list[index], (int, float))
):
in_value.append( in_value.append(
{'piid': prop.iid, 'value': in_list[index]}) {'piid': prop.iid, 'value': in_list[index]})
continue continue
if ( elif prop.format_ == 'int':
prop.format_ == 'int' if isinstance(in_list[index], int):
and isinstance(in_list[index], int)
):
in_value.append( in_value.append(
{'piid': prop.iid, 'value': in_list[index]}) {'piid': prop.iid, 'value': in_list[index]})
continue continue