Source code for af.utils.automatic_dimensions

from af.model.hierarchies.BaseHierarchy import BaseHierarchy


[docs]class AutomaticDimension(object): """Class to create automatic dimensions for the hierarchies """ def __init__(self, list_of_values): self.set_of_values = set([str(val) for val in list_of_values])
[docs] def create_dimensions(self, dimensions_queue=None): if dimensions_queue is None: dimensions_queue = [{key: None} for key in self.set_of_values] new_dimensions_queue = {} for element in dimensions_queue: parent = self.get_parent(element.keys()[0]) if parent not in new_dimensions_queue.keys(): new_dimensions_queue[parent] = element else: new_dimensions_queue[parent].update(element) if len(new_dimensions_queue) == 1: return new_dimensions_queue else: return self.create_dimensions([{key: value} for key, value in new_dimensions_queue.iteritems()])
[docs]class IntPartialSupressionLeftToRight(AutomaticDimension): def __init__(self, list_of_values, amount_to_supress=2): AutomaticDimension.__init__(self, list_of_values) self.amount_to_supress = amount_to_supress
[docs] def get_parent(self, value): if not '*' in value: supressed = '' not_supressed = value else: supressed = value[0:value.rfind('*')+1] not_supressed = value[value.rfind('*')+1:] if len(supressed) == len(value): return value to_supress = '*'*self.amount_to_supress still_not_supressed = not_supressed[1*self.amount_to_supress:] parent = supressed+to_supress+still_not_supressed if all('*' == i for i in parent): parent = BaseHierarchy.supression_node().value return parent
[docs]class IntPartialSupressionRightToLeft(IntPartialSupressionLeftToRight): def __init__(self, list_of_values, amount_to_supress=2): IntPartialSupressionLeftToRight.__init__(self, list_of_values, amount_to_supress)
[docs] def get_parent(self, value): aux = value[::-1] result = IntPartialSupressionLeftToRight.get_parent(self, aux) return result [::-1]
[docs]class IntRange(AutomaticDimension): def __init__(self, list_of_values, amount_range=10): AutomaticDimension.__init__(self, list_of_values) self.amount_range = amount_range
[docs] def get_parent(self, value): pass
[docs]class DatePartialSupressionYYYYMMDD(AutomaticDimension): def __init__(self, list_of_values): AutomaticDimension.__init__(self, list_of_values)
[docs] def get_parent(self, value): year, month, day = value.split('/') if '*' not in day: day = '**' elif '*' not in month: month = '**' else: #Full supression of value result = BaseHierarchy.supression_node().value return result result = "{0}/{1}/{2}".format(year, month, day) return result
[docs]class DatePartialSupressionDDMMYYYY(DatePartialSupressionYYYYMMDD): def __init__(self, list_of_values): DatePartialSupressionYYYYMMDD.__init__(self, list_of_values)
[docs] def get_parent(self, value): aux = value[::-1] result = DatePartialSupressionYYYYMMDD.get_parent(self, aux) return result [::-1]