.. _table-py: ######## table.py ######## def table2array(table): ======================= Create a single Numpy array from XML cash_flow table:: >>> from lxml import etree >>> from simo.builder.modelbase.table import table2array >>> tablexml = u""" ... 1 1 1 160 370 55 ... 2 1 1 160 400 56 ...
""" >>> elem = etree.fromstring(tablexml) >>> arr = table2array(elem) >>> arr array([[ 1., 1., 1., 160., 370., 55.], [ 2., 1., 1., 160., 400., 56.]]) def table2dict(table): ====================== Create a dictionary with the classifying values joined together as the dictionary key and the cash flow as the value; i.e., a row '1 2 3 4 5 6 7 100' translates to cdict['1:2:3:4:5:6:7']=100:: >>> from lxml import etree >>> from simo.builder.modelbase.table import table2dict >>> tablexml = u""" ... 1 1 1 160 370 55 ... 2 1 1 160 400 56 ...
""" >>> elem = etree.fromstring(tablexml) >>> dct = table2dict(elem) >>> dct {'1:1:1:160:370': 55.0, '2:1:1:160:400': 56.0} def parse_classifiers(ns, elem): ================================ Parse cash flow table and/or trend classifiers:: >>> class Lexicon(object): ... def variable_ind(self, level, var, active=False): ... return (1, 1) ... def level_ind(self, level): ... return 1 >>> class Validator(object): ... def __init__(self, lexicon): ... self.errors = set([]) ... self.warnings = [] ... self.lexicon = lexicon ... def variable_ind(self, level, variable): ... return self.lexicon.variable_ind(level, variable) ... def level_ind(self, level): ... return self.lexicon.level_ind(level) ... def add_error(self, msg): ... self.errors.add(msg) ... def add_warnings(self, msg): ... self.warnings.append(msg) ... model_type = 'cash_flow_table' >>> validator = Validator(Lexicon()) >>> from lxml import etree >>> from simo.builder.modelbase.table import parse_classifiers >>> cfierxml = u""" ... ... PRICE_REGION ... comp_unit ... ... ... SP ... comp_unit ... ... ... assortment ... within-operation ... ... ... ... ... ... """ >>> elem = etree.fromstring(cfierxml) >>> ns = "" >>> cfiers, db_cfiers, db_cfiers_ind, corder, keycount = \ ... parse_classifiers(ns, elem, validator) >>> 'PRICE_REGION' in cfiers True >>> c = cfiers['assortment'] >>> c.name 'assortment' >>> c.level 'within-operation' >>> c.ind >>> c.order 2 >>> c.variable_type >>> len(c.labels) 2 >>> l = c.labels[1] >>> l.label 'pulp' >>> l.value 2 >>> db_cfiers ['SP'] >>> keycount 2 def parse_tables(ns, elem): =========================== Parse element contents into ctypes array, value dictionary and activation time objects for each table:: >>> from lxml import etree >>> from simo.builder.modelbase.table import parse_tables >>> tablesxml = u""" ... ... 1 1 1 160 370 55 ... 2 1 1 160 400 56 ...
...
""" >>> elem = etree.fromstring(tablesxml) >>> tables, n = parse_tables(elem) >>> tables[0].array array([[ 1., 1., 1., 160., 370., 55.], [ 2., 1., 1., 160., 400., 56.]]) >>> tables[0].dict {'1:1:1:160:370': 55.0, '2:1:1:160:400': 56.0} >>> n 1 ******************** class Label(object): ******************** Class for cash flow table classifier labels Attributes: - label: label text - value: label value ************************* class Classifier(object): ************************* Class for cash flow table classifiers Attributes: - level: classifier level as string - name: classifier name as string - ind: classifier level-variable indices a tuple: (level, variable,) - labels: list of Label objects - order: classifier order as list of integers ******************** class Array(object): ******************** Class for cash flow table arrays Attributes: - array: table values in a numpy array - dict: table values in a dictionary - start_date: date object - end_date: date object