Skip to content

Conversation

@bdunleavy22
Copy link
Contributor

Add parameter to get elements dynamically from subset object

Add parameter to get elements dynamically from subset object
@MariusWirtz
Copy link
Collaborator

What do you think about merging the subset and subset_name arguments into 1 argument like this?

def get_element_names(
    self,
    dimension_name: str,
    hierarchy_name: str,
    subset: Union[str, "Subset"],
    private: bool = False,
    **kwargs
) -> List[str]:
    """
    Retrieve element names from a static or dynamic subset.

    :param dimension_name: Name of the dimension.
    :param hierarchy_name: Name of the hierarchy.
    :param subset: Subset name (str) or Subset object.
    :param private: Whether the subset is private.
    :param kwargs: Additional arguments.
    :return: List of element names.
    """
    if isinstance(subset, str):
        subset = self.get(subset, dimension_name, hierarchy_name, private=private, **kwargs)
    elif not isinstance(subset, Subet):
        raise ValueError(...)

    if subset.is_static:
        return list(subset_obj.elements)

    element_service = ElementService(self._rest)
    tuples = element_service.execute_set_mdx(
        mdx=subset_obj.expression,
        member_properties=["Name"],
        element_properties=None,
        parent_properties=None,
        **kwargs
    )
    return [entry[0].get("Name", "") for entry in tuples if entry and "Name" in entry[0]]

@bdunleavy22
Copy link
Contributor Author

I think that's a great idea!

Modified argument options for get_element_names
Added `update_static_elements()`
Modified `update()` to use `update_static_elements()` to reduce number of requests from 2 to 1 for static subset updates
@bdunleavy22
Copy link
Contributor Author

Modified argument options for get_element_names
Added update_static_elements()
Modified update() to use update_static_elements() to reduce number of requests from 2 to 1 for static subset updates

Copy link
Collaborator

@MariusWirtz MariusWirtz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider my feedback and make sure the tests in SubsetService_test.py execute successfully.

@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from typing import List
from typing import List, Optional, Union, Iterable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing 2 imports

import json
from TM1py.Objects import Element

subset = self.get(subset_name, dimension_name, hierarchy_name, private=private, **kwargs)
if isinstance(subset, str):
subset = self.get(subset, dimension_name, hierarchy_name, private=private, **kwargs)
elif not isinstance(subset, Subet):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: this should be Subset

subset_name = subset
else:
raise ValueError(f"subset argument must be of type 'str' or 'Subset', not '{type(subset)}'")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If dimension_name and hierarchy_name is not provided as arguments to the function, I think you should try to derive it from the subset object.

if subset is provided as string but dimension_name and hierarchy_name are missing it should raise a ValueError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants