-
Notifications
You must be signed in to change notification settings - Fork 166
Implement Any, All, Equals, and NotEquals substitutions #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
471dd62
f5ff332
d00945d
4de2826
097846f
bec6745
f34b2e5
7599a28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||
| # Copyright 2022 Open Source Robotics Foundation, Inc. | ||||||
| # | ||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| # you may not use this file except in compliance with the License. | ||||||
| # You may obtain a copy of the License at | ||||||
| # | ||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| # | ||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| # See the License for the specific language governing permissions and | ||||||
| # limitations under the License. | ||||||
|
|
||||||
| """Module for the EqualsSubstitution substitution.""" | ||||||
|
|
||||||
| import math | ||||||
|
|
||||||
| from typing import Any | ||||||
| from typing import Iterable | ||||||
| from typing import Optional | ||||||
| from typing import Text | ||||||
| from typing import Union | ||||||
|
|
||||||
| from ..frontend import expose_substitution | ||||||
| from ..launch_context import LaunchContext | ||||||
| from ..some_substitutions_type import SomeSubstitutionsType | ||||||
| from ..substitution import Substitution | ||||||
| from ..utilities import normalize_to_list_of_substitutions | ||||||
| from ..utilities.type_utils import is_substitution, perform_substitutions | ||||||
|
|
||||||
|
|
||||||
| def _str_is_bool(input_str: Text) -> bool: | ||||||
| """Check if string input is convertible to a boolean.""" | ||||||
| if not isinstance(input_str, Text): | ||||||
| return False | ||||||
| else: | ||||||
| return input_str.lower() in ('true', 'false', '1', '0') | ||||||
|
|
||||||
|
|
||||||
| def _str_is_float(input_str: Text) -> bool: | ||||||
| """Check if string input is convertible to a float.""" | ||||||
| try: | ||||||
| float(input_str) | ||||||
| return True | ||||||
| except ValueError: | ||||||
| return False | ||||||
|
|
||||||
|
|
||||||
| @expose_substitution('equals') | ||||||
| class EqualsSubstitution(Substitution): | ||||||
| """ | ||||||
| Substitution that checks if two inputs are equal. | ||||||
|
|
||||||
| Returns 'true' or 'false' strings depending on the result. | ||||||
| """ | ||||||
|
|
||||||
| def __init__( | ||||||
| self, | ||||||
| left: Optional[Union[Any, Iterable[Any]]], | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curios why this isn't just:
Suggested change
Same for the below.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using SomeSubstitutionsType means we can't have basic types as inputs (e.g. bool, numeric types, None) when it would be conceivable that a user might use them (at least via Python, maybe not the xml frontend) Though I guess users could just str() it if we remove it... What do you think? |
||||||
| right: Optional[Union[Any, Iterable[Any]]] | ||||||
| ) -> None: | ||||||
| """Create an EqualsSubstitution substitution.""" | ||||||
| super().__init__() | ||||||
|
|
||||||
| if not is_substitution(left): | ||||||
| if left is None: | ||||||
| left = '' | ||||||
| elif isinstance(left, bool): | ||||||
| left = str(left).lower() | ||||||
| else: | ||||||
| left = str(left) | ||||||
|
|
||||||
| if not is_substitution(right): | ||||||
| if right is None: | ||||||
| right = '' | ||||||
| elif isinstance(right, bool): | ||||||
| right = str(right).lower() | ||||||
| else: | ||||||
| right = str(right) | ||||||
|
|
||||||
| self.__left = normalize_to_list_of_substitutions(left) | ||||||
| self.__right = normalize_to_list_of_substitutions(right) | ||||||
|
|
||||||
| @classmethod | ||||||
| def parse(cls, data: Iterable[SomeSubstitutionsType]): | ||||||
| """Parse `EqualsSubstitution` substitution.""" | ||||||
| if len(data) != 2: | ||||||
| raise TypeError('and substitution expects 2 arguments') | ||||||
| return cls, {'left': data[0], 'right': data[1]} | ||||||
|
|
||||||
| @property | ||||||
| def left(self) -> Substitution: | ||||||
| """Getter for left.""" | ||||||
| return self.__left | ||||||
|
|
||||||
| @property | ||||||
| def right(self) -> Substitution: | ||||||
| """Getter for right.""" | ||||||
| return self.__right | ||||||
|
|
||||||
| def describe(self) -> Text: | ||||||
| """Return a description of this substitution as a string.""" | ||||||
| return f'EqualsSubstitution({self.left} {self.right})' | ||||||
|
|
||||||
| def perform(self, context: LaunchContext) -> Text: | ||||||
| """Perform the substitution.""" | ||||||
| left = perform_substitutions(context, self.left) | ||||||
| right = perform_substitutions(context, self.right) | ||||||
|
|
||||||
| # Special case for booleans | ||||||
| if _str_is_bool(left) and _str_is_bool(right): | ||||||
| return str((left.lower() in ('true', '1')) == (right.lower() in ('true', '1'))).lower() | ||||||
|
|
||||||
| # Special case for floats (epsilon closeness) | ||||||
| if _str_is_float(left) and _str_is_float(right): | ||||||
| return str(math.isclose(float(left), float(right))).lower() | ||||||
|
|
||||||
| return str(left == right).lower() | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This leaves me wondering about special cases, like float comparison. We're obviously comparing strings here, and that works for most situations, but I think we should expand the documentation of these to clarify what we're doing and what we're returning, i.e. none of the doc blocks mention we're returning the string
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am now supporting float comparisons. I will stop short of complex numbers though 😬 The docblocks also mention returning booleans-as-strings now |
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright 2022 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Module for the NotEqualsSubstitution substitution.""" | ||
|
|
||
| from typing import Any | ||
| from typing import Iterable | ||
| from typing import Optional | ||
| from typing import Text | ||
| from typing import Union | ||
|
|
||
| from .equals_substitution import EqualsSubstitution | ||
| from ..frontend import expose_substitution | ||
| from ..launch_context import LaunchContext | ||
|
|
||
|
|
||
| @expose_substitution('not-equals') | ||
| class NotEqualsSubstitution(EqualsSubstitution): | ||
| """ | ||
| Substitution that checks if two inputs are not equal. | ||
|
|
||
| Returns 'true' or 'false' strings depending on the result. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| left: Optional[Union[Any, Iterable[Any]]], | ||
| right: Optional[Union[Any, Iterable[Any]]] | ||
| ) -> None: | ||
| """Create a NotEqualsSubstitution substitution.""" | ||
| super().__init__(left, right) | ||
|
|
||
| def describe(self) -> Text: | ||
| """Return a description of this substitution as a string.""" | ||
| return f'NotEqualsSubstitution({self.left} {self.right})' | ||
|
|
||
| def perform(self, context: LaunchContext) -> Text: | ||
| """Perform the substitution.""" | ||
| return str(not (super().perform(context) == 'true')).lower() |
Uh oh!
There was an error while loading. Please reload this page.