source: coopr.pyomo/trunk/coopr/pyomo/data/sheet.py @ 2289

Last change on this file since 2289 was 2289, checked in by wehart, 10 years ago

Setting up 'import' tests for the other simple examples.
These seem to work, but the internal code is too ugly. I'm
going to clean that up next to help explain what is going on...

File size: 2.2 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11import os.path
12from TableData import TableData
13from pyutilib.excel import ExcelSpreadsheet
14from coopr.pyomo.base.plugin import DataManagerRegistration
15
16class SheetTable(TableData):
17
18    def __init__(self):
19        TableData.__init__(self)
20
21    def open(self):
22        if self.filename is None:
23            raise IOError, "No filename specified"
24        if not os.path.exists(self.filename):
25            raise IOError, "Cannot find file '%s'" % self.filename
26        self.sheet = None
27        if self._data is not None:
28            self.sheet = self._data
29        else:
30            try:
31                self.sheet = ExcelSpreadsheet(self.filename)
32            except pyutilib.ApplicationError:
33                raise
34
35    def read(self):
36        if self.sheet is None:
37            return
38        tmp = self.sheet.get_range(self.options.range, raw=True)
39        if len(tmp) == 0:
40            raise IOError, "Empty range '%s'" % self.options.range
41        elif type(tmp) in (int,long,float):
42            if not self.options.param is None:
43                self._info = ["param",self.options.param,":=",tmp[0][0]]
44            elif len(self.options.symbol_map) == 1:
45                self._info = ["param",self.options.symbol_map[self.options.         symbol_map.keys()[0]],":=",tmp]
46            else:
47                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
48        else:
49            self._set_data(tmp[0], tmp[1:])
50        return True
51
52    def close(self):
53        if self._data is None and not self.sheet is None:
54            del self.sheet
55
56
57DataManagerRegistration("xls", SheetTable, "Manage IO with Excel XLS files.")
58
Note: See TracBrowser for help on using the repository browser.