Repository: Avaiga/taipy
File: taipy/gui_core/_adapters.py (Python)

Code before:
}


def _invoke_action(ent: t.Any, col: str, col_type: str, is_dn: bool, action: str, val: t.Any) -> bool:
    try:
        if col_type == "any":
            # when a property is not found, return True only if action is not equals
            entity = getattr(ent, col.split(".")[0]) if is_dn else ent
            if not hasattr(entity, "properties") or not entity.properties.get(col):
                return action == "!="
        if op := _operators.get(action):
            cur_val = attrgetter(col)(ent)
            return op(cur_val.isoformat() if isinstance(cur_val, (datetime, date)) else cur_val, val)
    except Exception as e:
        _warn(f"Error filtering with {col} {action} {val} on {ent}.", e)
    return True


def _get_datanode_property(attr: str):
    if (parts := attr.split(".")) and len(parts) > 1:
        return parts[1]
    return None


class _GuiCoreScenarioProperties(_TaipyBase):
    _SC_TYPES: t.Dict[str, t.Dict[str, t.Union[str, bool]]] = {
        "Config id": {"type": "string"},
        "Label": {"type": "string"},
        "Creation date": {"type": "date"},
        "Cycle label": {"type": "string", "cycle": True},
        "Cycle start": {"type": "date", "cycle": True},
        "Cycle end": {"type": "date", "cycle": True},
        "Primary": {"type": "boolean", "cycle": True},
        "Tags": {"type": "string"},
    }
    __SC_LABELS: t.Dict[str, str] = {
        "Config id": "config_id",
        "Creation date": "creation_date",
        "Label": "name",
        "Cycle label": "cycle.name",
        "Cycle start": "cycle.start",
        "Cycle end": "cycle.end",
        "Primary": "is_primary",
        "Tags": "tags",
    }
    __DN_TYPES = {"Up to date": "boolean", "Valid": "boolean", "Last edit date": "date"}
    __DN_LABELS = {"Up to date": "is_up_to_date", "Valid": "is_valid", "Last edit date": "last_edit_date"}
    __ENUMS = None

    @staticmethod
    def get_hash():

Review comment (suggestion):
Now that I think again about this, I think it makes far more sense to have a single dict:
```
_SC_PROPS: t.Dict[str, t.Dict[str, t.Union[str, bool]]] = {
    "Config id": {"attr": "config_id", "type": "string"},
    "Label": {"attr": "name", "type": "string"},
    "Creation date": {"attr": "creation_date", "type": "date"},
    ...
    "Cycle label": {"attr": "cycle.name", "type": "string", "for_cycle": True},
    ...
}
```

No need for _TYPES and _LABELS really, and less error prone.
Note that I renamed "cycle" to "for_cycle". Less ambiguous.
     

Code after:
}


def _invoke_action(ent: t.Any, col: str, col_type: str, is_dn: bool, action: str, val: t.Any) -> bool:
    try:
        if col_type == "any":
            # when a property is not found, return True only if action is not equals
            entity = getattr(ent, col.split(".")[0]) if is_dn else ent
            if not hasattr(entity, "properties") or not entity.properties.get(col):
                return action == "!="
        if op := _operators.get(action):
            cur_val = attrgetter(col)(ent)
            return op(cur_val.isoformat() if isinstance(cur_val, (datetime, date)) else cur_val, val)
    except Exception as e:
        _warn(f"Error filtering with {col} {action} {val} on {ent}.", e)
    return True


def _get_datanode_property(attr: str):
    if (parts := attr.split(".")) and len(parts) > 1:
        return parts[1]
    return None


class _GuiCoreScenarioProperties(_TaipyBase):
    _SC_PROPS: t.Dict[str, t.Dict[str, t.Union[str, bool]]] = {
        "Config id": {"attr": "config_id", "type": "string"},
        "Label": {"attr": "name", "type": "string"},
        "Creation date": {"attr": "creation_date", "type": "date"},
        "Cycle label": {"attr": "cycle.name", "type": "string", "for_cycle": True},
        "Cycle start": {"attr": "cycle.start", "type": "date", "for_cycle": True},
        "Cycle end": {"attr": "cycle.end", "type": "date", "for_cycle": True},
        "Primary": {"attr": "is_primary", "type": "boolean", "for_cycle": True},
        "Tags": {"attr": "tags", "type": "string"},
    }
    __DN_PROPS = {
        "Up to date": {"attr": "is_up_to_date", "type": "boolean"},
        "Valid": {"attr": "is_valid", "type": "boolean"},
        "Last edit date": {"attr": "last_edit_date", "type": "date"},
    }
    __ENUMS = None

    @staticmethod
    def get_hash():
        return _TaipyBase._HOLDER_PREFIX + "ScP"

    @staticmethod
    def get_type(attr: str):
        if prop := _get_datanode_property(attr):
            return _GuiCoreScenarioProperties.__DN_PROPS.get(prop, {"type": "any"}).get("type", "any")
        return _GuiCoreScenarioProperties._SC_PROPS.get(attr, {"type": "any"}).get("type", "any")