Skip to content

Commit

Permalink
cycle sort by creation_date (#1554)
Browse files Browse the repository at this point in the history
fix filter contains on str
resolves #1546

Co-authored-by: Fred Lefévère-Laoide <[email protected]>
  • Loading branch information
FredLL-Avaiga and Fred Lefévère-Laoide committed Jul 19, 2024
1 parent f987f22 commit 43081f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 13 additions & 7 deletions taipy/gui_core/_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def _invoke_action(
if op := _operators.get(action):
cur_val = attrgetter(col_fn or col)(ent)
cur_val = cur_val() if col_fn else cur_val
if isinstance(cur_val, Iterable):
if not isinstance(cur_val, str) and isinstance(cur_val, Iterable):
return _filter_iterable(cur_val, op, val)
return _filter_value(cur_val, op, val, _adapt_type)
except Exception as e:
Expand All @@ -306,11 +306,11 @@ def _invoke_action(
return True


def _get_entity_property(col: str, a_type: t.Type):
col_parts = col.split("(") # handle the case where the col is a method (ie get_simple_label())
def _get_entity_property(col: str, *types: t.Type):
col_parts = col.split("(", 2) # handle the case where the col is a method (ie get_simple_label())
col_fn = (
next(
(col_parts[0] for i in inspect.getmembers(a_type, predicate=inspect.isfunction) if i[0] == col_parts[0]),
(col_parts[0] for i in inspect.getmembers(types[0], predicate=inspect.isfunction) if i[0] == col_parts[0]),
None,
)
if len(col_parts) > 1
Expand All @@ -319,10 +319,16 @@ def _get_entity_property(col: str, a_type: t.Type):

def sort_key(entity: t.Union[Scenario, Cycle, Sequence, DataNode]):
# we compare only strings
if isinstance(entity, a_type):
if isinstance(entity, types):
if isinstance(entity, Cycle):
lcol = "creation_date"
lfn = None
else:
lcol = col
lfn = col_fn
try:
val = attrgetter(col_fn or col)(entity)
if col_fn:
val = attrgetter(lfn or lcol)(entity)
if lfn:
val = val()
except AttributeError as e:
if _is_debugging():
Expand Down
4 changes: 2 additions & 2 deletions taipy/gui_core/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ def get_sorted_scenario_list(
col = sd.get("col", "")
col = _GuiCoreScenarioProperties.get_col_name(col)
order = sd.get("order", True)
sorted_list = sorted(sorted_list, key=_get_entity_property(col, Scenario), reverse=not order)
sorted_list = sorted(sorted_list, key=_get_entity_property(col, Scenario, Cycle), reverse=not order)
else:
sorted_list = sorted(entities, key=_get_entity_property("creation_date", Scenario))
sorted_list = sorted(entities, key=_get_entity_property("creation_date", Scenario, Cycle))
return [self.cycle_adapter(e, sorts) if isinstance(e, Cycle) else e for e in sorted_list]

def get_filtered_scenario_list(
Expand Down

0 comments on commit 43081f7

Please sign in to comment.