Skip to content

MDXUtils

DimensionSelection(dimension_name, elements=None, subset=None, expression=None)

Instances of this class to be passed to construct_mdx function

Source code in TM1py/Utils/MDXUtils.py
def __init__(self, dimension_name, elements=None, subset=None, expression=None):
    warnings.warn(
        "class DimensionSelection will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    self.dimension_name = dimension_name
    self.selection_type = self.determine_selection_type(elements, subset, expression)
    if self.selection_type == self.SUBSET:
        self.expression = curly_braces(
            expression="Tm1SubsetToSet([{dimension}], '{subset}')".format(dimension=dimension_name, subset=subset)
        )
    elif self.selection_type == self.EXPRESSION:
        self.expression = curly_braces(expression=expression)
    elif self.selection_type == self.ITERABLE:
        self.expression = curly_braces(
            expression=",".join(["[{}].[{}]".format(dimension_name, element) for element in elements])
        )
    elif not self.selection_type:
        self.expression = curly_braces(expression="TM1SubsetAll([{dimension}])".format(dimension=dimension_name))

EXPRESSION = 2 class-attribute instance-attribute

ITERABLE = 3 class-attribute instance-attribute

SUBSET = 1 class-attribute instance-attribute

dimension_name = dimension_name instance-attribute

expression = curly_braces(expression=("Tm1SubsetToSet([{dimension}], '{subset}')".format(dimension=dimension_name, subset=subset))) instance-attribute

selection_type = self.determine_selection_type(elements, subset, expression) instance-attribute

determine_selection_type(elements=None, subset=None, expression=None) staticmethod

Source code in TM1py/Utils/MDXUtils.py
@staticmethod
def determine_selection_type(elements=None, subset=None, expression=None):
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    if elements is not None and subset is None and expression is None:
        return DimensionSelection.ITERABLE
    elif elements is None and subset is not None and expression is None:
        return DimensionSelection.SUBSET
    elif elements is None and subset is None and expression is not None:
        return DimensionSelection.EXPRESSION
    elif elements is None and subset is None and expression is None:
        return None
    else:
        raise ValueError(
            "DimensionSelection constructor takes one type of selection only: " "elements, subset or expression"
        )

construct_mdx(cube_name, rows, columns, contexts=None, suppress=None)

Method to construct MDX Query from different dimension selection

Parameters:

Name Type Description Default
cube_name

Name of the Cube

required
rows

List of DimensionSelections

required
columns

List of DimensionSelections

required
contexts

Dictionary of Dimensions and Elements

None
suppress

"Both", "Rows", "Columns" or None

None

Returns:

Type Description

Generated MDX Query

Source code in TM1py/Utils/MDXUtils.py
def construct_mdx(cube_name, rows, columns, contexts=None, suppress=None):
    """Method to construct MDX Query from different dimension selection

    :param cube_name: Name of the Cube
    :param rows: List of DimensionSelections
    :param columns: List of DimensionSelections
    :param contexts: Dictionary of Dimensions and Elements
    :param suppress: "Both", "Rows", "Columns" or None
    :return: Generated MDX Query
    """
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    # MDX Skeleton
    mdx_template = "SELECT {}{} ON ROWS, {}{} ON COLUMNS FROM [{}] {}"
    # Suppression
    mdx_rows_suppress = "NON EMPTY " if suppress and suppress.upper() in ["ROWS", "BOTH"] else ""
    mdx_columns_suppress = "NON EMPTY " if suppress and suppress.upper() in ["COLUMNS", "BOTH"] else ""
    # Rows and Columns
    mdx_rows = construct_mdx_axis(rows)
    mdx_columns = construct_mdx_axis(columns)
    # Context filter (where statement)
    mdx_where = ""
    if contexts:
        mdx_where_parts = ["[{}].[{}]".format(dim, elem) for dim, elem in contexts.items()]
        mdx_where = "".join(["WHERE (", ",".join(mdx_where_parts), ")"])
    # Return Full MDX
    return mdx_template.format(mdx_rows_suppress, mdx_rows, mdx_columns_suppress, mdx_columns, cube_name, mdx_where)

construct_mdx_axis(dim_selections)

Construct MDX for one Axis (Row or Column). Can have multiple dimensions stacked.

Parameters:

Name Type Description Default
dim_selections

instances of TM1py.Utils.MDXUtils.DimensionSelection

required

Returns:

Type Description

a valid MDX for an Axis

Source code in TM1py/Utils/MDXUtils.py
def construct_mdx_axis(dim_selections):
    """Construct MDX for one Axis (Row or Column).
    Can have multiple dimensions stacked.

    :param dim_selections: instances of TM1py.Utils.MDXUtils.DimensionSelection
    :return: a valid MDX for an Axis
    """
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    return "*".join(selection.expression for selection in dim_selections)

curly_braces(expression)

Put curly braces around a string

Parameters:

Name Type Description Default
expression
required

Returns:

Type Description
Source code in TM1py/Utils/MDXUtils.py
def curly_braces(expression):
    """Put curly braces around a string

    :param expression:
    :return:
    """
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    return "".join(
        ["{" if not expression.startswith("{") else "", expression, "}" if not expression.endswith("}") else ""]
    )

read_cube_name_from_mdx(mdx)

Read the cube name from a valid MDX Query

Parameters:

Name Type Description Default
mdx

The MDX Query as String

required

Returns:

Type Description

String, name of a cube

Source code in TM1py/Utils/MDXUtils.py
def read_cube_name_from_mdx(mdx):
    """Read the cube name from a valid MDX Query

    :param mdx: The MDX Query as String
    :return: String, name of a cube
    """
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    cube, _, _, _ = read_dimension_composition_from_mdx(mdx)
    return cube

read_dimension_composition_from_mdx(mdx)

Parse a valid MDX Query and return the name of the cube and a list of dimensions for each axis

Parameters:

Name Type Description Default
mdx
required

Returns:

Type Description
Source code in TM1py/Utils/MDXUtils.py
def read_dimension_composition_from_mdx(mdx):
    """Parse a valid MDX Query and return the name of the cube and a list of dimensions for each axis

    :param mdx:
    :return:
    """
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    mdx_rows, mdx_columns, mdx_from, mdx_where = split_mdx(mdx)

    cube = mdx_from[1:-1]
    rows = read_dimension_composition_from_mdx_set_or_tuple(mdx_rows)
    columns = read_dimension_composition_from_mdx_set_or_tuple(mdx_columns)
    titles = read_dimension_composition_from_mdx_set_or_tuple(mdx_where)

    return cube, rows, columns, titles

read_dimension_composition_from_mdx_set(mdx)

Source code in TM1py/Utils/MDXUtils.py
def read_dimension_composition_from_mdx_set(mdx):
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    dimensions = []
    mdx_without_spaces = "".join(mdx.split())
    for sub_mdx in mdx_without_spaces.split("}*{"):
        pos_start, pos_end = sub_mdx.find("["), sub_mdx.find("]")
        dimension_name = sub_mdx[pos_start + 1 : pos_end]
        dimensions.append(dimension_name)
    return dimensions

read_dimension_composition_from_mdx_set_or_tuple(mdx)

Source code in TM1py/Utils/MDXUtils.py
def read_dimension_composition_from_mdx_set_or_tuple(mdx):
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    mdx_without_spaces = "".join(mdx.split())
    # case for mdx statement no where statement
    if len(mdx_without_spaces) == 0:
        return []
    # case for tuples mdx statement on rows or columns
    if mdx_without_spaces[1] == "(" and mdx_without_spaces[-2] == ")":
        return read_dimension_composition_from_mdx_tuple(mdx)
    # case for where mdx statement
    elif mdx_without_spaces[0] == "(" and mdx_without_spaces[-1] == ")":
        return read_dimension_composition_from_mdx_tuple(mdx)
    # case for set mdx statement on rows or columns
    else:
        return read_dimension_composition_from_mdx_set(mdx)

read_dimension_composition_from_mdx_tuple(mdx)

Source code in TM1py/Utils/MDXUtils.py
def read_dimension_composition_from_mdx_tuple(mdx):
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    dimensions = []
    for unique_member_name in mdx.split(","):
        pos_start, pos_end = unique_member_name.find("["), unique_member_name.find("]")
        dimension_name = unique_member_name[pos_start + 1 : pos_end]
        # only parse through first tuple of potentially many tuples
        if dimension_name in dimensions:
            return dimensions
        dimensions.append(dimension_name)
    return dimensions

split_mdx(mdx)

Source code in TM1py/Utils/MDXUtils.py
def split_mdx(mdx):
    warnings.warn(
        "Module MdxUtils will be deprecated. Use https://github.com/cubewise-code/mdxpy instead",
        DeprecationWarning,
        stacklevel=2,
    )
    try:
        mdx_rows, mdx_rest = _find_case_and_space_insensitive_first_occurrence(
            text=mdx, pattern_start="{", pattern_end="}ONROWS"
        )
        mdx_columns, mdx_rest = _find_case_and_space_insensitive_first_occurrence(
            text=mdx_rest, pattern_start="{", pattern_end="}ONCOLUMNSFROM"
        )
        mdx_from, mdx_where = _find_case_and_space_insensitive_first_occurrence(text=mdx_rest, pattern_end="]WHERE")
        return mdx_rows, mdx_columns, mdx_from, mdx_where
    except ValueError:
        ValueError("Can't parse mdx: {}".format(mdx))