Table Of Contents

Previous topic

inline.py

Next topic

out.py

This Page

opres.py

class OutputOpres(Output):

This module handles writing operation results into files

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, group_by_vars=None):

Creates the output class. fully inherited, executes self.run()

>>> from pprint import pprint
>>> 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.opres import OutputOpres
>>> out = OutputOpres(testdb, 'result', ['stand1'], 'stratum',
...               'optimized',
...               'output/test/opres.txt', TestLogger(),
...               const_obj, 1, False, True, aggr_obj, expr_obj,
...               ['cash_flow', 'Income', 'Scrapwood', 'Volume',
...               'BIOMASS_branches', 'BIOMASS_stumps'], ['assortment',
...               'SP'])
>>> try: 
...     file = open('output/test/opres.txt', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close() 
  op_level;     id; op_date; iteration; branch;      op_group;  op_name; Volume; cash_flow; assortment;  SP
 comp_unit; stand1;    2009;         0;      0;  regeneration; planting;       ;    -456...;           ;
 comp_unit; stand1;    2009;         0;      0; final_harvest; clearcut;       ;    3456.7;           ;
 comp_unit; stand1;    2009;         0;      0; final_harvest; clearcut;  123.2;          ;        5.0; 1.0

def run(self):

Uses data from init to run the class-specific output (self.opres)

def _get_opres_strings(self, constraints=None, dates=None, date_res=0):

This function will get the operation results from the datadb and returns them in a formatted list of rows. The constraints, dates and date_res are as above. Classifiers are prepended to the end of the row, where applicable

>>> headers = ['op_id','op_level','id','op_date',
...            'iteration','branch','op_group','op_name'] + \
...           ['Volume', 'cash_flow']
>>> opres = out._get_opres_strings(headers, constraints=None,
...                                dates=None, date_res=0)
>>> pprint(opres) 
['  op_level;     id; op_date; iteration; branch;      op_group;  op_name; Volume; cash_flow; assortment;  SP',
 u' comp_unit; stand1;    2009;         0;      0;  regeneration; planting;       ;    -456...;           ;    ',
 u' comp_unit; stand1;    2009;         0;      0; final_harvest; clearcut;       ;    3456.7;           ;    ',
 u' comp_unit; stand1;    2009;         0;      0; final_harvest; clearcut;  123.2;          ;        5.0; 1.0']
>>> pprint(out._get_opres_strings(['op_id','op_level','id','op_date',
...                'iteration','branch','op_group','op_name', 'cash_flow'],
...                constraints=None, dates=None, date_res=0))
... 
['  op_level;     id; op_date; iteration; branch;      op_group;  op_name; cash_flow; assortment;         SP',
 u' comp_unit; stand1;    2009;         0;      0;  regeneration; planting;    -456...;           ;           ',
 u' comp_unit; stand1;    2009;         0;      0; final_harvest; clearcut;    3456.7;           ;           ',
 u' comp_unit; stand1;    2009;         0;      0; final_harvest; clearcut;          ;        5.0;        1.0']

def opres(self, file, constraints=None, dates=None, date_res=0):

This function will use _get_opres_strings with the given restrictions and date resolution, and writes the results into the given file

>>> out.opres('test.txt')
>>> lines = []
>>> try:
...     file = open('test.txt', 'r')
...     for line in file:
...         lines.append(line.rstrip('\n'))
... finally:
...     file.close()
>>> lines == opres
True