def setUpClass(cls): """ Establishes a connection to TM1 and creates TM! objects to use across all tests """ # Connection to TM1 cls.config = configparser.ConfigParser() cls.config.read(Path(__file__).parent.joinpath('config.ini')) cls.tm1 = TM1Service(**cls.config['tm1srv01']) # generate random coordinates cls.target_coordinates = list(zip(('Element ' + str(e) for e in range(1, 100)), ('Element ' + str(e) for e in range(1, 100)), ('Element ' + str(e) for e in range(1, 100)))) # Build Dimensions for dimension_name in cls.dimension_names: elements = [Element('Element {}'.format(str(j)), 'Numeric') for j in range(1, 1001)] element_attributes = [ElementAttribute("Attr1", "String"), ElementAttribute("Attr2", "Numeric"), ElementAttribute("Attr3", "Numeric")] hierarchy = Hierarchy(dimension_name=dimension_name, name=dimension_name, elements=elements, element_attributes=element_attributes) dimension = Dimension(dimension_name, [hierarchy]) cls.tm1.dimensions.update_or_create(dimension) attribute_cube = "}ElementAttributes_" + dimension_name attribute_values = dict() for element in elements: attribute_values[(element.name, "Attr1")] = "TM1py" attribute_values[(element.name, "Attr2")] = "2" attribute_values[(element.name, "Attr3")] = "3" cls.tm1.cubes.cells.write_values(attribute_cube, attribute_values) # Build Cube cube = Cube(cls.cube_name, cls.dimension_names) if not cls.tm1.cubes.exists(cls.cube_name): cls.tm1.cubes.create(cube) # Sum of all the values that we write in the cube. serves as a checksum. cls.total_value = 0 # cellset of data that shall be written cls.cellset = {} for element1, element2, element3 in cls.target_coordinates: value = 1 cls.cellset[(element1, element2, element3)] = value # update the checksum cls.total_value += value # Fill cube with values cls.tm1.cubes.cells.write_values(cls.cube_name, cls.cellset) # Elements cls.years = ("No Year", "1989", "1990", "1991", "1992") cls.extra_year = "4321" # Element Attributes cls.attributes = ('Previous Year', 'Next Year') cls.alias_attributes = ("Financial Year",) # create dimension with a default hierarchy d = Dimension(cls.dimension_name) h = Hierarchy(cls.dimension_name, cls.dimension_name) h.add_element('Total Years', 'Consolidated') h.add_element('All Consolidations', 'Consolidated') h.add_edge("All Consolidations", "Total Years", 1) for year in cls.years: h.add_element(year, 'Numeric') h.add_edge('Total Years', year, 1) for attribute in cls.attributes: h.add_element_attribute(attribute, "String") for attribute in cls.alias_attributes: h.add_element_attribute(attribute, "Alias") d.add_hierarchy(h) cls.tm1.dimensions.update_or_create(d) # write attribute values cls.tm1.cubes.cells.write_value('1988', '}ElementAttributes_' + cls.dimension_name, ('1989', 'Previous Year')) cls.tm1.cubes.cells.write_value('1989', '}ElementAttributes_' + cls.dimension_name, ('1990', 'Previous Year')) cls.tm1.cubes.cells.write_value('1990', '}ElementAttributes_' + cls.dimension_name, ('1991', 'Previous Year')) cls.tm1.cubes.cells.write_value('1991', '}ElementAttributes_' + cls.dimension_name, ('1992', 'Previous Year')) cls.tm1.cubes.cells.write_value('1988/89', '}ElementAttributes_' + cls.dimension_name, ('1989', 'Financial Year')) cls.tm1.cubes.cells.write_value('1989/90', '}ElementAttributes_' + cls.dimension_name, ('1990', 'Financial Year')) cls.tm1.cubes.cells.write_value('1990/91', '}ElementAttributes_' + cls.dimension_name, ('1991', 'Financial Year')) cls.tm1.cubes.cells.write_value('1991/92', '}ElementAttributes_' + cls.dimension_name, ('1992', 'Financial Year'))
def get(self, dimension_name, hierarchy_name, element_name): request = "/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements('{}')?$expand=*" \ .format(dimension_name, hierarchy_name, element_name) response = self._rest.GET(request) return Element.from_dict(response.json())
def setup_class(cls): # Connection to TM1 cls.tm1 = TM1Service(**config['tm1srv01']) # generate random coordinates cls.target_coordinates = list( zip(('Element ' + str(random.randint(1, 1000)) for _ in range(100)), ('Element ' + str(random.randint(1, 1000)) for _ in range(100)), ('Element ' + str(random.randint(1, 1000)) for _ in range(100)))) # Build Dimensions for dimension_name in DIMENSION_NAMES: elements = [ Element('Element {}'.format(str(j)), 'Numeric') for j in range(1, 1001) ] element_attributes = [ ElementAttribute("Attr1", "String"), ElementAttribute("Attr2", "Numeric"), ElementAttribute("Attr3", "Numeric") ] hierarchy = Hierarchy(dimension_name=dimension_name, name=dimension_name, elements=elements, element_attributes=element_attributes) dimension = Dimension(dimension_name, [hierarchy]) if cls.tm1.dimensions.exists(dimension.name): cls.tm1.dimensions.update(dimension) else: cls.tm1.dimensions.create(dimension) attribute_cube = "}ElementAttributes_" + dimension_name attribute_values = dict() for element in elements: attribute_values[(element.name, "Attr1")] = "TM1py" attribute_values[(element.name, "Attr2")] = "2" attribute_values[(element.name, "Attr3")] = "3" cls.tm1.cubes.cells.write_values(attribute_cube, attribute_values) # Build Cube cube = Cube(CUBE_NAME, DIMENSION_NAMES) if not cls.tm1.cubes.exists(CUBE_NAME): cls.tm1.cubes.create(cube) # Build cube view view = NativeView(cube_name=CUBE_NAME, view_name=VIEW_NAME, suppress_empty_columns=True, suppress_empty_rows=True) view.add_row(dimension_name=DIMENSION_NAMES[0], subset=AnonymousSubset(dimension_name=DIMENSION_NAMES[0], expression='{[' + DIMENSION_NAMES[0] + '].Members}')) view.add_row(dimension_name=DIMENSION_NAMES[1], subset=AnonymousSubset(dimension_name=DIMENSION_NAMES[1], expression='{[' + DIMENSION_NAMES[1] + '].Members}')) view.add_column( dimension_name=DIMENSION_NAMES[2], subset=AnonymousSubset(dimension_name=DIMENSION_NAMES[2], expression='{[' + DIMENSION_NAMES[2] + '].Members}')) cls.tm1.cubes.views.create(view=view, private=False) # Sum of all the values that we write in the cube. serves as a checksum. cls.total_value = 0 # cellset of data that shall be written cls.cellset = {} for element1, element2, element3 in cls.target_coordinates: value = random.randint(1, 1000) cls.cellset[(element1, element2, element3)] = value # update the checksum cls.total_value += value # Fill cube with values cls.tm1.cubes.cells.write_values(CUBE_NAME, cls.cellset)
def get(self, dimension_name: str, hierarchy_name: str, element_name: str, **kwargs) -> Element: url = format_url( "/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements('{}')?$expand=*", dimension_name, hierarchy_name, element_name) response = self._rest.GET(url, **kwargs) return Element.from_dict(response.json())
config.read(r'..\config.ini') # Time magic with python generator def daterange(start_date, end_date): for n in range(int((end_date - start_date).days)): yield start_date + timedelta(n) # push data to TM1 with TM1Service(**config['tm1srv01']) as tm1: # ============================ # create TM1 objects for fx rates sample currencies = ('RMB', 'EUR', 'JPY', 'CHF', 'USD', 'AUD', 'TWD', 'HKD', 'GBP', 'SGD', 'INR') elements = [Element(cur, 'Numeric') for cur in currencies] # create dimension TM1py Currency From hierarchy = Hierarchy('TM1py Currency From', 'TM1py Currency From', elements) dimension = Dimension('TM1py Currency From', [hierarchy]) if not tm1.dimensions.exists(dimension.name): tm1.dimensions.create(dimension) # create dimension TM1py Currency To hierarchy = Hierarchy('TM1py Currency To', 'TM1py Currency To', elements) dimension = Dimension('TM1py Currency To', [hierarchy]) if not tm1.dimensions.exists(dimension.name): tm1.dimensions.create(dimension) # create dimension TM1py Date
def test01_create_element(self): element = Element(self.extra_year, "String") self.tm1.dimensions.hierarchies.elements.create( self.dimension_name, self.hierarchy_name, element)
""" Create a new dimension TM1py Big dimension with 100,000 elements """ import configparser config = configparser.ConfigParser() config.read('..\config.ini') from TM1py.Services import TM1Service from TM1py.Objects import Dimension, Hierarchy, Element from TM1py.Utils import Utils # Establish connection to TM1 Server with TM1Service(**config['tm1srv01']) as tm1: # Create Elements, Edges and stuff in python elements = [ Element('Element {}'.format(i), 'Numeric') for i in range(1, 100001) ] elements.append(Element('Even', 'Consolidated')) elements.append(Element('Odd', 'Consolidated')) elements.append(Element('4 Digit', 'Consolidated')) elements.append(Element('3 Digit', 'Consolidated')) edges = Utils.CaseAndSpaceInsensitiveTuplesDict() for i in range(2, 100001, 2): parent_child = ('Even', 'Element {}'.format(i)) edges[parent_child] = 1 for i in range(1, 100001, 2): parent_child = ('Odd', 'Element {}'.format(i)) edges[parent_child] = 1
def get_leaf_elements(self, dimension_name, hierarchy_name): request = "/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements?$expand=*&$filter=Type ne 3" \ .format(dimension_name, hierarchy_name) response = self._rest.GET(request) return [Element.from_dict(element) for element in response.json()["value"]]