.. _output-py: ######### output.py ######### ********************* class Output(object): ********************* A base class for all the output formats. Holds the common functionality. def __init__(self, datadb, data_type, id_list, main_level, result_type, output_filename, output_constraint=None, default_decimal_places=1, archiving=False, result_padding=True, aggregation_def=None, expression_def=None, opres_vars=None): ============================================================================================================================================================================================================================================= Creates the output class, saves all the parameters to self and executes self.run() :: >>> from simo.output.test.init_objects import InitData, TestLogger >>> idata = InitData() >>> idata.init() >>> testdb = idata.testdb >>> const_obj = idata.const_obj >>> aggr_obj = idata.aggr_obj >>> expr_obj = idata.expr_obj >>> from simo.output.output import Output >>> out = Output(testdb, 'result', ['stand1'], 'stratum', ... 'optimized', ... 'output/test/test.txt', TestLogger(), ... const_obj, 1, False, True, aggr_obj, expr_obj, ... ['cash_flow', 'Income', 'Scrapwood', 'Volume', ... 'BIOMASS_branches', 'BIOMASS_stumps']) def run(self): ============== Uses data from init to run the class-specific output. Does nothing; override in children. def _pad_all(self, rows): ========================= This function will format a two dimentional list of rows (of colums) so that on every row the columns are the same width (the widest column on any row for that index, with a leading space) :: >>> rows = [['1', '11', '111', '1111', '11111'], ... ['111', '1', '11', '111'], ... ['1', '11', '111', '1', '11', '11111']] >>> for i in out._pad_all(rows): print i [' 1', ' 11', ' 111', ' 1111', ' 11111'] [' 111', ' 1', ' 11', ' 111'] [' 1', ' 11', ' 111', ' 1', ' 11', ' 11111'] def _get_level_headers(self, level, level_const=None): ====================================================== Returns the headers for a given level in datadb :: >>> headers = out._get_level_headers(3) >>> headers ['branch', 'iteration', 'id', 'oid', 'data_date'] >>> headers = out._get_level_headers(3, level_const=(('d', 1), ('h', 2))) >>> headers ['branch', 'iteration', 'id', 'oid', 'data_date', 'd', 'h'] >>> headers = out._get_level_headers(2, out._get_level_constraints(2)) def _get_level_constraints(self, level): ======================================== Returns the headers to be used for a given level in datadb :: >>> out._get_level_constraints(1) [('AREA', 0), ('SC', 1)] >>> out._get_level_constraints(2) [('SP', 0), ('BA', 1), ('HgM', 3)] >>> out._get_level_constraints(3) [('d', 0), ('h', 1)] def _write_file(self, file, strings): ===================================== This will write a list of strings to the given file :: >>> out._write_file('test.txt', ['test', 'piece', 'of', 'text']) >>> lines = [] >>> try: ... file = open('test.txt', 'r') ... for line in file: ... print line.rstrip('\n') ... finally: ... file.close() test piece of text >>> try: ... os.remove('test.txt') ... except: ... pass def _listflt2str(self, list, d): ================================ Turns a list of floats into strings with d decimals. If the list contains non-float values, they are converted to strings with str(). This method operates in-place and returns nothing. :: >>> float_list = [1.0, 2.1111111, 3, 4.34235, '5.43abcde'] >>> out._listflt2str(float_list, 3) >>> float_list ['1.000', '2.111', '3', '4.342', u'5.43abcde'] def _has_data(self, level_dict): ================================ Checks if a level keyed dictionary has data other than empty lists in it. :: >>> out._has_data({}) False >>> out._has_data({1:[]}) False >>> out._has_data({1:[True]}) True