Skip to content

Commit

Permalink
Adjust allow_spread code
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusWirtz committed Apr 21, 2023
1 parent dbc9541 commit 0dfb8ee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
60 changes: 30 additions & 30 deletions TM1py/Services/CellService.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,39 +1148,42 @@ def _build_blob_to_cube_process(self, cube_name: str, process_name: str, blob_fi
# Define input statement depending on measure element's type: numeric or string
# For non-existing-element attempt to write to trigger error
# For consolidated elements spread if allowed else let fail.

measure_type_equal = f"ElementType('{cube_measure}', '', {variable_cube_measure}) @= "
n_write_types = ["'N'", "'AN'", "'C'", "''"]
numeric_write_condition = '% \n'.join([measure_type_equal + possible_type for possible_type in
n_write_types])
numeric_write_condition = '% \n'.join([
measure_type_equal + possible_type
for possible_type
in n_write_types])

any_c_element_in_write = '% \n'.join([f"ElementType('{dim}', '', {ele}) @= 'C'" for dim, ele in
zip(dimensions, dimension_variables)])
any_c_element_in_write = '% \n'.join([
f"ElementType('{dim}', '', {ele}) @= 'C'"
for dim, ele in
zip(dimensions, dimension_variables)])

numeric_write_statement_with_spread = f"""
nValue = StringToNumber({value_variable});
IF({any_c_element_in_write});
CellPutProportionalSpread(nValue,'{cube_name}',{comma_sep_var_elements});
ELSE;
{numeric_function_str}(nValue,'{cube_name}',{comma_sep_var_elements});
ENDIF;
"""
nValue = StringToNumber({value_variable});
IF({any_c_element_in_write});
CellPutProportionalSpread(nValue,'{cube_name}',{comma_sep_var_elements});
ELSE;
{numeric_function_str}(nValue,'{cube_name}',{comma_sep_var_elements});
ENDIF;
"""

numeric_write_statement_without_spread = f"""
nValue = StringToNumber({value_variable});
{numeric_function_str}(nValue,'{cube_name}',{comma_sep_var_elements});
"""
nValue = StringToNumber({value_variable});
{numeric_function_str}(nValue,'{cube_name}',{comma_sep_var_elements});
"""

string_write_condition = f"""
ElementType('{cube_measure}', '', {variable_cube_measure}) @= 'S' %
ElementType('{cube_measure}', '', {variable_cube_measure}) @= 'AS' %
ElementType('{cube_measure}', '', {variable_cube_measure}) @= 'AA'
"""
ElementType('{cube_measure}', '', {variable_cube_measure}) @= 'S' %
ElementType('{cube_measure}', '', {variable_cube_measure}) @= 'AS' %
ElementType('{cube_measure}', '', {variable_cube_measure}) @= 'AA'
"""

string_write_statement = f"""
sValue = {value_variable};
CellPutS(sValue,'{cube_name}',{comma_sep_var_elements});
"""
sValue = {value_variable};
CellPutS(sValue,'{cube_name}',{comma_sep_var_elements});
"""

input_statement = f"""
If({numeric_write_condition});
Expand Down Expand Up @@ -1338,8 +1341,7 @@ def _build_attribute_update_statements(cube_name, cellset_as_dict, precision: in
def _build_cell_update_statements(cube_name: str, cellset_as_dict: Dict, increment: bool,
measure_dimension_elements: Dict, precision: int = None,
skip_non_updateable: bool = False,
dimensions: List = None,
allow_spread: bool = False):
dimensions: List = None, allow_spread: bool = False):
statements = list()

for coordinates, value in cellset_as_dict.items():
Expand Down Expand Up @@ -1394,18 +1396,16 @@ def _build_cell_update_statements(cube_name: str, cellset_as_dict: Dict, increme
zip(dimensions, coordinates)])

consolidated_spread_check_start = f"""
IF({any_c_element_in_write});
CellPutProportionalSpread({value_str},'{cube_name}',{comma_separated_elements});
ELSE;
"""
IF({any_c_element_in_write});
CellPutProportionalSpread({value_str},'{cube_name}',{comma_separated_elements});
ELSE;
"""

consolidated_spread_check_end = "ENDIF;"
else:
consolidated_spread_check_start = ''
consolidated_spread_check_end = ''



statement = "".join([
cell_is_updateable_pre,
consolidated_spread_check_start,
Expand Down
6 changes: 4 additions & 2 deletions Tests/CellService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,14 @@ def test_write_use_blob_allow_spread(self):
except (TM1pyWriteFailureException, TM1pyWritePartialFailureException) as ex:
for log_file in ex.error_log_files:
print(self.tm1.processes.get_error_log_file_content(log_file))

query = MdxBuilder.from_cube(self.cube_with_consolidations_name)
query.add_member_tuple_to_columns(
f"[{self.dimensions_with_consolidations_names[0]}].[Element 1]",
f"[{self.dimensions_with_consolidations_names[1]}].[Element 4]",
f"[{self.dimensions_with_consolidations_names[2]}].[TOTAL_TM1py_Tests_Cell_Dimension3_With_Consolidations]")

self.assertEqual(self.tm1.cells.execute_mdx_values(mdx=query.to_mdx()), [54321])
self.assertAlmostEqual(54321, self.tm1.cells.execute_mdx_values(mdx=query.to_mdx())[0], delta=0.0001)

def test_write_use_ti_allow_spread(self):
cells = {("Element 1", "Element4", "Element9"): 1,
Expand All @@ -501,13 +502,14 @@ def test_write_use_ti_allow_spread(self):
except (TM1pyWriteFailureException, TM1pyWritePartialFailureException) as ex:
for log_file in ex.error_log_files:
print(self.tm1.processes.get_error_log_file_content(log_file))

query = MdxBuilder.from_cube(self.cube_with_consolidations_name)
query.add_member_tuple_to_columns(
f"[{self.dimensions_with_consolidations_names[0]}].[Element 1]",
f"[{self.dimensions_with_consolidations_names[1]}].[Element 4]",
f"[{self.dimensions_with_consolidations_names[2]}].[TOTAL_TM1py_Tests_Cell_Dimension3_With_Consolidations]")

self.assertEqual(self.tm1.cells.execute_mdx_values(mdx=query.to_mdx()), [54321])
self.assertAlmostEqual(54321, self.tm1.cells.execute_mdx_values(mdx=query.to_mdx())[0], delta=0.0001)

def test_write_use_ti_skip_non_updateable(self):
cells = CaseAndSpaceInsensitiveTuplesDict()
Expand Down

0 comments on commit 0dfb8ee

Please sign in to comment.