comparison.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import typing as t
  12. import pandas as pd
  13. from .._warnings import _warn
  14. from ..gui import Gui
  15. from ..utils import _getscopeattr
  16. def _compare_function(
  17. gui: Gui, compare_name: str, name: str, value: pd.DataFrame, datanames: str
  18. ) -> t.Optional[pd.DataFrame]:
  19. try:
  20. names = datanames.split(",")
  21. if not names:
  22. return None
  23. compare_fn = gui._get_user_function(compare_name) if compare_name else None
  24. if callable(compare_fn):
  25. return gui._get_accessor().to_pandas(
  26. gui._call_function_with_state(compare_fn, [name, [gui._get_real_var_name(n) for n in names]])
  27. )
  28. elif compare_fn is not None:
  29. _warn(f"{compare_name}(): compare function name is not valid.")
  30. dfs = [gui._get_accessor().to_pandas(_getscopeattr(gui, n)) for n in names]
  31. return value.compare(dfs[0], keep_shape=True)
  32. except Exception as e:
  33. if not gui._call_on_exception(compare_name or "Gui._compare_function", e):
  34. _warn(f"{compare_name or 'Gui._compare_function'}(): compare function raised an exception", e)
  35. return None