Model - Cubes meta-data objects and functionality for working with them. Logical Model and Metadata
Load a model from resource which can be a filename, URL, file-like object or a directory path.
If a resource is a directory it is considered a model bundle and has to have the following contents:
Create a model from a model description dictionary in model. This is designated way of creating the model from a dictionary.
cubes or dimensions are list of their respective dictionary definitions. If definition of a cube in cubes or dimension in dimensions already exists in the model, then ModelError is raised.
The model dictionary contains main model description. The structure is:
{
"name": "public_procurements",
"label": "Procurements",
"description": "Procurement Contracts of an Organisation"
"cubes": [...]
"dimensions": [...]
}
translations is a dictionary where keys are locale names and values are translation dictionaries. See Model.localize() for more information.
Creates a Cube instance from dictionary description desc with dimension dictionary in dimensions
In file based model representation, the cube descriptions are stored in json files with prefix cube_ like cube_contracts, or as a dictionary for key cubes in the model description dictionary.
JSON example:
{
"name": "contracts",
"measures": ["amount"],
"dimensions": [ "date", "contractor", "type"]
"details": ["contract_name"],
}
Creates a Dimension instance from obj which can be a Dimension instance or a string or a dictionary. If it is a string, then it represents dimension name, the only level name and the only attribute.
Keys of a dictionary representation:
name: dimension name
levels: list of dimension levels (see: cubes.Level)
list of level names of a single hierarchy. Only one of the two should be specified, otherwise an exception is raised.
default_hierarchy_name: name of a hierarchy that will be used when no hierarchy is explicitly specified
label: dimension name that will be displayed (human readable)
description: human readable dimension description
info - custom information dictionary, might be used to store application/front-end specific information (icon, color, ...)
template – name of a dimension to be used as template. The dimension is taken from dimensions argument which should be a dictionary of already created dimensions.
Defaults
String representation of a dimension str(dimension) is equal to dimension name.
Class is not meant to be mutable.
Raises ModelInconsistencyError when both hierarchy and hierarchies is specified.
Creates a level from obj which can be a Level instance, string or a dictionary. If it is a string, then the string represents level name and the only one attribute of the level. If obj is a dictionary, then the keys are:
Defaults:
There might be cases where one would like to analyse simple (denormalised) table. It can be either a table in a database or data from a single CSV file. For convenience, there is an utility function called simple_model that will create a model with just one cube, simple dimensions and couple of specified measures.
Create a simple model with only one cube with name cube_name`and flat dimensions. `dimensions is a list of dimension names as strings and measures is a list of measure names, also as strings. This is convenience method mostly for quick model creation for denormalized views or tables with data from a single CSV file.
Example:
model = simple_model("contracts",
dimensions=["year", "supplier", "subject"],
measures=["amount"])
cube = model.cube("contracts")
browser = workspace.create_browser(cube)
Logical Model represents analysts point of view on data.
Attributes:
The mappings is a dictiononary of form:
{
"cubes": {
"cube_name" : { ... }
},
"dimensions": {
"dimension_name" : {
"dimension_attribute": "physical_attribute"
}
}
}
The mappings in cubes are the same as mappings in the Cube definition. The logical name (keys) of dimension mappings is just dimension attribute name without the dimension prefix.
Adds cube to the model and also assigns the model to the cube. If cube has a model assigned and it is not this model, then error is raised.
Raises ModelInconsistencyError when trying to assing a cube that is already assigned to a different model or if trying to add a dimension with existing name but different specification.
Add dimension to model. Replace dimension with same name
Get a cube with name name or coalesce object to a cube.
Get dimension by name or by object. Raises NoSuchDimensionError when there is no dimension dim.
Check whether model is valid. Model is considered valid if there are no validation errors. If you want to be sure that there are no warnings as well, set strict to True. If strict is False only errors are considered fatal, if True also warnings will make model invalid.
Returns True when model is valid, otherwise returns False.
Get model locale dictionary - localizable parts of the model
Return localized version of the model.
translation might be a string or a dicitonary. If it is a string, then it represents locale name from model’s localizations provided on model creation. If it is a dictionary, it should contains full model translation that is going to be applied.
Translation dictionary structure example:
{
"locale": "sk",
"cubes": {
"sales": {
"label": "Predaje",
"measures":
{
"amount": "suma",
"discount": {"label": "zľava",
"description": "uplatnená zľava"}
}
}
},
"dimensions": {
"date": {
"label": "Dátum"
"attributes": {
"year": "rok",
"month": {"label": "mesiac"}
},
"levels": {
"month": {"label": "mesiac"}
}
}
}
}
Note
Whenever master model changes, you should call this method to get actualized localization of the original model.
Removes cube from the model
Remove a dimension from receiver
Return dictionary representation of the model. All object references within the dictionary are name based
Validate the model, check for model consistency. Validation result is array of tuples in form: (validation_result, message) where validation_result can be ‘warning’ or ‘error’.
Returs: array of tuples
Create a new Cube model.
Attributes:
Attributes used by backends:
Add dimension to cube. Replace dimension with same name. Raises ModelInconsistencyError when dimension with same name already exists in the receiver.
Get dimension object. If obj is a string, then dimension with given name is returned, otherwise dimension object is returned if it belongs to the cube.
Raises NoSuchDimensionError when there is no such dimension.
Get measure object. If obj is a string, then measure with given name is returned, otherwise measure object is returned if it belongs to the cube. Returned object is of Attribute type.
Raises NoSuchAttributeError when there is no such measure or when there are multiple measures with the same name (which also means that the model is not valid).
Remove a dimension from receiver. dimension can be either dimension name or dimension object.
Convert to a dictionary. If expand_dimensions is True (default is False) then fully expand dimension information If with_mappings is True (which is default) then joins, mappings, fact and options are included. Should be set to False when returning a dictionary that will be provided in an user interface or through server API.
Validate cube. See Model.validate() for more information.
Create a new dimension
Attributes:
Dimension class is not meant to be mutable. All level attributes will have new dimension assigned.
Note that the dimension will claim ownership of levels and their attributes. You should make sure that you pass a copy of levels if you are cloning another dimension.
Return all dimension attributes regardless of hierarchy. Order is not guaranteed, use cubes.Hierarchy.all_attributes() to get known order. Order of attributes within level is preserved.
Get dimension attribute from reference.
Get default hierarchy specified by default_hierarchy_name, if the variable is not set then get a hierarchy with name default
Warning
Depreciated. Use Dimension.hierarchy() instead.
Returns True when each level has only one attribute, usually key.
Get hierarchy object either by name or as Hierarchy. If obj is None then default hierarchy is returned.
Is true if dimension has only one level
Return all dimension key attributes, regardless of hierarchy. Order is not guaranteed, use a hierarchy to have known order.
Get level by name or as Level object. This method is used for coalescing value
Get list of level names. Order is not guaranteed, use a hierarchy to have known order.
Get list of all dimension levels. Order is not guaranteed, use a hierarchy to have known order.
Return dictionary representation of the dimension
Validate dimension. See Model.validate() for more information.
Dimension hierarchy - specifies order of dimension levels.
Attributes:
Some collection operations might be used, such as level in hierarchy or hierarchy[index]. String value str(hierarchy) gives the hierarchy name.
Return all dimension attributes as a single list.
Returns True if level is last level of the hierarchy.
Return all dimension key attributes as a single list.
Get order index of level. Can be used for ordering and comparing levels within hierarchy.
Returns levels for given depth. If path is longer than hierarchy levels, cubes.ArgumentError exception is raised
Returns levels for given path. If path is longer than hierarchy levels, cubes.ArgumentError exception is raised
Returns next level in hierarchy after level. If level is last level, returns None. If level is None, then the first level is returned.
Returns True if path is base path for the hierarchy. Base path is a path where there are no more levels to be added - no drill down possible.
Returns previous level in hierarchy after level. If level is first level or None, returns None
Rolls-up the path to the level. If level is None then path is rolled-up only one level.
If level is deeper than last level of path the cubes.HierarchyError exception is raised. If level is the same as path level, nothing happens.
Convert to dictionary. Keys:
Object representing a hierarchy level. Holds all level attributes.
This object is immutable, except localization. You have to set up all attributes in the initialisation process.
Attributes:
Get attribute by name
Is True when level has more than one attribute, for all levels with only one attribute it is False.
Convert to dictionary
Cube attribute - represents any fact field/column
Attributes:
String representation of the Attribute returns its name (without dimension prefix).
cubes.ArgumentError is raised when unknown ordering type is specified.
Return full name of an attribute as if it was part of dimension. Append locale if it is one of of attribute’s locales, otherwise raise cubes.ArgumentError.
Return full attribute reference. Append locale if it is one of of attribute’s locales, otherwise raise cubes.ArgumentError. If simplify is True, then reference to an attribute of flat dimension without details will be just the dimension name.
Warning
This method might be renamed.
Helper function to coalesce list of attributes, which can be provided as strings or as Attribute objects:
Create a list of attributes from a list of strings or dictionaries. see cubes.coalesce_attribute() for more information.
Exception raised when there is an error with model and its structure, mostly during model construction.
Raised when there is incosistency in model structure, mostly when model was created programatically in a wrong way by mismatching classes or misonfiguration.
Raised when a dimension is requested that does not exist in the model.
Raised when an unknown attribute, measure or detail requested.